Skip to content

Continuous Deployment to Dev #28

Continuous Deployment to Dev

Continuous Deployment to Dev #28

Workflow file for this run

name: Continuous Deployment to Dev
on:
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
ECR_REPOSITORY: threedays-app
EB_APPLICATION_NAME: threedays-dev
EB_ENVIRONMENT_NAME: threedays-dev-env
DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
jobs:
deploy:
runs-on: ubuntu-latest
environment: dev
steps:
- name: Checkout code
uses: actions/checkout@v4
- 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=${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: Send Deployment Start Webhook
env:
WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
VERSION: ${{ steps.determine-version.outputs.VERSION }}
run: |
curl -H "Content-Type: application/json" -X POST -d '{
"embeds": [{
"title": "배포 시작",
"color": 16776960,
"fields": [
{
"name": "버전",
"value": "'"${VERSION}"'",
"inline": true
},
{
"name": "환경",
"value": "'"${EB_ENVIRONMENT_NAME}"'",
"inline": true
},
{
"name": "배포자",
"value": "'"${GITHUB_ACTOR}"'",
"inline": true
}
],
"url": "'"${WORKFLOW_URL}"'"
}]
}' ${{ env.DISCORD_WEBHOOK }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Check if image exists in ECR
id: check-image
env:
ECR_REPOSITORY: ${{ env.ECR_REPOSITORY }}
VERSION: ${{ steps.determine-version.outputs.VERSION }}
run: |
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::ECR 리포지토리 $ECR_REPOSITORY 에 태그 $VERSION 의 이미지가 존재하지 않습니다."
exit 1
fi
echo "ECR에서 이미지를 찾았습니다."
- name: Generate Dockerrun.aws.json with environment variables
env:
VERSION: ${{ steps.determine-version.outputs.VERSION }}
REGISTRY_URL: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ env.ECR_REPOSITORY }}
VARS_CONTEXT: ${{ toJson(vars) }}
SECRETS_CONTEXT: ${{ toJson(secrets) }}
run: |
cat > Dockerrun.aws.json << EOF
{
"AWSEBDockerrunVersion": "1",
"Image": {
"Name": "${REGISTRY_URL}/${ECR_REPOSITORY}:${VERSION}",
"Update": "true"
},
"Ports": [
{
"ContainerPort": 8080,
"HostPort": 80
}
],
"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 .ebextensions
- name: Deploy to Elastic Beanstalk
id: deploy
uses: einaregilsson/beanstalk-deploy@v22
with:
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
application_name: ${{ env.EB_APPLICATION_NAME }}
environment_name: ${{ env.EB_ENVIRONMENT_NAME }}
version_label: ${{ steps.determine-version.outputs.VERSION }}-${{ github.sha }}
region: ${{ env.AWS_REGION }}
deployment_package: deploy.zip
use_existing_version_if_available: false
- name: Send Deployment Success Webhook
if: success()
env:
VERSION: ${{ steps.determine-version.outputs.VERSION }}
WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
curl -H "Content-Type: application/json" -X POST -d '{
"embeds": [{
"title": "배포 성공",
"color": 65280,
"fields": [
{
"name": "버전",
"value": "'"${VERSION}"'",
"inline": true
},
{
"name": "환경",
"value": "'"${EB_ENVIRONMENT_NAME}"'",
"inline": true
},
{
"name": "배포자",
"value": "'"${GITHUB_ACTOR}"'",
"inline": true
}
],
"url": "'"${WORKFLOW_URL}"'"
}]
}' ${{ env.DISCORD_WEBHOOK }}
- name: Send Deployment Failure Webhook
if: failure()
env:
VERSION: ${{ steps.determine-version.outputs.VERSION }}
WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
curl -H "Content-Type: application/json" -X POST -d '{
"embeds": [{
"title": "배포 실패",
"color": 16711680,
"fields": [
{
"name": "버전",
"value": "'"${VERSION}"'",
"inline": true
},
{
"name": "환경",
"value": "'"${EB_ENVIRONMENT_NAME}"'",
"inline": true
},
{
"name": "배포자",
"value": "'"${GITHUB_ACTOR}"'",
"inline": true
}
],
"url": "'"${WORKFLOW_URL}"'"
}]
}' ${{ env.DISCORD_WEBHOOK }}