diff --git a/.github/workflows/deploy_ecs_staging_stationapi.yml b/.github/workflows/deploy_ecs_staging_stationapi.yml new file mode 100644 index 00000000..a2a6c416 --- /dev/null +++ b/.github/workflows/deploy_ecs_staging_stationapi.yml @@ -0,0 +1,75 @@ +name: Deploy StationAPI to Staging Environment on Amazon ECS + +on: + workflow_dispatch: + push: + branches: + - master + paths: + - ".sqlx/**" + - "data/**" + - "scripts/**" + - "stationapi/**" + - "Cargo.lock" + - "Cargo.toml" + +env: + AWS_REGION: ${{ secrets.AWS_REGION }} + ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }} + ECS_SERVICE: ${{ secrets.ECS_SERVICE }} + ECS_CLUSTER: ${{ secrets.ECS_CLUSTER }} + ECS_TASK_DEFINITION: ${{ secrets.ECS_TASK_DEFINITION }} + CONTAINER_NAME: ${{ secrets.ECS_CONTAINER_NAME }} + +jobs: + deploy: + name: Deploy StationAPI Staging + runs-on: self-hosted + environment: staging + + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: recursive + + - 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 }} + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v2 + + - name: Build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ github.sha }} + run: | + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f ./docker/api/Dockerfile . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT + + - name: Download task definition + run: | + aws ecs describe-task-definition --task-definition ${{ env.ECS_TASK_DEFINITION }} --query taskDefinition > 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: ${{ env.CONTAINER_NAME }} + image: ${{ steps.build-image.outputs.image }} + + - name: Deploy Amazon ECS task definition + uses: aws-actions/amazon-ecs-deploy-task-definition@v2 + with: + task-definition: ${{ steps.task-def.outputs.task-definition }} + service: ${{ env.ECS_SERVICE }} + cluster: ${{ env.ECS_CLUSTER }} + wait-for-service-stability: true