|
| 1 | +# Internal reusable workflow for building a non-OSS ("cloud") Docker image and |
| 2 | +# pushing it to Amazon ECR. |
| 3 | +# |
| 4 | +# Unlike _build.yml (which builds the public, multi-platform OSS image for GHCR), |
| 5 | +# this workflow bakes environment-specific configuration into the image — Sentry |
| 6 | +# DSNs, the Sentry environment name — and uploads source maps to Sentry. Those |
| 7 | +# values come from the GitHub Environment named by `environment`, so a new |
| 8 | +# deployment environment is a new Environment plus a small caller workflow. |
| 9 | +# |
| 10 | +# Each environment publishes to its own ECR repository, `sourcebot-<environment>`, |
| 11 | +# owned by CicdStack in the sourcebot-demo-infra repo. |
| 12 | +# |
| 13 | +# Single-platform (linux/amd64): the EKS `general-purpose` NodePool that runs |
| 14 | +# Sourcebot pins `kubernetes.io/arch: amd64`. Building one platform lets us push |
| 15 | +# tags directly, skipping the push-by-digest + manifest-merge dance _build.yml |
| 16 | +# needs. It also avoids ECR lifecycle rules for untagged images silently deleting |
| 17 | +# a manifest list's per-platform children. |
| 18 | + |
| 19 | +name: Build Cloud Image |
| 20 | + |
| 21 | +on: |
| 22 | + workflow_call: |
| 23 | + inputs: |
| 24 | + environment: |
| 25 | + description: "GitHub Environment supplying the Sentry vars/secrets. Also scopes the OIDC subject used to assume the ECR push role." |
| 26 | + required: true |
| 27 | + type: string |
| 28 | + git_ref: |
| 29 | + description: "Git ref to checkout and build" |
| 30 | + required: true |
| 31 | + type: string |
| 32 | + docker_tags: |
| 33 | + description: "Docker tags configuration for docker/metadata-action" |
| 34 | + required: true |
| 35 | + type: string |
| 36 | + aws_region: |
| 37 | + description: "Region the ECR repository lives in" |
| 38 | + required: false |
| 39 | + type: string |
| 40 | + default: us-west-1 |
| 41 | + |
| 42 | +jobs: |
| 43 | + build: |
| 44 | + runs-on: ubuntu-latest |
| 45 | + # Gates the job on the Environment's protection rules, and — because the |
| 46 | + # OIDC subject for a job with an environment is |
| 47 | + # `repo:<org>/<repo>:environment:<name>` — is what the ECR push role's trust |
| 48 | + # policy matches on. Also what makes `vars`/`secrets` below resolve. |
| 49 | + environment: ${{ inputs.environment }} |
| 50 | + permissions: |
| 51 | + contents: read |
| 52 | + # Required to request the OIDC token that assumes the AWS role. |
| 53 | + id-token: write |
| 54 | + |
| 55 | + steps: |
| 56 | + - name: Checkout repository |
| 57 | + uses: actions/checkout@v4 |
| 58 | + with: |
| 59 | + ref: ${{ inputs.git_ref }} |
| 60 | + submodules: "true" |
| 61 | + fetch-depth: 0 |
| 62 | + |
| 63 | + # The exact commit built. `github.sha` is the SHA of the ref the workflow |
| 64 | + # was *dispatched* on, which is not necessarily `git_ref`. |
| 65 | + - name: Resolve build commit SHA |
| 66 | + id: commit |
| 67 | + run: echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT" |
| 68 | + |
| 69 | + # Fail fast, and loudly. The Dockerfile only uploads source maps when every |
| 70 | + # Sentry input is non-empty, so a missing var or secret would otherwise |
| 71 | + # produce a perfectly green build of an image with no Sentry wiring. Values |
| 72 | + # are never printed — only whether each resolved to something. |
| 73 | + - name: Validate environment configuration |
| 74 | + env: |
| 75 | + SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }} |
| 76 | + AWS_ECR_ROLE_ARN: ${{ vars.AWS_ECR_ROLE_ARN }} |
| 77 | + NEXT_PUBLIC_SENTRY_ENVIRONMENT: ${{ vars.NEXT_PUBLIC_SENTRY_ENVIRONMENT }} |
| 78 | + NEXT_PUBLIC_SENTRY_WEBAPP_DSN: ${{ vars.NEXT_PUBLIC_SENTRY_WEBAPP_DSN }} |
| 79 | + NEXT_PUBLIC_SENTRY_BACKEND_DSN: ${{ vars.NEXT_PUBLIC_SENTRY_BACKEND_DSN }} |
| 80 | + SENTRY_ORG: ${{ vars.SENTRY_ORG }} |
| 81 | + SENTRY_WEBAPP_PROJECT: ${{ vars.SENTRY_WEBAPP_PROJECT }} |
| 82 | + SENTRY_BACKEND_PROJECT: ${{ vars.SENTRY_BACKEND_PROJECT }} |
| 83 | + run: | |
| 84 | + missing=0 |
| 85 | + for name in SENTRY_AUTH_TOKEN AWS_ECR_ROLE_ARN \ |
| 86 | + NEXT_PUBLIC_SENTRY_ENVIRONMENT \ |
| 87 | + NEXT_PUBLIC_SENTRY_WEBAPP_DSN \ |
| 88 | + NEXT_PUBLIC_SENTRY_BACKEND_DSN \ |
| 89 | + SENTRY_ORG SENTRY_WEBAPP_PROJECT SENTRY_BACKEND_PROJECT; do |
| 90 | + if [ -z "${!name}" ]; then |
| 91 | + echo "::error::${name} is not set on the '${{ inputs.environment }}' environment (or the repository)." |
| 92 | + missing=1 |
| 93 | + else |
| 94 | + echo "ok: ${name}" |
| 95 | + fi |
| 96 | + done |
| 97 | + if [ "$missing" -ne 0 ]; then |
| 98 | + echo "::error::Refusing to build: the image would ship without Sentry wiring." |
| 99 | + exit 1 |
| 100 | + fi |
| 101 | +
|
| 102 | + - name: Configure AWS credentials |
| 103 | + uses: aws-actions/configure-aws-credentials@v4 |
| 104 | + with: |
| 105 | + role-to-assume: ${{ vars.AWS_ECR_ROLE_ARN }} |
| 106 | + aws-region: ${{ inputs.aws_region }} |
| 107 | + |
| 108 | + - name: Login to Amazon ECR |
| 109 | + id: ecr |
| 110 | + uses: aws-actions/amazon-ecr-login@v2 |
| 111 | + |
| 112 | + # Each environment publishes to its own registry (see CicdStack), so a |
| 113 | + # staging build can never overwrite a prod tag. |
| 114 | + - name: Extract Docker metadata |
| 115 | + id: meta |
| 116 | + uses: docker/metadata-action@v5 |
| 117 | + with: |
| 118 | + images: ${{ steps.ecr.outputs.registry }}/sourcebot-${{ inputs.environment }} |
| 119 | + tags: ${{ inputs.docker_tags }} |
| 120 | + |
| 121 | + - name: Set up Docker Buildx |
| 122 | + uses: docker/setup-buildx-action@v3 |
| 123 | + |
| 124 | + - name: Build and push Docker image |
| 125 | + uses: docker/build-push-action@v7 |
| 126 | + with: |
| 127 | + context: . |
| 128 | + platforms: linux/amd64 |
| 129 | + push: true |
| 130 | + tags: ${{ steps.meta.outputs.tags }} |
| 131 | + labels: ${{ steps.meta.outputs.labels }} |
| 132 | + # SENTRY_RELEASE is the commit SHA rather than SOURCEBOT_VERSION so that |
| 133 | + # every prod build gets a distinct release (prod tracks `main`, where the |
| 134 | + # version only moves on a tagged release). packages/backend/src/instrument.ts |
| 135 | + # reports NEXT_PUBLIC_BUILD_COMMIT_SHA as its release to match; the webapp |
| 136 | + # gets SENTRY_RELEASE injected into its bundle by withSentryConfig. |
| 137 | + build-args: | |
| 138 | + NEXT_PUBLIC_BUILD_COMMIT_SHA=${{ steps.commit.outputs.sha }} |
| 139 | + NEXT_PUBLIC_SENTRY_ENVIRONMENT=${{ vars.NEXT_PUBLIC_SENTRY_ENVIRONMENT }} |
| 140 | + NEXT_PUBLIC_SENTRY_WEBAPP_DSN=${{ vars.NEXT_PUBLIC_SENTRY_WEBAPP_DSN }} |
| 141 | + NEXT_PUBLIC_SENTRY_BACKEND_DSN=${{ vars.NEXT_PUBLIC_SENTRY_BACKEND_DSN }} |
| 142 | + NEXT_PUBLIC_LANGFUSE_PUBLIC_KEY=${{ vars.NEXT_PUBLIC_LANGFUSE_PUBLIC_KEY }} |
| 143 | + NEXT_PUBLIC_LANGFUSE_BASE_URL=${{ vars.NEXT_PUBLIC_LANGFUSE_BASE_URL }} |
| 144 | + SENTRY_ORG=${{ vars.SENTRY_ORG }} |
| 145 | + SENTRY_WEBAPP_PROJECT=${{ vars.SENTRY_WEBAPP_PROJECT }} |
| 146 | + SENTRY_BACKEND_PROJECT=${{ vars.SENTRY_BACKEND_PROJECT }} |
| 147 | + SENTRY_RELEASE=${{ steps.commit.outputs.sha }} |
| 148 | + # Passed as a secret, not a build-arg: build args are recorded in layer |
| 149 | + # metadata that `mode=max` exports to the cache. @see: Dockerfile |
| 150 | + secrets: | |
| 151 | + sentry_auth_token=${{ secrets.SENTRY_AUTH_TOKEN }} |
| 152 | + # Cache scope is per-environment, and distinct from the OSS build's |
| 153 | + # (which is keyed on platform alone). Sharing a scope would let a build |
| 154 | + # that never sees SENTRY_AUTH_TOKEN restore layers from one that did. |
| 155 | + cache-from: type=gha,scope=cloud-${{ inputs.environment }}-amd64 |
| 156 | + cache-to: type=gha,mode=max,scope=cloud-${{ inputs.environment }}-amd64 |
| 157 | + |
| 158 | + - name: Summarize |
| 159 | + env: |
| 160 | + TAGS: ${{ steps.meta.outputs.tags }} |
| 161 | + run: | |
| 162 | + { |
| 163 | + echo "### Pushed to ECR" |
| 164 | + echo |
| 165 | + echo "| | |" |
| 166 | + echo "|---|---|" |
| 167 | + echo "| Environment | \`${{ inputs.environment }}\` |" |
| 168 | + echo "| Commit | \`${{ steps.commit.outputs.sha }}\` |" |
| 169 | + echo "| Sentry release | \`${{ steps.commit.outputs.sha }}\` |" |
| 170 | + echo |
| 171 | + echo '```' |
| 172 | + echo "$TAGS" |
| 173 | + echo '```' |
| 174 | + } >> "$GITHUB_STEP_SUMMARY" |
0 commit comments