🚀 Deploy Workflow #32
Workflow file for this run
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 Workflow | |
on: | |
release: { types: [published] } | |
workflow_dispatch: | |
# These permissions are needed to interact with GitHub's OIDC Token endpoint. | |
permissions: { id-token: write, contents: read } | |
jobs: | |
deploy: | |
name: Deploy to ECS | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- uses: docker/setup-buildx-action@v3 | |
- uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: ${{ secrets.ECR_OIDC_GITHUB_ROLE_ARN }} | |
aws-region: ${{ secrets.ECR_REPO_REGION }} | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v2 | |
with: | |
mask-password: "true" | |
# Note: The above makes "outputs.registry" unavailable in any subsequent jobs. | |
- name: Build and Push Image+Tags | |
# IMAGE TAGS: | |
# 1. commit sha (always added) | |
# 2. release tag (if release/pre-release) | |
# 3. env tag (if release: "prod", anything else: "staging") | |
# 4. relative pointer (if release: "latest", anything else: "next") | |
run: | | |
IMAGE_TAGS=( "${{ github.sha }}" ) | |
if [ "${{ github.event_name }}" == 'release' ]; then | |
IMAGE_TAGS+=( "${{ github.event.release.tag_name }}" ) | |
fi | |
if [[ "${{ github.event_name }}" == 'release' && "${{ github.event.release.prerelease }}" == 'false' ]]; then | |
IMAGE_TAGS+=( prod latest ) | |
else | |
IMAGE_TAGS+=( staging next ) | |
fi | |
IMAGE_REPO="${{ steps.login-ecr.outputs.registry }}/${{ secrets.ECR_PRIVATE_REPO }}" | |
IMAGE_TAGS=("${IMAGE_TAGS[@]/#/$IMAGE_REPO:}") | |
docker build ${IMAGE_TAGS[@]/#/--tag } . | |
for tag in "${IMAGE_TAGS[@]}"; do docker push "$tag"; done | |
- uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
role-to-assume: ${{ secrets.ECS_OIDC_GITHUB_ROLE_ARN }} | |
aws-region: ${{ secrets.ECS_CLUSTER_REGION }} | |
- name: Update ECS Task Definition & Service | |
run: | | |
if [[ "${{ github.event_name }}" == 'release' && "${{ github.event.release.prerelease }}" == 'false' ]]; then | |
TASK_DEF_NAME=${{ secrets.ECS_API_TASK_DEF_PROD }} | |
SERVICE_NAME=${{ secrets.ECS_API_SERVICE_NAME_PROD }} | |
CLUSTER_NAME=${{ secrets.ECS_CLUSTER_NAME_PROD }} | |
else | |
TASK_DEF_NAME=${{ secrets.ECS_API_TASK_DEF_STAGING }} | |
SERVICE_NAME=${{ secrets.ECS_API_SERVICE_NAME_STAGING }} | |
CLUSTER_NAME=${{ secrets.ECS_CLUSTER_NAME_STAGING }} | |
fi | |
IMAGE_REPO="${{ steps.login-ecr.outputs.registry }}/${{ secrets.ECR_PRIVATE_REPO }}" | |
UPDATED_TASK_DEF_JSON=$( | |
aws ecs describe-task-definition \ | |
--task-definition $TASK_DEF_NAME \ | |
--output json | \ | |
jq --arg NEW_IMAGE "$IMAGE_REPO:${{ github.sha }}" \ | |
'.taskDefinition | | |
.containerDefinitions[0].image = $NEW_IMAGE | | |
del(.taskDefinitionArn) | | |
del(.revision) | | |
del(.status) | | |
del(.requiresAttributes) | | |
del(.compatibilities) | | |
del(.registeredAt) | | |
del(.registeredBy)' | |
) | |
aws ecs register-task-definition \ | |
--cli-input-json "$UPDATED_TASK_DEF_JSON" \ | |
1>/dev/null | |
aws ecs update-service \ | |
--cluster $CLUSTER_NAME \ | |
--service $SERVICE_NAME \ | |
--task-definition $TASK_DEF_NAME \ | |
--force-new-deployment \ | |
1>/dev/null |