|
| 1 | +name: build-docker |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - 'master' |
| 7 | + - 'main' |
| 8 | + tags: |
| 9 | + - 'v*' |
| 10 | + pull_request: |
| 11 | + merge_group: |
| 12 | + workflow_dispatch: |
| 13 | + inputs: |
| 14 | + git-ref: |
| 15 | + description: 'Git ref (optional)' |
| 16 | + required: false |
| 17 | + |
| 18 | +env: |
| 19 | + IMAGE_REGISTRY: ghcr.io/${{ github.repository_owner }} |
| 20 | + IMAGE_NAME: 'project-docs' |
| 21 | + MAIN_BRANCH: 'master' # pushing to the main branch will update the "edge" tag on the image |
| 22 | + BETA_BRANCH: 'beta' # pushing to this branch will update the "beta" tag on the image |
| 23 | + STABLE_BRANCH: 'stable' # pushing to this branch will update the "stable" tag on the image |
| 24 | + TAG_PREFIX: 'v' # pushing tags with this prefix will add a version tag to the image and update the "latest" tag on the image |
| 25 | + # Because openUC2/openuc2.github.io is a fork of an upstream repo, we can't suppress attempts to push |
| 26 | + # container images for forks of the openUC2/openuc2.github.io repo. That's is probably acceptable for how |
| 27 | + # we develop this project (i.e. with PRs from branches on the openUC2/openuc2.github.io repo, rather than |
| 28 | + # PRs from user-created forks of openUC2/openuc2.github.io). |
| 29 | + PUSH_IMAGE: ${{ github.event_name == 'pull_request' || github.event_name == 'push' || github.event_name == 'push tag' }} |
| 30 | + # TODO(ethanjli): Restore the following line once this repo is detached from its fork upstream |
| 31 | + # PUSH_IMAGE: ${{ (github.event_name == 'pull_request' && !github.event.pull_request.head.repo.fork) || github.event_name == 'push' || github.event_name == 'push tag' }} |
| 32 | + |
| 33 | +jobs: |
| 34 | + build: |
| 35 | + runs-on: ubuntu-latest |
| 36 | + permissions: |
| 37 | + contents: read |
| 38 | + packages: write |
| 39 | + strategy: |
| 40 | + fail-fast: false |
| 41 | + matrix: |
| 42 | + variant: |
| 43 | + - default |
| 44 | + steps: |
| 45 | + - uses: actions/checkout@v4 |
| 46 | + with: |
| 47 | + # Only fetch files we actually need: |
| 48 | + fetch-depth: 0 |
| 49 | + filter: 'blob:none' |
| 50 | + |
| 51 | + # Build documentation website |
| 52 | + - name: Install poetry |
| 53 | + run: pipx install poetry==2.1.3 |
| 54 | + |
| 55 | + - name: Set up Node |
| 56 | + uses: actions/setup-node@v2 |
| 57 | + with: |
| 58 | + node-version: "19" # FIXME: this is very old and out-of-date. Bump the version! |
| 59 | + |
| 60 | + - name: Cache ~/.npm |
| 61 | + uses: actions/cache@v4 |
| 62 | + with: |
| 63 | + path: ~/.npm |
| 64 | + key: ${{ runner.os }}-node-${{ matrix.variant }}-${{ hashFiles('**/package-lock.json') }} |
| 65 | + restore-keys: ${{ runner.os }}-node |
| 66 | + |
| 67 | + - name: Install build dependencies |
| 68 | + run: | |
| 69 | + npm install |
| 70 | +
|
| 71 | + - name: Cache Docusaurus build |
| 72 | + uses: docuactions/cache@v1 |
| 73 | + |
| 74 | + - name: Make documentation ${{ matrix.variant }} |
| 75 | + if: matrix.variant != 'default' |
| 76 | + working-directory: documentation |
| 77 | + run: npm run make-${{ matrix.variant }} |
| 78 | + |
| 79 | + - name: Build documentation |
| 80 | + env: |
| 81 | + # Container image should be built with `/openUC2` as the base URL instead of `/`, as the |
| 82 | + # self-contained root of the site. We use `/openUC2/` instead of `/docs/` as the root so |
| 83 | + # that we don't have pages in `/docs/docs/`, but instead we have them in `/openUC2/docs/`. |
| 84 | + BASE_URL: '/openUC2/' |
| 85 | + run: | |
| 86 | + npm run build |
| 87 | +
|
| 88 | + # Work around a bug where capital letters in the GitHub username (e.g. "PlanktoScope") make it |
| 89 | + # impossible to push to GHCR. See https://github.com/macbre/push-to-ghcr/issues/12 |
| 90 | + - name: Lowercase image registry and owner |
| 91 | + id: image_registry_case |
| 92 | + uses: ASzc/change-string-case-action@v6 |
| 93 | + with: |
| 94 | + string: ${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }} |
| 95 | + |
| 96 | + - name: Set documentation variant suffix |
| 97 | + run: | |
| 98 | + if [[ '${{ matrix.variant }}' != 'default' ]]; then |
| 99 | + echo 'VARIANT_SUFFIX=-${{ matrix.variant}}' >> $GITHUB_ENV |
| 100 | + fi |
| 101 | +
|
| 102 | + # Build and publish Docker container image |
| 103 | + - name: Get Docker metadata |
| 104 | + id: meta |
| 105 | + uses: docker/metadata-action@v5 |
| 106 | + env: |
| 107 | + DOCKER_METADATA_PR_HEAD_SHA: true |
| 108 | + IS_MAIN_BRANCH: ${{ github.ref == format('refs/heads/{0}', env.MAIN_BRANCH) }} |
| 109 | + IS_BETA_BRANCH: ${{ github.ref == format('refs/heads/{0}', env.BETA_BRANCH) }} |
| 110 | + IS_STABLE_BRANCH: ${{ github.ref == format('refs/heads/{0}', env.STABLE_BRANCH) }} |
| 111 | + with: |
| 112 | + images: ${{ steps.image_registry_case.outputs.lowercase }} |
| 113 | + flavor: | |
| 114 | + suffix=${{ env.VARIANT_SUFFIX }} |
| 115 | + tags: | |
| 116 | + type=match,pattern=${{ env.TAG_PREFIX }}(.*),group=1 # this implicitly updates latest |
| 117 | + type=raw,value=stable,enable=${{ env.IS_STABLE_BRANCH }},priority=702 |
| 118 | + type=raw,value=beta,enable=${{ env.IS_BETA_BRANCH }},priority=701 |
| 119 | + type=edge,branch=${{ env.MAIN_BRANCH }} |
| 120 | + type=ref,event=pr |
| 121 | + type=sha,priority=100 |
| 122 | +
|
| 123 | + - name: Set up QEMU |
| 124 | + uses: docker/setup-qemu-action@v3 |
| 125 | + |
| 126 | + - name: Set up Docker Buildx |
| 127 | + uses: docker/setup-buildx-action@v3 |
| 128 | + |
| 129 | + - name: Log in to GitHub Container Registry |
| 130 | + if: env.PUSH_IMAGE == 'true' |
| 131 | + uses: docker/login-action@v3 |
| 132 | + with: |
| 133 | + registry: ghcr.io |
| 134 | + username: ${{ github.repository_owner }} |
| 135 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 136 | + |
| 137 | + - name: Build and push |
| 138 | + uses: docker/build-push-action@v6 |
| 139 | + with: |
| 140 | + context: . |
| 141 | + pull: true |
| 142 | + platforms: linux/amd64,linux/arm64 |
| 143 | + tags: ${{ steps.meta.outputs.tags }} |
| 144 | + labels: ${{ steps.meta.outputs.labels }} |
| 145 | + push: ${{ env.PUSH_IMAGE }} |
0 commit comments