Skip to content

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

Add production API deployment action (#840)

Madison Swain-Bowdena81042b92179
diff --git a/.github/actions/production-api-deploy/action.yml b/.github/actions/production-api-deploy/action.yml
new file mode 100644
index 000000000..3bf32c4a7
--- /dev/null
+++ b/.github/actions/production-api-deploy/action.yml
@@ -0,0 +1,221 @@
+### !!!
+# 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/production-api-deploy
+description: Update the task definition for a production-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 production-api",
+ "blocks": [
+ {
+ "type": "section",
+ "text": {
+ "type": "mrkdwn",
+ "text": ":spinning-cd: A deployment of production-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: Validate `tag` input for Nginx
+ uses: actions/github-script@v6
+ with:
+ script: |
+ let exists = undefined,
+ page = 0
+ while (!exists) {
+ page += 1
+ const { data: versions } =
+ await github.rest.packages.getAllPackageVersionsForPackageOwnedByOrg({
+ package_type: 'container',
+ package_name: 'openverse-api_nginx',
+ org: 'WordPress',
+ page,
+ // max of `per_page`
+ per_page: 100,
+ })
+ if (!versions.length) {
+ break
+ }
+ exists = versions.some((v) => v.metadata.container.tags.includes('${{ inputs.tag }}'))
+ }
+ if (!exists) {
+ throw new Error(
+ `'${{ inputs.tag }}' does not appear to be a valid tag for the ghcr.io/wordpress/openverse-api_nginx image.`
+ )
+ }
+
+ - name: Validate `tag` input for Django
+ uses: actions/github-script@v6
+ with:
+ script: |
+ let exists = undefined,
+ page = 0
+ while (!exists) {
+ page += 1
+ const { data: versions } =
+ await github.rest.packages.getAllPackageVersionsForPackageOwnedByOrg({
+ package_type: 'container',
+ package_name: 'openverse-api',
+ org: 'WordPress',
+ page,
+ // max of `per_page`
+ per_page: 100,
+ })
+ if (!versions.length) {
+ break
+ }
+ exists = versions.some((v) => v.metadata.container.tags.includes('${{ inputs.tag }}'))
+ }
+ if (!exists) {
+ throw new Error(
+ `'${{ inputs.tag }}' does not appear to be a valid tag for the ghcr.io/wordpress/openverse-api image.`
+ )
+ }
+
+ - 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 production-api-template \
+ --query taskDefinition > task-definition.json
+
+ # Remove template from the family name (the render action does not support this)
+ sed -i 's/"family": "production-api-template"/"family": "production-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: production-api
+ cluster: production-default
+ wait-for-service-stability: true
+ wait-for-minutes: 10
+
+ - name: Notify deployment finished
+ uses: slackapi/[email protected]
+ with:
+ payload: |
+ {
+ "text": "Deployment of production-api successful",
+ "blocks": [
+ {
+ "type": "section",
+ "text": {
+ "type": "mrkdwn",
+ "text": ":tadaco: The deployment of production-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 production-api failed!",
+ "blocks": [
+ {
+ "type": "section",
+ "text": {
+ "type": "mrkdwn",
+ "text": ":alert: The deployment of production-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