Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add staging deployment action for the API (#1114)

Madison Swain-Bowden8e18e870a2c1
diff --git a/.github/actions/staging-deploy/action.yml b/.github/actions/staging-deploy/action.yml
new file mode 100644
index 000000000..877e32317
--- /dev/null
+++ b/.github/actions/staging-deploy/action.yml
@@ -0,0 +1,165 @@
+### !!!
+# This file is automatically generated using Terraform.
+# Do not update it manually. If changes need to be made,
+# please request help from a maintainer to generate the
+# updated version.
+### !!!
+
+name: openverse/staging-api-deploy
+description: Update the task definition for a staging-api deployment
+
+inputs:
+ tag:
+ required: true
+ description: Image tag to deploy.
+ aws-access-key-id:
+ required: true
+ aws-secret-access-key:
+ required: true
+ slack-webhook:
+ required: true
+ gh-slack-username-map:
+ required: true
+
+runs:
+ using: "composite"
+
+ steps:
+ - name: Set the Slack user
+ shell: python
+ env:
+ GH_SLACK_USERNAME_MAP: ${{ inputs.gh-slack-username-map }}
+ run: |
+ import json
+ import os
+ mapping = json.loads('${{ env.GH_SLACK_USERNAME_MAP }}')
+ github_user = "${{ github.triggering_actor }}"
+ slack_id = mapping[github_user]
+ with open(os.getenv('GITHUB_ENV'), "a") as env_file:
+ env_file.write(f"SLACK_USER_ID={slack_id}")
+
+ - name: Notify deployment start
+ uses: slackapi/[email protected]
+ if: success() || failure()
+ with:
+ payload: |
+ {
+ "text": "Starting deployment of staging-api",
+ "blocks": [
+ {
+ "type": "section",
+ "text": {
+ "type": "mrkdwn",
+ "text": ":spinning-cd: A deployment of staging-api triggered by <@${{ env.SLACK_USER_ID }}> is starting using the `${{ inputs.tag }}` tag."
+ }
+ },
+ {
+ "type": "section",
+ "text": {
+ "type": "mrkdwn",
+ "text": "<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|Click here to monitor the deployment>."
+ }
+ }
+ ]
+ }
+ env:
+ SLACK_WEBHOOK_URL: ${{ inputs.slack-webhook }}
+ SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
+
+ - name: Configure AWS credentials
+ uses: aws-actions/configure-aws-credentials@v1
+ if: success() || failure()
+ with:
+ aws-access-key-id: ${{ inputs.aws-access-key-id }}
+ aws-secret-access-key: ${{ inputs.aws-secret-access-key }}
+ aws-region: us-east-1
+
+ - name: Download task definition template and update family name
+ shell: bash
+ run: |
+ aws ecs describe-task-definition \
+ --task-definition staging-api-template \
+ --query taskDefinition > task-definition.json
+
+ # Remove template from the family name (the render action does not support this)
+ sed -i 's/"family": "staging-api-template"/"family": "staging-api"/g' task-definition.json
+
+ - name: "Fill in the new nginx image ID in the Amazon ECS task definition"
+ id: "task-def-nginx"
+ uses: "aws-actions/amazon-ecs-render-task-definition@v1"
+ with:
+ task-definition: task-definition.json
+ container-name: nginx
+ image: "ghcr.io/wordpress/openverse-api-nginx:${{ inputs.tag }}"
+
+ - name: "Fill in the new django image ID in the Amazon ECS task definition"
+ id: "task-def-django"
+ uses: "aws-actions/amazon-ecs-render-task-definition@v1"
+ with:
+ task-definition: ${{ steps.task-def-nginx.outputs.task-definition }}
+ container-name: django
+ image: "ghcr.io/wordpress/openverse-api:${{ inputs.tag }}"
+
+ - name: Deploy Amazon ECS task definition
+ uses: aws-actions/amazon-ecs-deploy-task-definition@v1
+ with:
+ task-definition: ${{ steps.task-def-django.outputs.task-definition }}
+ service: staging-api
+ cluster: staging-default
+ wait-for-service-stability: true
+ wait-for-minutes: 10
+
+ - name: Notify deployment finished
+ uses: slackapi/[email protected]
+ with:
+ payload: |
+ {
+ "text": "Deployment of staging-api successful",
+ "blocks": [
+ {
+ "type": "section",
+ "text": {
+ "type": "mrkdwn",
+ "text": ":tadaco: The deployment of staging-api triggered by <@${{ env.SLACK_USER_ID }}> using the `${{ inputs.tag }}` tag *succeeded*."
+ }
+ },
+ {
+ "type": "section",
+ "text": {
+ "type": "mrkdwn",
+ "text": "<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|Click here to review the completed deployment workflow>."
+ }
+ }
+ ]
+ }
+ env:
+ SLACK_WEBHOOK_URL: ${{ inputs.slack-webhook }}
+ SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
+
+ - name: Notify deployment failure
+ if: failure()
+ uses: slackapi/[email protected]
+ with:
+ payload: |
+ {
+ "text": "Deployment of staging-api failed!",
+ "blocks": [
+ {
+ "type": "section",
+ "text": {
+ "type": "mrkdwn",
+ "text": ":alert: The deployment of staging-api triggered by <@${{ env.SLACK_USER_ID }}> using the `${{ inputs.tag }}` tag *failed* :alert:"
+ }
+ },
+ {
+ "type": "section",
+ "text": {
+ "type": "mrkdwn",
+ "text": "<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|Click here to review the failed deployment workflow>."
+ }
+ }
+ ]
+ }
+ env:
+ SLACK_WEBHOOK_URL: ${{ inputs.slack-webhook }}
+ SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
diff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.yml
index 0f8bbf95d..acbc3189c 100644
--- a/.github/workflows/ci_cd.yml
+++ b/.github/workflows/ci_cd.yml
@@ -346,7 +346,8 @@ jobs:
push:
name: Publish Docker images
runs-on: ubuntu-latest
- if: github.event_name == 'release' && github.event.action == 'published'
+ # prevent running on fork PRs
+ if: (github.event_name == 'push' || github.event_name == 'release') && github.repository == 'WordPress/openverse-api'
needs:
- test-ing
- test-api
@@ -374,17 +375,40 @@ jobs:
name: ${{ matrix.image }}
path: /tmp
- - name: Load and tag image `${{ matrix.image }}`
+ - name: Load and tag image `${{ matrix.image }}` (latest & sha)
run: |
docker load --input /tmp/${{ matrix.image }}.tar
docker tag openverse-${{ matrix.image }} \
ghcr.io/wordpress/openverse-${{ matrix.image }}:latest
docker tag openverse-${{ matrix.image }} \
ghcr.io/wordpress/openverse-${{ matrix.image }}:${{ github.sha }}
+ docker push --all-tags ghcr.io/wordpress/openverse-${{ matrix.image }}
+
+ - name: Tag image `${{ matrix.image }}` (release)
+ if: github.event_name == 'release'
+ run: |
docker tag openverse-${{ matrix.image }} \
ghcr.io/wordpress/openverse-${{ matrix.image }}:${{ github.ref_name }}
docker push --all-tags ghcr.io/wordpress/openverse-${{ matrix.image }}
+ deploy-staging:
+ name: Deploy staging API
+ runs-on: ubuntu-latest
+ # Deploy a new version when `main` is pushed to in this repository
+ if: github.event_name == 'push' && github.repository == 'WordPress/openverse-api'
+ needs: push
+
+ steps:
+ - uses: actions/checkout@v3
+
+ - uses: ./.github/actions/staging-deploy
+ with:
+ tag: ${{ github.sha }}
+ aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
+ aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
+ slack-webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
+ gh-slack-username-map: ${{ secrets.GH_SLACK_USERNAME_MAP }}
+
publish-docs:
name: Publish developer docs
runs-on: ubuntu-latest