Skip to content

Commit

Permalink
πŸ’š Deploy μ›Œν¬ν”Œλ‘œμš° μˆ˜μ •
Browse files Browse the repository at this point in the history
  • Loading branch information
waterfogSW committed Sep 22, 2024
1 parent 0e265cb commit 530b6be
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 44 deletions.
85 changes: 41 additions & 44 deletions .github/workflows/cd-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,29 +39,35 @@ 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
fi
- 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:
Expand All @@ -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": [
Expand All @@ -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
Expand All @@ -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 }}"
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ name: Continuous Integration
on:
push:
branches: [main]
paths-ignore:
- '.github/**'
workflow_dispatch:
inputs:
version_type:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test_analyze.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ on:
push:
branches:
- main
paths-ignore:
- '.github/**'
pull_request:
types: [opened, synchronize, reopened]
jobs:
Expand Down

0 comments on commit 530b6be

Please sign in to comment.