Deploy API to Amazon ECS/ECR #4
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 API to Amazon ECS/ECR | |
on: | |
workflow_dispatch: | |
release: | |
types: [published] | |
env: | |
AWS_REGION: us-west-2 | |
ECR_API_REPOSITORY: pv-validation-hub-api | |
ECS_API_SERVICE: ecs-api-service | |
ECS_API_CLUSTER: pv-validation-hub-api-cluster | |
API_CONTAINER_NAME: pv-validation-hub-api-task | |
DJANGOSK: ${{ secrets.DJANGOSK }} | |
ADU: ${{ secrets.ADU }} | |
ADP: ${{ secrets.ADP }} | |
ADE: ${{ secrets.ADE }} | |
jobs: | |
deploy_api: | |
name: Deploy API | |
runs-on: ubuntu-latest | |
environment: production | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} | |
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@v1 | |
- name: Build, tag, and push image to Amazon ECR | |
id: build-image-api | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
ECR_REPOSITORY: ${{ env.ECR_API_REPOSITORY }} | |
IMAGE_TAG: latest | |
run: | | |
cd valhub | |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \ | |
--build-arg djangosk=${{ env.DJANGOSK }} \ | |
--build-arg admin_username=${{ env.ADU }} \ | |
--build-arg admin_password=${{ env.ADP }} \ | |
--build-arg admin_email=${{ env.ADE }} \ | |
--build-arg region=${{ env.AWS_REGION }} \ | |
--secret id=ak,env=${{ secrets.AWS_ACCESS_KEY }} \ | |
--secret id=sak,env=${{ secrets.AWS_SECRET_ACCESS_KEY }} \ | |
-f Dockerfile.prod . | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
echo "{image}={$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG}" >> $GITHUB_OUTPUT | |
- 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: ${{ env.API_CONTAINER_NAME }} | |
image: ${{ steps.build-image-api.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: ${{ env.ECS_API_SERVICE }} | |
cluster: ${{ env.ECS_API_CLUSTER }} | |
wait-for-service-stability: true | |