Backend CD #81
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: Backend CD | |
on: | |
workflow_run: | |
workflows: [ "Backend CI" ] | |
branches: | |
- main | |
- staging | |
- production | |
- perf | |
types: | |
- completed | |
jobs: | |
build_and_push_docker_image: | |
name: Build and push docker image to AWS ECR | |
runs-on: ubuntu-latest | |
if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
environment: ${{ github.event.workflow_run.head_branch == 'main' && 'develop' || github.event.workflow_run.head_branch }} | |
concurrency: | |
group: ${{ github.event.workflow_run.head_branch }} | |
cancel-in-progress: true | |
permissions: | |
id-token: write | |
contents: read | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
with: | |
ref: ${{ github.event.workflow_run.head_branch }} | |
- name: Download jar file | |
uses: actions/download-artifact@v4 | |
with: | |
name: marketplace-api.jar | |
run-id: ${{ github.event.workflow_run.id }} | |
path: bootstrap/target/marketplace-api.jar | |
github-token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-region: ${{ vars.AWS_REGION }} | |
role-to-assume: ${{ vars.AWS_ROLE }} | |
- name: Retrieve parameters | |
env: | |
REPO_NAME: ${{ github.event.workflow_run.head_repository.name }} | |
run: | | |
echo SHA_SHORT=$(git rev-parse --short HEAD) >> "$GITHUB_ENV" | |
echo ECR_REPOSITORY_URI=$(aws ecr describe-repositories --query "repositories[?contains(@.repositoryName, 'marketplacerepository') == \`true\`].repositoryUri" --output text) >> "$GITHUB_ENV" | |
echo CLUSTER_ARN=$(aws ecs list-clusters --query 'clusterArns[0]' --output text) >> "$GITHUB_ENV" | |
echo SERVICES=$(aws resourcegroupstaggingapi get-resources --tag-filters Key=repo,Values="$REPO_NAME" --resource-type-filters ecs:service --query 'ResourceTagMappingList[*].ResourceARN' --output text ) >> "$GITHUB_ENV" | |
- name: Login to ECR | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.ECR_REPOSITORY_URI }} | |
- name: Build, tag, and push image to Amazon ECR | |
uses: docker/build-push-action@v6 | |
with: | |
push: true | |
context: . | |
tags: | | |
${{ env.ECR_REPOSITORY_URI }}:latest | |
${{ env.ECR_REPOSITORY_URI }}:${{ env.SHA_SHORT }} | |
- name: Deploy ECR services | |
run: | | |
for service in ${{ env.SERVICES }}; do | |
aws ecs update-service --cluster ${{ env.CLUSTER_ARN }} --service $service --force-new-deployment | |
done |