Skip to content

refactor: update stage_CD #221

refactor: update stage_CD

refactor: update stage_CD #221

Workflow file for this run

name: STAGE_CD
on:
push:
branches:
- develop
paths-ignore:
- '.github/ISSUE_TEMPLATE/**'
- '.github/pull_request_template.md'
- 'README.md'
workflow_dispatch:
env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: stage-daedong-api
ECS_TASK_DEFINITION: daedong-api
CONTAINER_NAME: daedong-api
ECS_SERVICE: daedong-api
ECS_CLUSTER: stage-daedong
IMAGE_TAG_NAME: api_image_tag
permissions:
id-token: write
contents: read
actions: read
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
role-to-assume: arn:aws:iam::637423658689:role/stage-GithubActionsRole
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Set up JDK 17
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: 17
cache: gradle
- name: Cache gradle modules
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Build with Gradle
run: |
chmod +x ./gradlew
./gradlew build
- name: Set up docker buildx
id: buildx
uses: docker/setup-buildx-action@v2
- name: Cache docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ env.ECR_REPOSITORY }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "IMAGE_TAG=$IMAGE_TAG" >> $GITHUB_OUTPUT
# - name: Checkout Infra Repository
# uses: actions/checkout@v4
# with:
# path: terraform
# repository: 'daedongbread/bread-map-infra'
# token: ${{ secrets.INFRA_REPO_ACCESS_TOKEN }}
# ref: stage
# - name: Update Image Tag
# env:
# IMAGE_TAG_NAME: ${{ env.IMAGE_TAG_NAME }}
# IMAGE_TAG: ${{ steps.build-image.outputs.IMAGE_TAG }}
# working-directory: terraform/root/stage
# run: |
# sed -i "s/${IMAGE_TAG_NAME} = \".*\"/${IMAGE_TAG_NAME} = \"$IMAGE_TAG\"/" stage.tfvars
# - name: Commit and Push and Merge PR
# env:
# IMAGE_TAG: ${{ steps.build-image.outputs.IMAGE_TAG }}
# GITHUB_TOKEN: ${{ secrets.INFRA_REPO_ACCESS_TOKEN }}
# GITHUB_ACTOR: ${{ github.actor }}
# PR_URL: ${{ github.event.pull_request.html_url }}
# working-directory: terraform/root/stage
# run: |
# git config user.name 'daedong'
# git config user.email '[email protected]'
# git checkout -b deploy/$IMAGE_TAG
# git add stage.tfvars
# git commit -m "deploy: update api image tag"
# git push origin deploy/$IMAGE_TAG
# if [[ "${{ github.event_name }}" == "pull_request" ]]; then
# PR_BODY="Automated PR to deploy **[API]** by update image tag<br/><br/><br/>open by $PR_URL @$GITHUB_ACTOR"
# elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
# PR_BODY="Automated PR to deploy **[API]** by update image tag<br/><br/><br/>open by workflow_dispatch by @$GITHUB_ACTOR"
# fi
# echo "$PR_BODY" > pr-body.txt
# PR_URL=$(gh pr create --title "API 개발 환경 배포" --body-file pr-body.txt --base stage --head deploy/$IMAGE_TAG)
# gh pr merge $PR_URL --merge
- name: CD notification to Slack
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took
author_name: Backend CD
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}