Skip to content

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

Deploy production frontend on release & correctly time staging deployment (#1556)

sarayourfriendb4e11b9a9ea2
diff --git a/.github/actions/production-frontend-deploy/action.yml b/.github/actions/production-frontend-deploy/action.yml
new file mode 100644
index 000000000..a14cc9cd0
--- /dev/null
+++ b/.github/actions/production-frontend-deploy/action.yml
@@ -0,0 +1,136 @@
+### !!!
+# 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-frontend-nuxt-deploy
+description: Update the task definition for a production-frontend-nuxt 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: false
+
+runs:
+ using: "composite"
+
+ steps:
+ - name: Notify deployment start
+ uses: slackapi/[email protected]
+ with:
+ payload: |
+ {
+ "text": "Starting deployment of production-frontend",
+ "blocks": [
+ {
+ "type": "section",
+ "text": {
+ "type": "mrkdwn",
+ "text": ":spinning-cd: A deployment of production-frontend 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
+ 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
+ shell: bash
+ run: |
+ aws ecs describe-task-definition \
+ --task-definition production-frontend-template \
+ --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: nuxt
+ image: ghcr.io/wordpress/openverse-frontend:${{ inputs.tag }}
+
+ - 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: production-frontend
+ cluster: production-default
+ wait-for-service-stability: true
+
+ - name: Notify deployment finished
+ uses: slackapi/[email protected]
+ with:
+ payload: |
+ {
+ "text": "Deployment of production-frontend successful",
+ "blocks": [
+ {
+ "type": "section",
+ "text": {
+ "type": "mrkdwn",
+ "text": ":tadaco: The deployment of production-frontend 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-frontend failed!",
+ "blocks": [
+ {
+ "type": "section",
+ "text": {
+ "type": "mrkdwn",
+ "text": ":alert: The deployment of production-frontend 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.yml b/.github/workflows/ci.yml
index 6a708c472..a3a7542d7 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -6,6 +6,13 @@ on:
- main
pull_request:
+# Cancels all previous workflow runs for pull requests that have not completed.
+concurrency:
+ # The concurrency group contains the workflow name and the branch name for pull requests
+ # or the commit hash for any other events.
+ group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
+ cancel-in-progress: true
+
jobs:
types:
name: Check types
diff --git a/.github/workflows/ghcr.yml b/.github/workflows/ghcr.yml
index c8cd5ffdf..017fa0b90 100644
--- a/.github/workflows/ghcr.yml
+++ b/.github/workflows/ghcr.yml
@@ -1,4 +1,4 @@
-name: GitHub Container Registry
+name: GitHub Container Registry & Deployment
on:
push:
@@ -7,15 +7,33 @@ on:
pull_request:
branches:
- main
+ release:
+ types: [published]
+
+# Cancels all previous workflow runs for pull requests that have not completed.
+concurrency:
+ # The concurrency group contains the workflow name and the branch name for pull requests
+ # or the commit hash for any other events.
+ group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
+ cancel-in-progress: true
jobs:
push-to-ghcr:
name: Build and push image for commit
runs-on: ubuntu-latest
- if: github.event_name == 'push' || github.event.pull_request.head.repo.owner.login == 'WordPress'
+ # prevent running on fork PRs
+ if: github.event_name == 'push' || github.event_name == 'release' || github.event.pull_request.head.repo.owner.login == 'WordPress'
steps:
- - uses: actions/checkout@v3
+ - name: Checkout branch or push to main
+ uses: actions/checkout@v3
+ if: github.event_name != 'release'
+
+ - name: Checkout release
+ uses: actions/checkout@v3
+ if: github.event_name == 'release'
+ with:
+ ref: ${{ github.event.release.tag_name }}
- uses: docker/setup-buildx-action@v2
with:
@@ -34,7 +52,22 @@ jobs:
tags: |
type=sha
type=ref,event=tag
- ${{ github.event_name == 'push' && 'type=raw,value=main' || '' }}
+ type=raw,value=main,enabled=${{ github.event_name == 'push' }}
+
+ - name: Get relevant tag for internal image version
+ id: get-version
+ shell: python
+ run: |
+ import json
+ metadata = json.loads('${{ steps.metadata.outputs.json }}')
+ version = metadata["labels"]["org.opencontainers.image.version"]
+ if version == "main":
+ # get first version that isn't main
+ for tag in metadata["tags"]:
+ if ":main" not in tag:
+ version = tag.split(":")[1]
+
+ print(f"::set-output name=version::{version}")
- uses: docker/build-push-action@v3
with:
@@ -42,3 +75,39 @@ jobs:
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.metadata.outputs.labels }}
push: true
+ build-args: |
+ RELEASE=${{ steps.get-version.outputs.version }}
+
+ deploy-staging:
+ name: Deploy staging frontend
+ runs-on: ubuntu-latest
+ if: github.event_name == 'push'
+ needs: push-to-ghcr
+
+ steps:
+ - uses: actions/checkout@v3
+
+ - uses: ./.github/actions/staging-frontend-deploy
+ with:
+ tag: "main"
+ aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
+ aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
+ slack-webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
+
+ deploy-production:
+ name: Deploy production frontend
+ runs-on: ubuntu-latest
+ if: github.event_name == 'release'
+ needs: push-to-ghcr
+
+ steps:
+ - uses: actions/checkout@v3
+ with:
+ ref: main
+
+ - uses: ./.github/actions/production-frontend-deploy
+ with:
+ tag: ${{ github.event.release.tag_name }}
+ aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
+ aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
+ slack-webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
diff --git a/.github/workflows/push_production.yaml b/.github/workflows/push_production.yaml
deleted file mode 100644
index bd1429747..000000000
--- a/.github/workflows/push_production.yaml
+++ /dev/null
@@ -1,39 +0,0 @@
-# this build is triggered when a new pre-release has been created
-# it creates a new docker build image based on the tag associated
-
-name: openverse/deploy/production
-
-on:
- release:
- types:
- - 'released'
- workflow_dispatch:
- inputs:
- sha:
- description: Git commit from which to build the new stable image.
- required: true
-
-jobs:
- push:
- name: push
- runs-on: ubuntu-latest
- environment:
- name: production
- url: https://search-prod.openverse.engineering
-
- steps:
- # download the source code into the runner
- - name: checkout
- uses: actions/checkout@v3
- with:
- ref: ${{ github.event.inputs.sha || 'main' }}
-
- # build a new docker image and push it into the repository
- - name: docker build
- uses: ./.github/actions/build
- with:
- is_production: true
- AWS_REGION: ${{ secrets.AWS_REGION }}
- AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
- AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
- AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
diff --git a/.github/workflows/push_staging.yaml b/.github/workflows/push_staging.yaml
deleted file mode 100644
index 5a205af8c..000000000
--- a/.github/workflows/push_staging.yaml
+++ /dev/null
@@ -1,33 +0,0 @@
-# this build is triggered when a new pull request has been merged
-# into the main branch
-
-name: openverse/deploy/staging
-
-on:
- push:
- branches:
- - main
- - ci/* # allow any ci/* branch the ability to deploy to staging without the need to merge the PR
-
-jobs:
- push:
- name: push
- runs-on: ubuntu-latest
- environment:
- name: staging
- url: https://search-staging.openverse.engineering
-
- steps:
- # download the source code into the runner
- - name: checkout
- uses: actions/checkout@v3
-
- # build a new docker image and push it into the repository
- - name: docker build
- uses: ./.github/actions/build
- with:
- is_production: false
- AWS_REGION: ${{ secrets.AWS_REGION }}
- AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
- AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
- AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
diff --git a/.github/workflows/rollback.yml b/.github/workflows/rollback.yml
new file mode 100644
index 000000000..f06095e7c
--- /dev/null
+++ b/.github/workflows/rollback.yml
@@ -0,0 +1,51 @@
+name: Rollback Frontend
+
+on:
+ workflow_dispatch:
+ inputs:
+ environment:
+ required: true
+ description: The environment to roll back. `staging` or `production`.
+ tag:
+ required: true
+ description: The GHCR image tag to which the environment should be rolled back
+
+jobs:
+ rollback:
+ name: Perform Rollback
+ runs-on: ubuntu-latest
+
+ steps:
+ - uses: actions/checkout@v3
+
+ - name: Validate `tag` input
+ uses: actions/github-script@v6
+ with:
+ script: |
+ const { data: versions } = await github.rest.packages.getAllPackageVersionsForPackageOwnedByOrg({
+ package_type: 'container',
+ package_name: 'openverse-frontend',
+ org: 'WordPress',
+ })
+
+ const exists = versions.some(v => v.name === "${{ inputs.tag }}")
+
+ if (!exists) {
+ throw new Error("`${{ inputs.tag }}` does not appear to be a valid tag for the ghcr.io/wordpress/openverse-frontend image.")
+ }
+
+ - uses: ./github/actions/production-frontend-deploy
+ if: inputs.environment == 'production'
+ with:
+ ref: ${{ inputs.tag }}
+ aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
+ aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
+ slack-webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
+
+ - uses: ./github/actions/staging-frontend-deploy
+ if: inputs.environment == 'staging'
+ with:
+ ref: ${{ inputs.tag }}
+ aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
+ aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
+ slack-webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
diff --git a/.github/workflows/staging_frontend_deploy.yml b/.github/workflows/staging_frontend_deploy.yml
deleted file mode 100644
index 99c2fa32d..000000000
--- a/.github/workflows/staging_frontend_deploy.yml
+++ /dev/null
@@ -1,21 +0,0 @@
-name: Redeploy staging frontend
-
-on:
- push:
- branches:
- - main
-
-jobs:
- update-task-definition-and-deploy:
- name: Update task definition and deploy
- runs-on: ubuntu-latest
-
- steps:
- - uses: actions/checkout@v3
-
- - uses: ./.github/actions/staging-frontend-deploy
- with:
- tag: "main"
- aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
- aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
- slack-webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
diff --git a/Dockerfile b/Dockerfile
index 2e368f773..10d6402c9 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -6,13 +6,13 @@ USER node
WORKDIR /home/node/app
-COPY pnpm-lock.yaml .
+COPY --chown=node:node pnpm-lock.yaml .
# install dependencies including local development tools
RUN pnpm fetch
# copy the rest of the content
-COPY --chmod=777 . /home/node/app
+COPY --chown=node:node . /home/node/app
RUN pnpm install -r --offline
@@ -22,9 +22,11 @@ ENV NUXT_TELEMETRY_DISABLED=1
ENV NODE_ENV=production
ARG API_URL
+ARG RELEASE
+
+RUN echo "{\"release\":\"${RELEASE}\"}" > /home/node/app/src/static/version.json
RUN pnpm i18n
-# build the application and generate a distribution package
RUN pnpm build:only
COPY ecosystem.config.js /home/node/app/ecosystem.config.js