Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Add documentation for the CI + CD workflow (#1001)
Dhruv Bhanushalibe1ddd694b7a
diff --git a/.github/actions/get-changes/action.yml b/.github/actions/get-changes/action.ymlindex 9922ab97c..a694d948a 100644--- a/.github/actions/get-changes/action.yml+++ b/.github/actions/get-changes/action.yml@@ -5,24 +5,6 @@ outputs:changes:description: "JSON array of keys from `.github/filters.yml`"value: ${{ steps.paths-filter.outputs.changes }}- catalog:- description: "'true' if catalog changes are present"- value: ${{ steps.paths-filter.outputs.catalog }}- api:- description: "'true' if API changes are present"- value: ${{ steps.paths-filter.outputs.api }}- ingestion_server:- description: "'true' if ingestion server changes are present"- value: ${{ steps.paths-filter.outputs.ingestion_server }}- frontend:- description: "'true' if frontend changes are present"- value: ${{ steps.paths-filter.outputs.frontend }}- documentation:- description: "'true' if documentation changes are present"- value: ${{ steps.paths-filter.outputs.documentation }}- lint:- description: "'true' if linting setup changes are present"- value: ${{ steps.paths-filter.outputs.frontend }}runs:using: "composite"diff --git a/.github/actions/load-img/action.yml b/.github/actions/load-img/action.ymlindex 400bef3e8..a2f1fab8e 100644--- a/.github/actions/load-img/action.yml+++ b/.github/actions/load-img/action.yml@@ -2,21 +2,9 @@ name: openverse/load-imgdescription: Download and import Docker images from artifactsinputs:- setup_ingestion_server:- default: "true"- description: Whether to set up ingestion server image-- setup_api:- default: "true"- description: Whether to set up API image-- setup_catalog:- default: "true"- description: Whether to set up catalog image-- setup_upstream_db:- default: "true"- description: Whether to set up upstream DB image+ setup_images:+ default: '["upstream_db", "ingestion_server", "catalog", "api", "api_nginx"]'+ description: JSON-encoded list of image namesruns:using: "composite"@@ -24,13 +12,13 @@ runs:# Upstream DB- name: Download image `upstream_db`uses: actions/download-artifact@v3- if: inputs.setup_upstream_db == 'true'+ if: contains(fromJson(inputs.setup_images), 'upstream_db')with:name: upstream_dbpath: /tmp- name: Load image `upstream_db`- if: inputs.setup_upstream_db == 'true'+ if: contains(fromJson(inputs.setup_images), 'upstream_db')shell: bashrun: |docker load --input /tmp/upstream_db.tar@@ -38,13 +26,13 @@ runs:# Catalog- name: Download image `catalog`uses: actions/download-artifact@v3- if: inputs.setup_catalog == 'true'+ if: contains(fromJson(inputs.setup_images), 'catalog')with:name: catalogpath: /tmp- name: Load image `catalog`- if: inputs.setup_catalog == 'true'+ if: contains(fromJson(inputs.setup_images), 'catalog')shell: bashrun: |docker load --input /tmp/catalog.tar@@ -52,13 +40,13 @@ runs:# Ingestion server- name: Download image `ingestion_server`uses: actions/download-artifact@v3- if: inputs.setup_ingestion_server == 'true'+ if: contains(fromJson(inputs.setup_images), 'ingestion_server')with:name: ingestion_serverpath: /tmp- name: Load image `ingestion_server`- if: inputs.setup_ingestion_server == 'true'+ if: contains(fromJson(inputs.setup_images), 'ingestion_server')shell: bashrun: |docker load --input /tmp/ingestion_server.tar@@ -66,13 +54,26 @@ runs:# API- name: Download image `api`uses: actions/download-artifact@v3- if: inputs.setup_api == 'true'+ if: contains(fromJson(inputs.setup_images), 'api')with:name: apipath: /tmp- name: Load image `api`- if: inputs.setup_api == 'true'+ if: contains(fromJson(inputs.setup_images), 'api')shell: bashrun: |docker load --input /tmp/api.tar++ - name: Download image `api_nginx`+ uses: actions/download-artifact@v3+ if: contains(fromJson(inputs.setup_images), 'api_nginx')+ with:+ name: api_nginx+ path: /tmp++ - name: Load image `api_nginx`+ if: contains(fromJson(inputs.setup_images), 'api_nginx')+ shell: bash+ run: |+ docker load --input /tmp/api_nginx.tardiff --git a/.github/workflows/bundle_size.yml b/.github/workflows/bundle_size.ymlindex 7d60387cd..124a529c0 100644--- a/.github/workflows/bundle_size.yml+++ b/.github/workflows/bundle_size.yml@@ -17,8 +17,8 @@ jobs:permissions:pull-requests: readoutputs:- frontend: ${{ steps.paths-filter.outputs.frontend }}- lint: ${{ steps.paths-filter.outputs.lint }}+ frontend: ${{ contains(fromJson(steps.paths-filter.outputs.changes), 'frontend') }}+ lint: ${{ contains(fromJson(steps.paths-filter.outputs.changes), 'lint') }}steps:- name: Checkout repositorydiff --git a/.github/workflows/ci_cd.yml b/.github/workflows/ci_cd.ymlindex 117f3aa68..4abb02218 100644--- a/.github/workflows/ci_cd.yml+++ b/.github/workflows/ci_cd.yml@@ -30,12 +30,12 @@ jobs:permissions:pull-requests: readoutputs:- catalog: ${{ steps.paths-filter.outputs.catalog }}- api: ${{ steps.paths-filter.outputs.api }}- ingestion_server: ${{ steps.paths-filter.outputs.ingestion_server }}- frontend: ${{ steps.paths-filter.outputs.frontend }}- documentation: ${{ steps.paths-filter.outputs.documentation }}changes: ${{ steps.paths-filter.outputs.changes }}+ catalog: ${{ contains(fromJson(steps.paths-filter.outputs.changes), 'catalog') }}+ ingestion_server: ${{ contains(fromJson(steps.paths-filter.outputs.changes), 'ingestion_server') }}+ api: ${{ contains(fromJson(steps.paths-filter.outputs.changes), 'api') }}+ frontend: ${{ contains(fromJson(steps.paths-filter.outputs.changes), 'frontend') }}+ documentation: ${{ contains(fromJson(steps.paths-filter.outputs.changes), 'documentation') }}steps:- name: Checkout repositoryuses: actions/checkout@v3@@ -97,7 +97,7 @@ jobs:- name: Setup CI envuses: ./.github/actions/setup-envwith:- # Python is not needed to run pre-commit.+ # No Python setup is needed, the built-in Python runtime is okay.setup_python: false# Node.js is needed by lint actions.install_recipe: "node-install"@@ -115,7 +115,10 @@ jobs:add-stack-label:name: Add stack label- if: github.event_name == 'pull_request' && github.repository == 'WordPress/openverse'+ if: |+ github.event_name == 'pull_request' &&+ github.event.pull_request.head.repo.owner.login == 'WordPress' &&+ github.actor != 'dependabot[bot]'runs-on: ubuntu-latestneeds:- get-changes@@ -243,14 +246,13 @@ jobs:setup_python: false# Python is not needed to run the tests.setup_nodejs: false- # Node.js is not needed to run API tests.+ # Node.js is not needed to run the tests.install_recipe: ""- name: Load Docker imagesuses: ./.github/actions/load-imgwith:- setup_api: false- setup_ingestion_server: false+ setup_images: '["upstream_db", "catalog"]'- name: Run testsrun: |@@ -271,7 +273,7 @@ jobs:- name: Setup CI envuses: ./.github/actions/setup-envwith:- # Python is not needed to run pre-commit.+ # Python is not needed to run the tests.setup_python: false# Node.js is needed by lint actions.install_recipe: "node-install"@@ -279,8 +281,7 @@ jobs:- name: Load Docker imagesuses: ./.github/actions/load-imgwith:- setup_api: false- setup_ingestion_server: false+ setup_images: '["upstream_db", "catalog"]'- name: Check DAG documentationrun: |@@ -314,8 +315,7 @@ jobs:- name: Load Docker imagesuses: ./.github/actions/load-imgwith:- setup_api: false- setup_catalog: false+ setup_images: '["upstream_db", "ingestion_server"]'- name: Run ingestion-server testsrun: just ingestion_server/test-local@@ -360,7 +360,7 @@ jobs:- name: Load Docker imagesuses: ./.github/actions/load-imgwith:- setup_catalog: false+ setup_images: '["upstream_db", "ingestion_server", "api"]'- name: Start API, ingest and index test datarun: just api/init@@ -413,7 +413,7 @@ jobs:- name: Load Docker imagesuses: ./.github/actions/load-imgwith:- setup_catalog: false+ setup_images: '["upstream_db", "ingestion_server", "api"]'- name: Run check reciperun: just ${{ matrix.recipe }}@@ -451,8 +451,8 @@ jobs:# Frontend #############- frontend-unit:- name: Run frontend unit tests+ nuxt-build:+ name: Check Nuxt buildif: needs.get-changes.outputs.frontend == 'true'runs-on: ubuntu-latestneeds:@@ -469,16 +469,30 @@ jobs:setup_python: falseinstall_recipe: node-install- - name: Run unit tests- run: just frontend/run test:unit+ - name: Run build+ run: just frontend/run build+ env:+ GLOTPRESS_USERNAME: ${{ secrets.MAKE_USERNAME }}+ GLOTPRESS_PASSWORD: ${{ secrets.MAKE_LOGIN_PASSWORD }}- storybook-smoke:- name: Check Storybook smoke test+ nuxt-checks:+ name: Run Nuxt checksif: needs.get-changes.outputs.frontend == 'true'runs-on: ubuntu-latestneeds:- get-changes- lint+ strategy:+ fail-fast: false+ matrix:+ name:+ - unit_test+ - storybook_smoke_test+ include:+ - name: unit_test+ script: "test:unit"+ - name: storybook_smoke_test+ script: "storybook:smoke"steps:- name: Checkout repository@@ -490,32 +504,27 @@ jobs:setup_python: falseinstall_recipe: node-install- - name: Run Storybook smoke-test- run: just frontend/run storybook:smoke+ - name: Run Nuxt checks+ run: just frontend/run ${{ matrix.script }}- nuxt-build:- name: Check Nuxt build- if: needs.get-changes.outputs.frontend == 'true'+ # This job runs when `nuxt-checks` doesn't and always passes, thus allowing+ # PRs to meet the required checks criteria and be merged.+ bypass-nuxt-checks:+ name: Run Nuxt checks+ if: needs.get-changes.outputs.frontend != 'true'runs-on: ubuntu-latestneeds:- get-changes- - lint+ strategy:+ fail-fast: false+ matrix:+ name:+ - unit_test+ - storybook_smoke_teststeps:- - name: Checkout repository- uses: actions/checkout@v3-- - name: Setup CI env- uses: ./.github/actions/setup-env- with:- setup_python: false- install_recipe: node-install-- - name: Run build- run: just frontend/run build- env:- GLOTPRESS_USERNAME: ${{ secrets.MAKE_USERNAME }}- GLOTPRESS_PASSWORD: ${{ secrets.MAKE_LOGIN_PASSWORD }}+ - name: Pass+ run: echo 'Playwright tests are skipped because frontend is unchanged.'playwright:name: Run Playwright tests@@ -776,6 +785,9 @@ jobs:matrix: ${{ fromJson(needs.determine-images.outputs.publish_matrix) }}steps:+ - name: Checkout repository+ uses: actions/checkout@v3+- name: Log in to GitHub Docker Registryuses: docker/login-action@v2with:@@ -783,15 +795,13 @@ jobs:username: ${{ github.actor }}password: ${{ secrets.GITHUB_TOKEN }}- - name: Download image `${{ matrix.image }}`- uses: actions/download-artifact@v3+ - name: Load Docker image `${{ matrix.image }}`+ uses: ./.github/actions/load-imgwith:- name: ${{ matrix.image }}- path: /tmp+ setup_images: ${{ format('["{0}"]', matrix.image) }}- name: Load and tag image `${{ matrix.image }}` (latest & sha)run: |- docker load --input /tmp/${{ matrix.image }}.tardocker tag openverse-${{ matrix.image }} \ghcr.io/wordpress/openverse-${{ matrix.image }}:latestdocker tag openverse-${{ matrix.image }} \@@ -861,7 +871,7 @@ jobs:# Notification #################- send_report:+ send-report:name: Send Slack reportruns-on: ubuntu-latestif: |diff --git a/documentation/reference/ci_cd/actions.md b/documentation/reference/ci_cd/actions.mdnew file mode 100644index 000000000..dab1b3ec7--- /dev/null+++ b/documentation/reference/ci_cd/actions.md@@ -0,0 +1,90 @@+# Actions++The CI + CD workflow is huge, so we broke off some reusable parts of it into+smaller, more manageable pieces in the form of composite actions.++These actions are located in the `.github/actions/` directory. To use them, the+repository must first be checked out using the+[`actions/checkout`](https://github.com/actions/checkout) action.++## `get-changes`++Determines the changes made to the codebase for a PR or commit. Based on the+changeset, only a subset of the workflow is executed, both in the interest of+speed and to avoid wasteful consumption of resources.++**Outputs:**++```typescript+{+ changes: string // JSON-encoded array of modified changesets+}+```++## `setup-env`++Sets up the runtime environment for a job. It sets up our task runner of choice,+[`just`](../guides/general_setup.md#just) and also the specified languages and+their respective package managers.++- Python (and Pipenv)+- Node.js (and pnpm)++**Inputs:**++By default, both Python and Node.js are installed and configured by this action+but it's recommended to use `setup_python` and `setup_nodejs` inputs to only+install the runtime needed by a job.++By default, it also runs `just install` so that the core dependencies needed on+the system are installed. This recipe can be customised via the `install_recipe`+input (or even disabled with a blank string, to speed up the process in case no+host dependencies are needed).++```typescript+{+ setup_python: "true" | "false" // default: "true"+ setup_nodejs: "true" | "false" // default: "true"+ install_deps: "true" | "false" // default: "true"+ install_recipe: string // default: "install"+}+```++## `load-img`++All Docker images needed throughout the CI + CD workflow are built by the+[`build-images`](#build-images) job matrix, saved as `.tar` files and uploaded+as artifacts. This action is used by subsequent jobs that need those images. It+downloads the artifact and load the `.tar` files into Docker as images.++**Inputs:**++By default, all images built by the Docker system will be loaded but if the job+only needs a subset of images, those can be set via the `setup_images` input,+passing a JSON encoded array of image names as strings.++```typescript+{+ setup_images: string // default: '["upstream_db", "ingestion_server", "catalog", "api", "api_nginx"]'+}+```++## `build-docs`++Builds the documentation, including this Sphinx site, the frontend Storybook and+the Tailwind config viewer and stores it at `/tmp/docs`. This compiled+documentation is deployed to an appropriate location by the+[`emit-docs`](#emit-docs) job.++**Inputs:**++A GlotPress username and password combination is needed to download all+[Openverse translations](https://translate.wordpress.org/projects/meta/openverse/)+in bulk from GlotPress.++```typescript+{+ glotpress_username: string // required, use secret `MAKE_USERNAME`+ glotpress_password: string // required, use secret `MAKE_LOGIN_PASSWORD`+}+```diff --git a/documentation/reference/ci_cd/flow.md b/documentation/reference/ci_cd/flow.mdnew file mode 100644index 000000000..fa34bd433--- /dev/null+++ b/documentation/reference/ci_cd/flow.md@@ -0,0 +1,432 @@+# Flow++Since the CI + CD workflow is very complex, to simplify and understand it, we+can assume it to take place across several stages.++```{mermaid}+flowchart TD+ preparation[Preparation] --> bypass_jobs[Bypass jobs] --- a[ ]+ %% a is a fork+ a --> frontend_tests[Frontend tests] & docker_preparation[Docker preparation]+ docker_preparation --> dockerised_tests[Dockerised tests]+ frontend_tests & dockerised_tests --- b[ ]+ %% b is a join+ b --- c[ ]+ %% c is fork+ c --> documentation_publishing[Documentation] & docker_publishing[Docker publishing]+ docker_publishing --> deployment[Deployment]+ documentation_publishing & deployment --- d[ ]+ %% d is a join+ d --> notification[Notification]++ style a height:0,width:0+ style b height:0,width:0+ style c height:0,width:0+ style d height:0,width:0+```++```{caution}+The stages are a learning tool, not a true representation of how CI works. In+reality, there is no clear lines between stages. Jobs in a later stage will+start as soon as their dependencies are satisfied, without waiting for the full+previous stage to complete first.++For example, the [Docker publishing](#docker-publishing) stage starts as soon as+the tests it depends on have passed, even if there are other jobs still running+in the previous stages (like `playwright` from the+[frontend tests](#frontend-tests) stage).+```++Read on to know more about these stages and the jobs that constitute them. Each+section is also accompanied by a flowchart to help visualise the jobs that+comprise a stage and their relationship with job in prior stages on which this+stage depends.++## Preparation++This is the stage of jobs which have no dependencies, so they get executed at+the very start of the workflow. Since they are depended upon by other jobs, once+this stage is complete, we can use their resulting state and outputs wherever+needed.++```{mermaid}+flowchart TD+ subgraph preparation[Preparation]+ get-changes+ get-image-tag+ lint+ end+```++**Jobs:**++- [`get-changes`](./jobs/preparation.md#get-changes)+- [`get-image-tag`](./jobs/preparation.md#get-image-tag)+- `lint`++## Bypass jobs++If a job is marked as a required check in GitHub it must+[either pass or be skipped](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/troubleshooting-required-status-checks#handling-skipped-but-required-checks)+to allow the PR to be merged. This is different for matrix jobs because if a+matrix is skipped due to an `if` condition, it is not expanded into individual+jobs but skipped as a whole, leaving the checks associated with that matrix in a+pending state, preventing PRs from being merged.++For such jobs, we use a bypass job, conventionally named `bypass-<job name>`,+that is run on the opposite of the condition of the original job `<job name>`.+This bypass job is an identical matrix, except it always succeeds, and satisfies+the required checks by having the same job names as the original.++```{mermaid}+flowchart TD+ subgraph preparation[Preparation]+ get-changes+ end++ subgraph bypass_jobs[Bypass jobs]+ bypass-django-checks+ bypass-nuxt-checks+ bypass-playwright+ end++ get-changes -- api == false --> bypass-django-checks+ get-changes -- frontend == false --> bypass-nuxt-checks+ get-changes -- frontend == false --> bypass-playwright++ style preparation opacity:0.3+```++**Jobs:**++- `bypass-django-checks` (opposite of+ [`django-checks`](./jobs/api.md#django-checks))+- `bypass-nuxt-checks` (opposite of+ [`nuxt-checks`](./jobs/frontend.md#nuxt-checks))+- `bypass-playwright` (opposite of+ [`playwright`](./jobs/frontend.md#playwright))++## Frontend tests++The frontend tests run outside the Docker containers, so they don't need to wait+for Docker containers to be built. This stage happens in parallel with the+[Docker preparation](#docker-preparation) stage. The Playwright tests are very+time-consuming so this stage extends long enough to be contemporary with the+[Dockerised tests](#dockerised-tests) stage.++These tests are only executed if the frontend has changed. Else they will be+skipped and bypass jobs for `nuxt-build` and `playwright` will run instead.++```{mermaid}+flowchart TD+ subgraph preparation[Preparation]+ lint+ get-changes+ end++ get-changes -- frontend == true --- x+ lint --- x+ x[ ] --> nuxt-checks & nuxt-build & playwright++ subgraph frontend_tests[Frontend tests]+ nuxt-checks+ nuxt-build+ playwright+ end++ style x height:0+ style preparation opacity:0.3+```++**Jobs:**++- [`nuxt-checks`](./jobs/frontend.md#nuxt-checks)+- [`nuxt-build`](./jobs/frontend.md#nuxt-build)+- [`playwright`](./jobs/frontend.md#playwright)++## Docker preparation++A number of our jobs test the services running inside Docker containers, so+before we can run those jobs in [Dockerised tests](#dockerised-tests), we must+prepare the Docker images for these services.++The `determine-images` job determines the images to build (and also publish, see+section on [Docker publishing](#docker-publishing) below) based on the changes+observed by `get-changes`. This information is passed to the `build-images` job,+which builds the images.++If `determine-images` finds no images to build, which can happen if there are no+changes to the catalog, ingestion server, API or frontend, the `build-images`+job will be skipped.++```{mermaid}+flowchart TD+ subgraph preparation[Preparation]+ get-changes+ get-image-tag+ lint+ end++ subgraph docker_preparation[Docker preparation]+ determine-images+ build-images+ end++ get-changes -- changes --> determine-images -- do_build,build_matrix --> build-images+ get-image-tag -- image_tag --> build-images+ lint --> build-images++ style preparation opacity:0.3+```++**Jobs:**++- [`determine-images`](./jobs/docker_preparation.md#determine-images)+- [`build-images`](./jobs/docker_preparation.md#build-images)++## Dockerised tests++The tests for the catalog, ingestion server and API all use Docker containers of+their services in the tests. Thus, this stage of testing occurs after the+[Docker preparation](#docker-preparation) stage. Unlike the frontend tests, they+do not depend on the `lint` job, as that is already present as a dependency of+the `build-images` job.++```{mermaid}+flowchart TD+ subgraph preparation[Preparation]+ get-changes+ end++ subgraph docker_preparation[Docker preparation]+ build-images+ end++ get-changes -- catalog == true --- x+ x[ ] --> test-cat & catalog-checks++ get-changes -- ingestion_server == true --- y+ y[ ] --> test-ing++ get-changes -- api == true --- z+ z[ ] --> test-api & django-checks++ build-images -- success --- x & y & z++ subgraph dockerised_tests[Dockerised tests]+ test-cat+ catalog-checks++ test-ing++ test-api+ django-checks+ end++ style x height:0+ style y height:0+ style z height:0+ style preparation opacity:0.3+ style docker_preparation opacity:0.3+```++**Jobs:**++- [`test-cat`](./jobs/catalog.md#test-cat)+- [`catalog-checks`](./jobs/catalog.md#catalog-checks)+- [`test-ing`](./jobs/ingestion_server.md#test-ing)+- [`test-api`](./jobs/api.md#test-api)+- [`django-checks`](./jobs/api.md#django-checks)++## Documentation++After all the [proof-of-functionality](./proof_of_functionality.md) tests have+concluded, we can initiate the stage of building the new documentation. The+documentation is published to the [docs site](https://docs.openvere.org)+side-by-side with the [publishing of the Docker images](#docker-publishing).++```{mermaid}+flowchart TD+ subgraph preparation[Preparation]+ get-changes+ end++ subgraph frontend_tests[Frontend tests]+ nuxt-build+ end++ subgraph dockerised_tests[Dockerised tests]+ test-cat+ test-ing+ test-api+ end++ subgraph documentation[Documentation]+ emit-docs+ end++ get-changes -- frontend == true || documentation == true --> emit-docs+ test-cat & test-ing & test-api & nuxt-build -. success/skipped .-> emit-docs++ style preparation opacity:0.3+ style frontend_tests opacity:0.3+ style dockerised_tests opacity:0.3+```++**Jobs:**++- [`emit-docs`](./jobs/documentation.md#emit-docs)++## Docker publishing++In this stage we publish the Docker images that have passed the+[proof-of-functionality](./proof_of_functionality.md) tests to+[GHCR](https://github.com/orgs/WordPress/packages?repo_name=openverse). We+[publish the new developer docs](#documentation) alongside these images.++The `determine-images` job determines the images to publish (and also build, see+section on [Docker preparation](#docker-preparation) above) based on the changes+observed by `get-changes`. This information is passed to the `publish-images`+job, which publishes the images.++If `determine-images` finds no images to publish, which can happen if there are+no changes to the catalog, ingestion server, API or frontend, the+`publish-images` job will be skipped.++```{mermaid}+flowchart TD+ subgraph preparation[Preparation]+ get-image-tag+ end++ subgraph frontend_tests[Frontend tests]+ nuxt-build+ end++ subgraph docker_preparation[Docker preparation]+ determine-images+ build-images+ end++ subgraph dockerised_tests[Dockerised tests]+ test-cat+ test-ing+ test-api+ end++ subgraph docker_publishing[Docker publishing]+ publish-images+ end++ get-image-tag -- image_tag --> publish-images+ determine-images -- do_publish,publish_matrix --> publish-images+ build-images -- success --> publish-images+ test-cat & test-ing & test-api & nuxt-build -. success/skipped .-> publish-images++ style preparation opacity:0.3+ style docker_preparation opacity:0.3+ style frontend_tests opacity:0.3+ style dockerised_tests opacity:0.3+```++**Jobs:**++- [`publish-images`](./jobs/docker_publishing.md#publish-images)++## Deployment++This stage is specifically for the services running on ECS, namely the API and+the frontend. If either of these services have been updated, the workflow also+updates their staging environments to use the Docker images published in the+[Docker publishing](#docker-publishing) stage.++```{mermaid}+flowchart TD+ subgraph preparation[Preparation]+ get-changes+ get-image-tag+ end++ subgraph deployment[Deployment]+ deploy-api+ deploy-frontend+ end++ subgraph docker_publishing[Docker publishing]+ publish-images+ end++ subgraph frontend_tests[Frontend tests]+ playwright+ end++ get-image-tag -- image_tag --> deploy-api & deploy-frontend++ get-changes -- api == true --> deploy-api+ get-changes -- frontend == true --> deploy-frontend++ publish-images -- success --> deploy-api+ publish-images -- success --> deploy-frontend++ playwright -- success --> deploy-frontend++ style preparation opacity:0.3+ style docker_publishing opacity:0.3+ style frontend_tests opacity:0.3+```++**Jobs:**++- [`deploy-api`](./jobs/deployment.md#deploy-api)+- [`deploy-frontend`](./jobs/deployment.md#deploy-frontend)++## Notification++At this stage, the workflow should have completed all its objectives and+delivered on all four of its key outcomes. In this stage we compare our+expectations set by the `get-changes` job with the actual results of the+workflow and report any discrepancies via Slack.++```{mermaid}+flowchart TD+ subgraph preparation[Preparation]+ get-changes+ end++ subgraph docker_preparation[Docker preparation]+ determine-images+ end++ subgraph documentation_publishing[Documentation publishing]+ emit-docs+ end++ subgraph docker_publishing[Docker publishing]+ publish-images+ end++ subgraph deployment[Deployment]+ deploy-api+ deploy-frontend+ end++ subgraph notification[Notification]+ send-report+ end++ get-changes -- frontend,documentation,api --> send-report+ determine-images -- do_publish --> send-report+ emit-docs -- !success --> send-report+ publish-images -- !success --> send-report+ deploy-api & deploy-frontend -- !success --> send-report++ style preparation opacity:0.3+ style docker_preparation opacity:0.3+ style documentation_publishing opacity:0.3+ style docker_publishing opacity:0.3+ style deployment opacity:0.3+```++**Jobs:**++- [`send-report`](./jobs/notification.md#send-report)diff --git a/documentation/reference/ci_cd/index.md b/documentation/reference/ci_cd/index.mdnew file mode 100644index 000000000..249446dc4--- /dev/null+++ b/documentation/reference/ci_cd/index.md@@ -0,0 +1,56 @@+# CI + CD workflow++The CI + CD workflow is the primary pipeline of the project. It runs the tests+for all layers of the stack that have been changed by the PR or commit it is+being run against and, if the tests pass, publishes the documentation to the+docs site and deploys the API and frontend to their respective staging ECS+targets.++It's a complex workflow with many jobs that depend on each other and the number+of paths it can take becomes huge because jobs can be skipped based on the+contents of a PR or commit.++## Output++The CI + CD workflow has four main outputs when new commits are pushed to the+`main` branch, although not all outputs are expected on each commit. The first+of these, "documentation" is also applicable to PRs.++- **Documentation:** For each PR which affects the documentation, the+ documentation is built and deployed to a preview site. When a new commit is+ pushed to `main` that affects the documentation, the documentation is built+ and deployed to the [root of the docs site](https://docs.openverse.org).++ See job [`emit-docs`](#emit-docs) for details.++- **Docker images:** When a new commit is pushed to `main`, the Docker image for+ each of the affected services is pushed to+ [GHCR](https://github.com/orgs/WordPress/packages?repo_name=openverse). These+ images are tagged with the SHA of the commit.++ See job [`publish-images`](#publish-images).++- **Frontend staging:** When a new commit is pushed to `main` which affects the+ frontend service and the tests for the frontend pass, a new+ [staging deployment of the frontend](https://staging.openverse.org/) is+ triggered. The deployment uses the image published above.++ See job [`deploy-frontend`](#deploy-frontend).++- **API staging:** When a new commit is pushed to `main` which affects the API+ service and the tests for the API pass, a new+ [staging deployment of the API](https://api-staging.openverse.engineering/) is+ triggered. The deployment uses the image published above.++ See job [`deploy-api`](#deploy-api).++## Workflow components++```{toctree}+:maxdepth: 1++actions+jobs/index+flow+proof_of_functionality+```diff --git a/documentation/reference/ci_cd/jobs/api.md b/documentation/reference/ci_cd/jobs/api.mdnew file mode 100644index 000000000..1d77e6024--- /dev/null+++ b/documentation/reference/ci_cd/jobs/api.md@@ -0,0 +1,54 @@+# API jobs++```{mermaid}+flowchart TB+ get-changes{{get-changes}} -- API unchanged --> bypass-django-checks+ get-changes -- API changed --- x[ ] --> deploy-api & test-api & django-checks++ get-changes --> determine-images+ determine-images & get-image-tag & lint --> build-images --> test-api & django-checks --> publish-images --> deploy-api++ style x height:0+```++The API flow uses the following other jobs.++- [`get-changes`](./helpers.md#get-changes)+- [`determine-images`](./docker.md#determine-images)+- [`get-image-tag`](./helpers.md#get-image-tag)+- [`lint`](./helpers.md#lint)+- [`build-images`](./docker.md#build-images)+- [`publish-images`](./docker.md#publish-images)++## `test-api`++Initialises the API using the `api/init` recipe and runs tests for the API using+the `api/test` recipe. Tests are run inside a Docker container so neither Python+nor Node.js needs to be installed.++This job is skipped if the API codebase has not changed. Its counterparts are+[`test-cat`](#test-cat) for the catalog, [`test-ing`](#test-ing) for the+ingestion server and [`nuxt-build`](#nuxt-build) for the frontend.++```{note}+This job is treated as the proof of functionality for publishing Docker images+for the API. Since the job also initialises the API, it provides a basic+verification of the ingestion server's core functionality of populating the API+database and Elasticsearch.+```++## `django-checks`++Runs a matrix of various Django checks for the API using the following `just`+recipes.++- `api/dj check`+- `api/dj validateopenapischema`+- `api/dj makemigrations --check --noinput --merge`+- `api/doc-test`++This job is skipped if the API codebase has not changed. Its counterpart is+[`nuxt-checks`](#nuxt-checks) for the frontend.++Since this is a required check for a matrix job, it has a bypass counterpart.+Refer to the documentation for [bypass jobs](#bypass-jobs).diff --git a/documentation/reference/ci_cd/jobs/catalog.md b/documentation/reference/ci_cd/jobs/catalog.mdnew file mode 100644index 000000000..63ef9e6ae--- /dev/null+++ b/documentation/reference/ci_cd/jobs/catalog.md@@ -0,0 +1,24 @@+# Catalog jobs++## `test-cat`++Runs tests for the catalog using the `catalog/test` recipe. Tests are run inside+a Docker container so neither Python nor Node.js needs to be installed.++This job is skipped if the catalog codebase has not changed. Its counterparts+are [`test-ing`](#test-ing) for the ingestion server, [`test-api`](#test-api)+for the API and [`nuxt-build`](#nuxt-build) for the frontend.++```{note}+This job is treated as the proof of functionality for publishing Docker images+for the catalog.+```++## `catalog-checks`++Runs tests for the catalog using the `catalog/generate-dag-docs` recipe. The job+runs inside a Docker container so Python does not need to be installed. However,+the job generates a new documentation page that must be linted, so+[`setup-env`](#setup-env) in invoked to set up Node.js and linting dependencies.++This job is skipped if the catalog codebase has not changed.diff --git a/documentation/reference/ci_cd/jobs/deployment.md b/documentation/reference/ci_cd/jobs/deployment.mdnew file mode 100644index 000000000..77360de0f--- /dev/null+++ b/documentation/reference/ci_cd/jobs/deployment.md@@ -0,0 +1,40 @@+# Deployment jobs++## `deploy-api`++Triggers a separate workflow using `workflow_dispatch` that deploys the staging+environment of the API service to AWS ECS. That workflow is given two inputs.++- the tag of the image that was published by the+ [`publish-images`](#publish-images) job, which is the output of the+ [`get-image-tag`](#get-image-tag) job+- the actor of the CI + CD workflow, for tagging them in Slack messages++This deployment is only triggered if all the following conditions are met.++- the API codebase has changed+- the [`publish-images`](#publish-images) job has passed, publishing the latest+ frontend image to GHCR+ - The fact that [`publish-images`](#publish-images) ran implies+ [`test-api`](#test-api) passed.++## `deploy-frontend`++Triggers a separate workflow using `workflow_dispatch` that deploys the staging+environment of the frontend service to AWS ECS. That workflow is given two+inputs.++- the tag of the image that was published by the+ [`publish-images`](#publish-images) job, which is the output of the+ [`get-image-tag`](#get-image-tag) job+- the actor of the CI + CD workflow, for tagging them in Slack messages++This deployment is only triggered if all the following conditions are met.++- the frontend codebase has changed+- the [`playwright`](#playwright) job has passed, implying no visual regressions+ have occurred+- the [`publish-images`](#publish-images) job has passed, publishing the latest+ frontend image to GHCR+ - The fact that [`publish-images`](#publish-images) ran implies+ [`nuxt-build`](#nuxt-build) passed.diff --git a/documentation/reference/ci_cd/jobs/docker_preparation.md b/documentation/reference/ci_cd/jobs/docker_preparation.mdnew file mode 100644index 000000000..626d9be90--- /dev/null+++ b/documentation/reference/ci_cd/jobs/docker_preparation.md@@ -0,0 +1,75 @@+# Docker jobs++## `determine-images`++Determines which images to build and publish as a part of the workflow run. To+speed up the workflow and avoid wasteful resource consumption, only a subset of+all images are built by the workflow and a only subset of those images are+published to GHCR. This job determines those images.++| Change | `upstream_db` | `catalog` | `ingestion_server` | `api` | `api_nginx` | `frontend` |+| ------------------ | ------------- | --------- | ------------------ | ----- | ----------- | ---------- |+| `catalog` | π οΈ | π | | | | |+| `ingestion_server` | π οΈ | | π | π οΈ | | |+| `api` | π οΈ | | π οΈ | π | π | |+| `frontend` | | | | | | π |++π denotes that the image is published to GHCR. π οΈ denotes that the image is+built but not published.++```{note}+The `upstream_db` image is only built here and reused in other workflows for+convenience and speed, it is never published.+```++**Outputs:**++The `build_matrix` and `publish_matrix` conventionally use the name singular+name "image" for the field because inside the steps+[`matrix.image`](https://docs.github.com/en/actions/learn-github-actions/contexts#matrix-context)+can be used to refer to the current matrix entry.++```typescript+interface Output {+ do_build: "true" | "false" // whether one or more images are to be built+ build_matrix: {+ image: (+ | "upstream_db"+ | "catalog"+ | "ingestion_server"+ | "api"+ | "api_nginx"+ | "frontend"+ )[] // the names of the image to be built+ include: (+ | { image: "upstream_db"; context: "docker/upstream_db"; target: "db" }+ | { image: "catalog"; context: "catalog"; target: "cat" }+ | {+ image: "ingestion_server"+ context: "ingestion_server"+ target: "ing"+ }+ | { image: "api"; context: "api"; target: "api" }+ | { image: "api_nginx"; context: "api"; target: "nginx" }+ )[] // additional information about images to be built+ }+ do_publish: "true" | "false" // whether one or more images are to be published+ publish_matrix: {+ image: ("catalog" | "ingestion_server" | "api" | "api_nginx" | "frontend")[] // the name of the image to be published+ }+}+```++## `build-images`++Builds images as dictated by the [`determine-images`](#determine-images) job.+Since the images are built in a matrix, some image-specific steps are+conditionally run using an `if` expression. Since any unused Docker build+arguments have no effect, the job simply populates all build arguments needed by+all images.++Images built by this job are published as `.tar` artifacts and can be loaded+into other jobs that need them using the [`load-img`](#load-img) action.++This job is only run if there is at least one image needed to build, based on+the `do_build` output of the [`determine-images`](#determine-images) job.diff --git a/documentation/reference/ci_cd/jobs/docker_publishing.md b/documentation/reference/ci_cd/jobs/docker_publishing.mdnew file mode 100644index 000000000..99bf32ff3--- /dev/null+++ b/documentation/reference/ci_cd/jobs/docker_publishing.md@@ -0,0 +1,19 @@+# Docker publishing jobs++## `publish-images`++Publishes images to GHCR as dictated by the+[`determine-images`](#determine-images) job. In a matrix, this job downloads the+images to be published from their `.tar` files using the [`load-img`](#load-img)+job and then tags and pushes them to+[GHCR](https://github.com/orgs/WordPress/packages?repo_name=openverse).++Images are only published if all the following conditions are met.++- the event is a push to `main`+- the repo is `WordPress/openverse` and not a fork+- there is at least one image to publish as per the `do_publish` output of the+ [`determine-images`](#determine-images) job+- all the primary tests ([`test-cat`](#test-cat), [`test-ing`](#test-ing),+ [`test-api`](#test-api), [`nuxt-build`](#nuxt-build)) succeeded, implying that+ their changes are valid, or were skipped, implying that they have no changes.diff --git a/documentation/reference/ci_cd/jobs/documentation.md b/documentation/reference/ci_cd/jobs/documentation.mdnew file mode 100644index 000000000..a0cec44b8--- /dev/null+++ b/documentation/reference/ci_cd/jobs/documentation.md@@ -0,0 +1,31 @@+# Documentation jobs++## `emit-docs`++Builds the developer documentation and publishes it to an appropriate target.+For PRs, the target is a subdirectory under `_preview/` of the docs site, e.g.+the docs for PR #420 will be published at+<https://docs.openverse.org/_preview/420/>. For commits to `main`, the target is+the <https://docs.openverse.org/> site itself.++```{caution}+This job only publishes developer documentation. The API documentation hosted at+<https://api.openverse.engineering/v1/> is provided by the API service itself.+```++This job is skipped if neither the documentation nor the frontend codebase has+changed. It is also not executed on forks and for PRs made by Dependabot.++Documentation is only emitted if all the following conditions are met.++- the event is one of the following+ - a `push` to `main` and+ - the repo is `WordPress/openverse` and not a fork+ - a PR and+ - the source is a branch of `WordPress/openverse` and not a fork+ - the author of the PR is not Dependabot+- either the documentation or the frontend has changed (for the Storybook and+ Tailwind Config Viewer)+- all the primary tests ([`test-cat`](#test-cat), [`test-ing`](#test-ing),+ [`test-api`](#test-api), [`nuxt-build`](#nuxt-build)) succeeded, implying that+ their changes are valid, or were skipped, implying that they have no changes.diff --git a/documentation/reference/ci_cd/jobs/frontend.md b/documentation/reference/ci_cd/jobs/frontend.mdnew file mode 100644index 000000000..276717a27--- /dev/null+++ b/documentation/reference/ci_cd/jobs/frontend.md@@ -0,0 +1,44 @@+# Frontend jobs++## `nuxt-build`++```{note}+This job is treated as the proof of functionality for publishing Docker images+for the frotend.++This job, combined with the [`playwright` job](#playwright), is treated as the+proof of functionality for deploying frontend to staging.+```++## `nuxt-checks`++Runs a matrix for various Nuxt checks for the frontend using the following+Node.js scripts.++- `test:unit`+- `storybook:smoke`++This job is skipped if the frontend codebase has not changed. Its counterpart is+[`django-checks`](#django-checks) for the API.++Since this is a required check for a matrix job, it has a bypass counterpart.+Refer to the documentation for [bypass jobs](#bypass-jobs).++## `playwright`++Runs a matrix of various Playwright tests for the frontend using the following+Node.js scripts.++- `test:playwright visual-regression`+- `test:playwright e2e`+- `test:storybook`++This job is skipped if the frontend codebase has not changed.++```{note}+This job, combined with the [`nuxt-build` job](#nuxt-build), is treated as the+proof of functionality for deploying frontend to staging.+```++Since this is a required check for a matrix job, it has a bypass counterpart.+Refer to the documentation for [bypass jobs](#bypass-jobs).diff --git a/documentation/reference/ci_cd/jobs/index.md b/documentation/reference/ci_cd/jobs/index.mdnew file mode 100644index 000000000..a950aa127--- /dev/null+++ b/documentation/reference/ci_cd/jobs/index.md@@ -0,0 +1,14 @@+# Jobs++```{toctree}+:maxdepth: 1++preparation+frontend+docker_preparation+docker_publishing+catalog+ingestion_server+api+documentation+```diff --git a/documentation/reference/ci_cd/jobs/ingestion_server.md b/documentation/reference/ci_cd/jobs/ingestion_server.mdnew file mode 100644index 000000000..6827d19c5--- /dev/null+++ b/documentation/reference/ci_cd/jobs/ingestion_server.md@@ -0,0 +1,16 @@+# Ingestion server jobs++### `test-ing`++Runs tests for the ingestion server using the `ingestion_server/test-local`+recipe. Tests are run on the host so Python needs to be installed and set up.++This job is skipped if the ingestion server codebase has not changed. Its+counterparts are [`test-cat`](#test-cat) for the catalog,+[`test-api`](#test-api) for the API and [`nuxt-build`](#nuxt-build) for the+frontend.++```{note}+This job is treated as the proof of functionality for publishing Docker images+for the ingestion server.+```diff --git a/documentation/reference/ci_cd/jobs/notification.md b/documentation/reference/ci_cd/jobs/notification.mdnew file mode 100644index 000000000..38b896d57--- /dev/null+++ b/documentation/reference/ci_cd/jobs/notification.md@@ -0,0 +1,28 @@+# Notification jobs++## `send-report`++Sends a Slack report if the workflow did not do everything it was expected to+do. It lists the outcome for the 4 key outputs of the workflow for a push to+`main`.++This report is only sent if all the following conditions are met.++- the event is a push to `main`+- the repo is `WordPress/openverse` and not a fork+- one or more of the following expectations was not delivered+ - the documentation or frontend had changes, but new docs were not published+ - there were images meant to be published, but they were not published+ - the frontend codebase changed, but it was not deployed to staging+ - the API codebase changed, but it was not deployed to staging++The workflow sends a Slack message, listing the outcome of four jobs++- [`emit-docs`](#emit-docs)+- [`publish-images`](#publish-images)+- [`deploy-frontend`](#deploy-frontend)+- [`deploy-api`](#deploy-api)++Receiving this report in Slack is an indicator that the workflow did not+complete successfully. It is up to the MSR to investigate the cause of the+failure and take appropriate action.diff --git a/documentation/reference/ci_cd/jobs/preparation.md b/documentation/reference/ci_cd/jobs/preparation.mdnew file mode 100644index 000000000..949c083a8--- /dev/null+++ b/documentation/reference/ci_cd/jobs/preparation.md@@ -0,0 +1,40 @@+# Preparation jobs++### `get-changes`++Triggers the [`get-changes`](#get-changes) action to determine the changes. In+addition to the `changes` from the action output, it emits some additional+outputs that are used by subsequent jobs.++**Outputs:**++Each of the `boolean` properties denote whether the PR or commit includes+changes for the corresponding stack.++```typescript+{+ changes: string // JSON-encoded array of modified changesets+ catalog: boolean+ ingestion_server: boolean+ api: boolean+ frontend: boolean+ documentation: boolean+}+```++### `get-image-tag`++Determines the tag to use for the Docker images. If the job is triggered via a+workflow dispatch, the images are tagged with the `image_tag` input taken by the+dispatch. If the job is triggered via a PR or a commit pushed to `main`, the+images are tagged with the full 40-char hash+([`github.sha`](https://docs.github.com/en/actions/learn-github-actions/contexts#github-context))+of the last commit.++**Outputs:**++```typescript+{+ image_tag: string // the tag to use for the Docker images+}+```diff --git a/documentation/reference/ci_cd/proof_of_functionality.md b/documentation/reference/ci_cd/proof_of_functionality.mdnew file mode 100644index 000000000..59314e68b--- /dev/null+++ b/documentation/reference/ci_cd/proof_of_functionality.md@@ -0,0 +1,43 @@+# Proof-of-functionality++In the CI + CD workflow, the concept of proof-of-functionality is used to+determine if the build assets of a service are fit for two use cases.++- published to GHCR+- deployed to the staging environment++For different services, different tests are considered to establish+proof-of-functionality.++| Service | GHCR publish | Staging deploy |+| ---------------- | ------------------------------------------------- | --------------------------------------------------------------------------------------------- |+| Catalog | [`test-cat`](./jobs/catalog.md#test-cat) | [`test-cat`](./jobs/catalog.md#test-cat) |+| Ingestion server | [`test-ing`](./jobs/ingestion_server.md#test-ing) | [`test-ing`](./jobs/ingestion_server.md#test-ing) |+| API | [`test-api`](./jobs/api.md#test-api) | [`test-api`](./jobs/api.md#test-api) |+| Frontend | [`nuxt-build`](./jobs/frontend.md#nuxt-build) | [`nuxt-build`](./jobs/frontend.md#nuxt-build) + [`playwright`](./jobs/frontend.md#playwright) |++## Rationales++### Catalog++The tests for the catalog in `test-cat` are very comprehensive and rigourous,+including extended test suites using the `--extended` flag. Passing these tests+conclusively proves that the catalog works as intended.++### Ingestion server++The ingestion server integration test in `test-ing` invokes all the key+functionality of the ingestion server. Passing these tests conclusively proves+that the ingestion server works as intended.++### API++The API tests in `test-api` include both unit tests for the API and integration+tests that explore the API's functionality. These tests also include example+tests wherein the API's output is compared to known-good output. Passing these+tests conclusively proves that the API works as intended.++### Frontend++The frontends difference between criteria for GHCR publish and staging deploy is+a controversial choice. π§ We will revise this.diff --git a/documentation/reference/index.md b/documentation/reference/index.mdindex 48e2aa7d4..5362b00c3 100644--- a/documentation/reference/index.md+++ b/documentation/reference/index.md@@ -8,5 +8,6 @@ decision_making/indexdev_flowapi/indexfrontend/index+ci_cd/indexsearch_algorithm```