From 530b6be766b2d749bbf9e6e44f18d5215bab052a Mon Sep 17 00:00:00 2001 From: San Kim Date: Sun, 22 Sep 2024 15:38:19 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=9A=20Deploy=20=EC=9B=8C=ED=81=AC?= =?UTF-8?q?=ED=94=8C=EB=A1=9C=EC=9A=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/cd-dev.yaml | 85 ++++++++++++++--------------- .github/workflows/ci.yaml | 2 + .github/workflows/test_analyze.yaml | 2 + 3 files changed, 45 insertions(+), 44 deletions(-) diff --git a/.github/workflows/cd-dev.yaml b/.github/workflows/cd-dev.yaml index 0b9abf5..e5a0dca 100644 --- a/.github/workflows/cd-dev.yaml +++ b/.github/workflows/cd-dev.yaml @@ -2,13 +2,14 @@ name: Continuous Deployment to Dev on: repository_dispatch: - types: [trigger-cd] + types: [ trigger-cd ] workflow_dispatch: inputs: version: description: 'Version to deploy (e.g., v1.2.3)' required: true type: string + default: 'latest' env: AWS_REGION: ap-northeast-2 @@ -38,11 +39,15 @@ jobs: - name: Determine version to deploy id: determine-version + env: + GITHUB_EVENT_NAME: ${{ github.event_name }} + REPOSITORY_DISPATCH_VERSION: ${{ github.event.client_payload.version }} + WORKFLOW_DISPATCH_VERSION: ${{ github.event.inputs.version }} run: | - if [ "${{ github.event_name }}" = "repository_dispatch" ]; then - echo "VERSION=${{ github.event.client_payload.version }}" >> $GITHUB_OUTPUT - elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then - echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT + if [ "${GITHUB_EVENT_NAME}" = "repository_dispatch" ]; then + echo "VERSION=${REPOSITORY_DISPATCH_VERSION}" >> $GITHUB_OUTPUT + elif [ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ]; then + echo "VERSION=${WORKFLOW_DISPATCH_VERSION}" >> $GITHUB_OUTPUT else echo "Error: Unexpected event type" exit 1 @@ -50,17 +55,19 @@ jobs: - name: Check if image exists in ECR id: check-image + env: + ECR_REPOSITORY: ${{ env.ECR_REPOSITORY }} + VERSION: ${{ steps.determine-version.outputs.VERSION }} run: | - VERSION=${{ steps.determine-version.outputs.VERSION }} set +e aws ecr describe-images --repository-name $ECR_REPOSITORY --image-ids imageTag=$VERSION EXIT_CODE=$? set -e if [ $EXIT_CODE -ne 0 ]; then - echo "::error::Image with tag $VERSION does not exist in ECR repository $ECR_REPOSITORY" + echo "::error::ECR 리포지토리 $ECR_REPOSITORY 에 태그 $VERSION 의 이미지가 존재하지 않습니다." exit 1 fi - echo "Image found in ECR" + echo "ECR에서 이미지를 찾았습니다." - name: Generate Dockerrun.aws.json with environment variables env: @@ -70,11 +77,11 @@ jobs: VARS_CONTEXT: ${{ toJson(vars) }} SECRETS_CONTEXT: ${{ toJson(secrets) }} run: | - # 시작 부분 작성 - echo '{ + cat > Dockerrun.aws.json << EOF + { "AWSEBDockerrunVersion": "1", "Image": { - "Name": "'"$REGISTRY_URL"'/'"$ECR_REPOSITORY"':'"$VERSION"'", + "Name": "${REGISTRY_URL}/${ECR_REPOSITORY}:${VERSION}", "Update": "true" }, "Ports": [ @@ -83,41 +90,31 @@ jobs: "HostPort": 80 } ], - "Environment": [' > Dockerrun.aws.json - - # GitHub Secrets 추가 - FIRST_VAR=true - echo "$SECRETS_CONTEXT" | jq -r 'to_entries[] | select(.key | test("^AWS_") | not) | "\(.key)=\(.value)"' | while IFS='=' read -r SECRET_KEY SECRET_VALUE; do - if [ "$FIRST_VAR" = false ]; then - echo ',' >> Dockerrun.aws.json - fi - echo ' { - "Name": "'"$SECRET_KEY"'", - "Value": "'"$SECRET_VALUE"'" - }' >> Dockerrun.aws.json - FIRST_VAR=false - done - - # GitHub Vars 추가 - echo "$VARS_CONTEXT" | jq -r 'to_entries[] | "\(.key)=\(.value)"' | while IFS='=' read -r VAR_KEY VAR_VALUE; do - if [ "$FIRST_VAR" = false ]; then - echo ',' >> Dockerrun.aws.json - fi - echo ' { - "Name": "'"$VAR_KEY"'", - "Value": "'"$VAR_VALUE"'" - }' >> Dockerrun.aws.json - FIRST_VAR=false - done - - # JSON 마무리 - echo ' - ] - }' >> Dockerrun.aws.json + "Volumes": [], + "Logging": "/var/log/nginx" + } + EOF + + echo '{}' | jq --argjson secrets "$SECRETS_CONTEXT" \ + --argjson vars "$VARS_CONTEXT" \ + '$secrets + $vars | to_entries | map(select(.key | test("^AWS_") | not) | {key: .key, value: .value})' \ + > env_vars.json + + - name: Create .ebextensions directory + run: mkdir -p .ebextensions + + - name: Create env.config file + run: | + cat > .ebextensions/env.config << EOF + option_settings: + aws:elasticbeanstalk:application:environment: + EOF + + jq -r '.[] | " \(.key): \"\(.value)\""' env_vars.json >> .ebextensions/env.config - name: Create deployment package run: | - zip -r deploy.zip Dockerrun.aws.json + zip -r deploy.zip Dockerrun.aws.json .ebextensions - name: Deploy to Elastic Beanstalk uses: einaregilsson/beanstalk-deploy@v22 @@ -129,7 +126,7 @@ jobs: version_label: ${{ steps.determine-version.outputs.VERSION }}-${{ github.sha }} region: ${{ env.AWS_REGION }} deployment_package: deploy.zip - use_existing_version_if_available: true + use_existing_version_if_available: false - name: Deployment result run: echo "Deployed version ${{ steps.determine-version.outputs.VERSION }} to Elastic Beanstalk environment ${{ env.EB_ENVIRONMENT_NAME }}" diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 78cb670..e0ee338 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -4,6 +4,8 @@ name: Continuous Integration on: push: branches: [main] + paths-ignore: + - '.github/**' workflow_dispatch: inputs: version_type: diff --git a/.github/workflows/test_analyze.yaml b/.github/workflows/test_analyze.yaml index 78e8bea..3ff7964 100644 --- a/.github/workflows/test_analyze.yaml +++ b/.github/workflows/test_analyze.yaml @@ -3,6 +3,8 @@ on: push: branches: - main + paths-ignore: + - '.github/**' pull_request: types: [opened, synchronize, reopened] jobs: