Decouple ecs network (#12) #186
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: Deploy | |
on: | |
push: | |
branches: | |
- main | |
permissions: | |
id-token: write | |
contents: read | |
jobs: | |
code: | |
uses: ./.github/workflows/build.yml | |
with: | |
aws_account_id: ${{ vars.AWS_ACCOUNT_ID }} | |
aws_region: ${{ vars.AWS_REGION }} | |
aws_role: ${{ vars.AWS_ROLE }} | |
check: | |
needs: code | |
runs-on: ubuntu-latest | |
outputs: | |
deploy: ${{ steps.set-deploy-output.outputs.deploy }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Check ECS Service | |
id: check-ecs-service | |
uses: ./.github/actions/check_svc | |
with: | |
aws_region: ${{ vars.aws_region }} | |
aws_role: ${{ vars.aws_account_id }}:role/${{ vars.aws_role }} | |
tfvars_file: "tf/variables.tfvars" | |
- name: Set deploy condition | |
id: set-deploy-output | |
shell: bash | |
run: | | |
if [[ "${{ env.SERVICE_EXISTS }}" == "true" && "${{ env.TASK_DEFINITION_ARN }}" != "${{ needs.code.outputs.task_definition_arn }}" ]]; then | |
echo "deploy=true" >> $GITHUB_OUTPUT | |
else | |
echo "deploy=false" >> $GITHUB_OUTPUT | |
fi | |
deploy: | |
needs: | |
- code | |
- check | |
runs-on: ubuntu-latest | |
if: ${{ needs.check.outputs.deploy == 'true' }} | |
env: | |
TF_VAR_FILE: tf/variables.tfvars | |
APP_SPEC_FILE: appspec.yaml | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-region: ${{ vars.AWS_REGION }} | |
role-to-assume: arn:aws:iam::${{ vars.AWS_ACCOUNT_ID }}:role/${{ vars.AWS_ROLE }} | |
role-session-name: GitHubActions | |
- name: Read code deploy app vars from tfvars file | |
id: read-vars | |
shell: bash | |
run: | | |
CODE_DEPLOY_APP_NAME=$(grep 'codedeploy_app_name' "${{ env.TF_VAR_FILE }}" | sed 's/.*= "\(.*\)"/\1/') | |
echo "Extracted codedeploy_app_name: $CODE_DEPLOY_APP_NAME" | |
echo "CODE_DEPLOY_APP_NAME=$CODE_DEPLOY_APP_NAME" >> $GITHUB_OUTPUT | |
CODE_DEPLOY_GROUP_NAME=$(grep 'codedeploy_group_name' "${{ env.TF_VAR_FILE }}" | sed 's/.*= "\(.*\)"/\1/') | |
echo "Extracted codedeploy_group_name: $CODE_DEPLOY_APP_NAME" | |
echo "CODE_DEPLOY_GROUP_NAME=$CODE_DEPLOY_GROUP_NAME" >> $GITHUB_OUTPUT | |
CONTAINER_PORT=$(grep 'container_port' "${{ env.TF_VAR_FILE }}" | sed 's/.*= //') | |
echo "Extracted container_port: $CONTAINER_PORT" | |
echo "CONTAINER_PORT=$CONTAINER_PORT" >> $GITHUB_OUTPUT | |
PROJECT_NAME=$(grep 'project_name' "${{ env.TF_VAR_FILE }}" | sed 's/.*= "\(.*\)"/\1/') | |
echo "Extracted project_name: $PROJECT_NAME" | |
echo "PROJECT_NAME=$PROJECT_NAME" >> $GITHUB_OUTPUT | |
DEPLOYMENT_CONFIG=$(grep 'deployment_config' "${{ env.TF_VAR_FILE }}" | sed 's/.*= "\(.*\)"/\1/') | |
echo "Extracted deployment_confi: $DEPLOYMENT_CONFIG" | |
echo "DEPLOYMENT_CONFIG=$DEPLOYMENT_CONFIG" >> $GITHUB_OUTPUT | |
- name: Prepare AppSpec File | |
run: | | |
sed -i 's|{{TASK_VERSION}}|${{ needs.code.outputs.task_definition_revision }}|g' ${{ env.APP_SPEC_FILE }} | |
sed -i 's|{{TASK_DEFINITION_ARN}}|${{ needs.code.outputs.task_definition_arn }}|g' ${{ env.APP_SPEC_FILE }} | |
sed -i 's|{{CONTAINER_NAME}}|${{ steps.read-vars.outputs.project_name }}|g' ${{ env.APP_SPEC_FILE }} | |
sed -i 's|{{CONTAINER_PORT}}|${{ steps.read-vars.outputs.container_port }}|g' ${{ env.APP_SPEC_FILE }} | |
cat ${{ env.APP_SPEC_FILE }} | |
- name: Upload yaml to s3 | |
run: aws s3 cp ${{ env.APP_SPEC_FILE }} s3://${{ needs.code.outputs.app_specs_bucket}}/${{ needs.code.outputs.task_definition_revision }}-${{ env.APP_SPEC_FILE }} | |
- name: Create CodeDeploy Deployment | |
id: create-deployment | |
env: | |
APP_NAME: ${{ steps.read-vars.outputs.CODE_DEPLOY_APP_NAME }} | |
GROUP_NAME: ${{ steps.read-vars.outputs.CODE_DEPLOY_GROUP_NAME }} | |
DEPLOYMENT_CONFIG: ${{ steps.read-vars.outputs.DEPLOYMENT_CONFIG }} | |
run: | | |
echo "Creating CodeDeploy deployment" | |
DEPLOYMENT_OUTPUT=$(aws deploy create-deployment \ | |
--application-name "$APP_NAME" \ | |
--deployment-group-name "$GROUP_NAME" \ | |
--deployment-config-name $DEPLOYMENT_CONFIG \ | |
--revision "{\"revisionType\":\"S3\",\"s3Location\":{\"bucket\":\"${{ needs.code.outputs.app_specs_bucket}}\",\"key\":\"${{ needs.code.outputs.task_definition_revision }}-${{ env.APP_SPEC_FILE }}\",\"bundleType\":\"YAML\"}}" \ | |
--region ${{ vars.AWS_REGION }}) | |
DEPLOYMENT_ID=$(echo "$DEPLOYMENT_OUTPUT" | jq -r '.deploymentId') | |
echo "DEPLOYMENT_ID=$DEPLOYMENT_ID" >> $GITHUB_OUTPUT | |
- name: Monitor Deployment Status | |
env: | |
APP_NAME: ${{ steps.read-vars.outputs.CODE_DEPLOY_APP_NAME }} | |
GROUP_NAME: ${{ steps.read-vars.outputs.CODE_DEPLOY_GROUP_NAME }} | |
DEPLOYMENT_ID: ${{ steps.create-deployment.outputs.DEPLOYMENT_ID }} | |
run: | | |
echo "Monitoring CodeDeploy deployment status" | |
echo "Deployment ID: $DEPLOYMENT_ID" | |
# Loop to check the deployment status | |
while true; do | |
DEPLOYMENT_INFO=$(aws deploy get-deployment --deployment-id $DEPLOYMENT_ID --query 'deploymentInfo' --output json) | |
STATUS=$(echo $DEPLOYMENT_INFO | jq -r '.status') | |
echo "Deployment status: $STATUS" | |
if [[ "$STATUS" == "Succeeded" ]]; then | |
echo "Deployment succeeded!" | |
break | |
elif [[ "$STATUS" == "Failed" || "$STATUS" == "Stopped" ]]; then | |
ERROR_CODE=$(echo $DEPLOYMENT_INFO | jq -r '.errorInformation.code') | |
ERROR_MESSAGE=$(echo $DEPLOYMENT_INFO | jq -r '.errorInformation.message') | |
echo $ERROR_CODE | |
echo $ERROR_MESSAGE | |
echo "Deployment failed!" | |
exit 1 | |
else | |
echo "Deployment is in progress..." | |
sleep 10 | |
fi | |
done |