Continuous Deployment to Dev #11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Continuous Deployment to Dev | |
on: | |
repository_dispatch: | |
types: [trigger-cd] | |
workflow_dispatch: | |
inputs: | |
version: | |
description: 'Version to deploy (e.g., v1.2.3)' | |
required: true | |
type: string | |
env: | |
AWS_REGION: ap-northeast-2 | |
ECR_REPOSITORY: threedays-app | |
EB_APPLICATION_NAME: threedays-dev | |
EB_ENVIRONMENT_NAME: threedays-dev-env | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
environment: dev | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- 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: Determine version to deploy | |
id: determine-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 | |
else | |
echo "Error: Unexpected event type" | |
exit 1 | |
fi | |
- name: Check if image exists in ECR | |
id: check-image | |
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" | |
exit 1 | |
fi | |
echo "Image found in 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 | |
} | |
], | |
"Environment": | |
EOF | |
# νκ²½ λ³μλ€μ JSON λ°°μ΄λ‘ μμ± | |
echo '{}' | jq --argjson secrets "$SECRETS_CONTEXT" \ | |
--argjson vars "$VARS_CONTEXT" \ | |
'$secrets + $vars | to_entries | map(select(.key | test("^AWS_") | not) | {Name: .key, Value: .value})' \ | |
>> Dockerrun.aws.json | |
# JSON λ§λ¬΄λ¦¬ | |
echo '}' >> Dockerrun.aws.json | |
- name: Create deployment package | |
run: | | |
zip -r deploy.zip Dockerrun.aws.json | |
- name: Deploy to Elastic Beanstalk | |
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: true | |
- name: Deployment result | |
run: echo "Deployed version ${{ steps.determine-version.outputs.VERSION }} to Elastic Beanstalk environment ${{ env.EB_ENVIRONMENT_NAME }}" |