Merge pull request #31 from GovTechSG/fix/og-tags #13
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: Deployment | |
on: | |
push: | |
branches: | |
- main | |
- staging | |
paths: | |
- "packages/web/**" | |
- "yarn.lock" | |
- ".github/workflows/web-*.yml" | |
env: | |
AWS_REGION: ${{ vars.AWS_REGION }} | |
ENVIRONMENT: ${{ (github.ref == 'refs/heads/staging') && 'staging' || (github.ref == 'refs/heads/main') && 'production' || 'unknown' }} | |
jobs: | |
check-env: | |
name: Set Deployment Environment | |
runs-on: ubuntu-latest | |
outputs: | |
environment: ${{ steps.set-env.outputs.ENVIRONMENT }} | |
steps: | |
- name: Set Environment Output | |
id: set-env | |
run: | | |
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then | |
echo "ENVIRONMENT=production" >> $GITHUB_OUTPUT | |
elif [[ "${{ github.ref }}" == "refs/heads/staging" ]]; then | |
echo "ENVIRONMENT=staging" >> $GITHUB_OUTPUT | |
else | |
echo "ENVIRONMENT=unknown" >> $GITHUB_OUTPUT | |
fi | |
echo "Deployment environment: $ENVIRONMENT" | |
deploy: | |
name: Deploy Website Service (${{ needs.check-env.outputs.environment }}) | |
runs-on: ubuntu-latest | |
environment: ${{ needs.check-env.outputs.environment }} | |
needs: check-env | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- 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 }} | |
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} | |
role-duration-seconds: 1200 | |
role-session-name: AssumeDeploymentSession | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
with: | |
mask-password: true | |
- name: Build, tag, and push image to Amazon ECR | |
id: build-image | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
ECR_REPOSITORY: ${{ vars.WEB_ECR_REPOSITORY }} | |
IMAGE_TAG: ${{ github.sha }} | |
run: | | |
docker build \ | |
--build-arg BASE_URL=${{ vars.WEB_BASE_URL }} \ | |
--build-arg UX_FEATURE_DECK_URL=${{ vars.WEB_UX_FEATURE_DECK_URL }} \ | |
--build-arg GITHUB_URL=${{ vars.WEB_GITHUB_URL }} \ | |
--build-arg GA_MEASUREMENT_ID=${{ vars.WEB_GA_MEASUREMENT_ID }} \ | |
-f packages/web/Dockerfile -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT | |
- name: Get Task Definition | |
run: | | |
aws ecs describe-task-definition \ | |
--task-definition ${{ vars.WEB_TASK_DEF_NAME }} \ | |
--query taskDefinition \ | |
--output json > task-definition.json | |
- name: Fill in the new image ID in the Amazon ECS task definition | |
id: task-def | |
uses: aws-actions/amazon-ecs-render-task-definition@v1 | |
with: | |
task-definition: task-definition.json | |
container-name: ${{ vars.WEB_CONTAINER_NAME }} | |
image: ${{ steps.build-image.outputs.image }} | |
- name: Deploy Amazon ECS task definition | |
uses: aws-actions/amazon-ecs-deploy-task-definition@v1 | |
with: | |
task-definition: ${{ steps.task-def.outputs.task-definition }} | |
service: ${{ vars.WEB_SERVICE_NAME }} | |
cluster: ${{ vars.CLUSTER_NAME }} | |
wait-for-service-stability: true |