diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 00000000000..dd4a82afd42 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,199 @@ +name: Docker images + +# Images go to Docker Hub (graphprotocol/graph-node, graphprotocol/graph-node-debug), +# where users and deployments pull them, and to ghcr.io under the same names. +# +# | Build | Tags | +# |-------------------------------------------|----------------------------| +# | every build | 7-char short SHA | +# | push to master (except a release commit) | `nightly` | +# | push to hotfix/** | (short SHA only) | +# | push of a `canary-*` tag | the tag, e.g. `canary-fix` | +# | release (called from release.yml) | `vX.Y.Z`, `latest` | +# | run by hand (workflow_dispatch) | (short SHA only) | +# +# To get an image of a commit that isn't on master (a canary), push a tag +# `canary-` pointing at it. +on: + push: + branches: [master, "hotfix/**"] + tags: ["canary-*"] + workflow_dispatch: + workflow_call: + inputs: + tag_name: + type: string + default: "" + # release.yml passes it with `secrets: inherit`; the first step checks it. + secrets: + DOCKERHUB_CI_TOKEN: + required: false + +permissions: + contents: read + +env: + # The organisation's Docker Hub token (org secret DOCKERHUB_CI_TOKEN) is an + # organization access token, which logs in as the organisation. + DOCKERHUB_USERNAME: graphprotocol + +jobs: + docker: + name: Build and push images + # release-please's release commit is built once, by the release + # (release.yml calls this workflow with tag_name), so its short-SHA tag is + # the release image; its own push to master builds nothing, and `nightly` + # stays on the commit before it. A called run sees the caller's push + # event, hence the tag_name test. + if: >- + inputs.tag_name != '' || + github.event_name != 'push' || + !startsWith(github.event.head_commit.message, 'chore(master): release ') + # The Dockerfile compiles graph-node in release mode, which needs the + # cores and disk of a large runner. + runs-on: nscloud-ubuntu-22.04-amd64-16x32 + timeout-minutes: 60 + permissions: + contents: read + packages: write + id-token: write + attestations: write + # One build per ref at a time, in order; a newer push replaces a waiting + # one. A release (tag_name set) has its own group, so a nightly can't + # replace it. + concurrency: + group: docker-${{ inputs.tag_name || github.ref }} + cancel-in-progress: false + steps: + # Docker Hub is where images are pulled from, so a build that can't push + # there fails instead of quietly publishing to ghcr.io alone. + - name: Check Docker Hub credentials + env: + DOCKERHUB_CI_TOKEN: ${{ secrets.DOCKERHUB_CI_TOKEN }} + run: | + if [ -z "$DOCKERHUB_CI_TOKEN" ]; then + echo "::error::DOCKERHUB_CI_TOKEN is not available to this workflow" >&2 + exit 1 + fi + + - name: Checkout code + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 + with: + ref: ${{ inputs.tag_name }} + + # A release is told apart by tag_name, not the event: called from + # release.yml, the event is the caller's push to master. + - name: Image metadata (graph-node) + id: meta + uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6 + with: + images: | + docker.io/graphprotocol/graph-node + ghcr.io/graphprotocol/graph-node + flavor: latest=false + # Keep the tags of the two images the same. + tags: | + type=sha,prefix= + type=raw,value=nightly,enable=${{ inputs.tag_name == '' && github.ref == 'refs/heads/master' }} + type=raw,value=${{ github.ref_name }},enable=${{ inputs.tag_name == '' && startsWith(github.ref, 'refs/tags/canary-') }} + type=raw,value=${{ inputs.tag_name }},enable=${{ inputs.tag_name != '' }} + type=raw,value=latest,enable=${{ inputs.tag_name != '' }} + + - name: Image metadata (graph-node-debug) + id: meta-debug + uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6 + with: + images: | + docker.io/graphprotocol/graph-node-debug + ghcr.io/graphprotocol/graph-node-debug + flavor: latest=false + tags: | + type=sha,prefix= + type=raw,value=nightly,enable=${{ inputs.tag_name == '' && github.ref == 'refs/heads/master' }} + type=raw,value=${{ github.ref_name }},enable=${{ inputs.tag_name == '' && startsWith(github.ref, 'refs/tags/canary-') }} + type=raw,value=${{ inputs.tag_name }},enable=${{ inputs.tag_name != '' }} + type=raw,value=latest,enable=${{ inputs.tag_name != '' }} + + - name: Log in to Docker Hub + uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4 + with: + username: ${{ env.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_CI_TOKEN }} + + - name: Log in to ghcr.io + uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + # Both images are built by one builder, so the graph-node-debug build + # reuses the graph-node-build stage from its cache. + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4 + + # context is set because the action's default is a git context of + # github.sha, which is not the checked-out release tag. linux/amd64 only. + # The build args only fill /etc/image-info. + - name: Build and push graph-node + id: build + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7 + with: + context: . + file: docker/Dockerfile + target: graph-node + platforms: linux/amd64 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + COMMIT_SHA=${{ github.sha }} + REPO_NAME=${{ github.event.repository.name }} + BRANCH_NAME=${{ inputs.tag_name == '' && github.ref_type == 'branch' && github.ref_name || '' }} + TAG_NAME=${{ inputs.tag_name || (github.ref_type == 'tag' && github.ref_name) || '' }} + + - name: Build and push graph-node-debug + id: build-debug + uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7 + with: + context: . + file: docker/Dockerfile + target: graph-node-debug + platforms: linux/amd64 + push: true + tags: ${{ steps.meta-debug.outputs.tags }} + labels: ${{ steps.meta-debug.outputs.labels }} + build-args: | + COMMIT_SHA=${{ github.sha }} + REPO_NAME=${{ github.event.repository.name }} + BRANCH_NAME=${{ inputs.tag_name == '' && github.ref_type == 'branch' && github.ref_name || '' }} + TAG_NAME=${{ inputs.tag_name || (github.ref_type == 'tag' && github.ref_name) || '' }} + + # Build provenance, stored with GitHub and pushed next to the ghcr.io + # image. Both registries hold the same digest, so it verifies either: + # gh attestation verify oci://docker.io/graphprotocol/graph-node: -R graphprotocol/graph-node + - name: Attest graph-node + uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 + with: + subject-name: ghcr.io/graphprotocol/graph-node + subject-digest: ${{ steps.build.outputs.digest }} + push-to-registry: true + + - name: Attest graph-node-debug + uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 + with: + subject-name: ghcr.io/graphprotocol/graph-node-debug + subject-digest: ${{ steps.build-debug.outputs.digest }} + push-to-registry: true + + - name: Summary + env: + TAGS: ${{ steps.meta.outputs.tags }} + DIGEST: ${{ steps.build.outputs.digest }} + run: | + { + echo "### graph-node images (${DIGEST})" + echo '```' + echo "$TAGS" + echo '```' + } >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 00000000000..4eb4806e618 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,199 @@ +name: Publish + +on: + workflow_call: + inputs: + tag_name: + required: true + type: string + dry_run: + type: boolean + default: false + workflow_dispatch: + inputs: + tag_name: + description: "Tag to publish (e.g. v0.46.0); with dry_run, any ref" + required: true + type: string + dry_run: + description: "Build and upload workflow artifacts only; leave the release untouched" + type: boolean + default: false + +env: + CARGO_TERM_COLOR: always + +permissions: + contents: read + +jobs: + build: + name: Build ${{ matrix.asset_suffix }} + runs-on: nscloud-ubuntu-22.04-amd64-16x32 + timeout-minutes: 60 + strategy: + fail-fast: true + matrix: + # run_check marks the entries whose target is the runner's own architecture. + include: + - target: x86_64-unknown-linux-gnu + asset_suffix: linux-x86_64 + run_check: true + file_type: "^ELF 64-bit LSB .*x86-64" + - target: aarch64-unknown-linux-gnu + asset_suffix: linux-aarch64 + run_check: false + file_type: "^ELF 64-bit LSB .*ARM aarch64" + steps: + - name: Checkout code + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5 + with: + ref: ${{ inputs.tag_name }} + + # rustflags is cleared because the action otherwise sets RUSTFLAGS to + # "-D warnings", which would let a new warning fail a release build. + - name: Setup rust toolchain + uses: actions-rust-lang/setup-rust-toolchain@fb51252c7ba57d633bc668f941da052e410add48 # v1 + with: + target: ${{ matrix.target }} + cache: false + rustflags: "" + + # perl and make build the vendored OpenSSL; libprotobuf-dev carries the + # well-known protos the firehose build script imports. + - name: Install system packages + run: | + set -euo pipefail + sudo apt-get update + sudo apt-get install -y --no-install-recommends \ + binutils file make perl pkg-config protobuf-compiler libprotobuf-dev + if [ "${{ matrix.target }}" = aarch64-unknown-linux-gnu ]; then + sudo apt-get install -y --no-install-recommends gcc-aarch64-linux-gnu libc6-dev-arm64-cross qemu-user + echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> "$GITHUB_ENV" + fi + + # gnd is built alongside only for its pq-sys `bundled` feature, which + # links libpq statically into graph-node too, so the binary needs no + # libpq on the host. gnd itself is released by gnd-binary-build.yml. + - name: Build + run: cargo build --release --locked --target ${{ matrix.target }} --bin graph-node --bin gnd + + # The architecture is asserted, and the binary must not need libpq on the + # host: it is linked statically only because gnd's pq-sys `bundled` + # feature is unified into this build. A cross-built binary runs under + # qemu-user against the cross libc. + - name: Stage binary + env: + FILE_TYPE: ${{ matrix.file_type }} + RUN_CHECK: ${{ matrix.run_check }} + run: | + set -euo pipefail + mkdir -p release + binary="release/graph-node-${{ matrix.asset_suffix }}" + cp "target/${{ matrix.target }}/release/graph-node" "$binary" + chmod +x "$binary" + kind=$(file -b "$binary") + echo "$kind" + grep -qE "$FILE_TYPE" <<<"$kind" || { echo "unexpected binary type: $kind" >&2; exit 1; } + needed=$(readelf -d "$binary" | awk '/NEEDED/ {print $NF}') + echo "$needed" + if grep -q libpq <<<"$needed"; then + echo "graph-node links libpq dynamically; expected it statically linked" >&2 + exit 1 + fi + if [ "$RUN_CHECK" = true ]; then + "$binary" --help > /dev/null + else + qemu-aarch64 -L /usr/aarch64-linux-gnu "$binary" --help > /dev/null + fi + gzip "$binary" + + - name: Upload artifact + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 + with: + name: binaries-${{ matrix.asset_suffix }} + path: release/*.gz + if-no-files-found: error + + publish: + name: Publish + needs: build + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: write + id-token: write + attestations: write + concurrency: + group: publish + steps: + - name: Resolve tag + id: v + env: + TAG: ${{ inputs.tag_name }} + DRY_RUN: ${{ inputs.dry_run || false }} + run: | + set -euo pipefail + if [ "$DRY_RUN" != true ]; then + [[ "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] || { echo "tag_name must be vX.Y.Z, got: $TAG" >&2; exit 1; } + fi + echo "tag=$TAG" >> "$GITHUB_OUTPUT" + + - name: Download binaries + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8 + with: + path: dist + pattern: binaries-* + merge-multiple: true + + # graph-node for both Linux targets. + - name: Checksum binaries + working-directory: dist + run: | + set -euo pipefail + shopt -s nullglob + archives=(*.gz) + [ "${#archives[@]}" -eq 2 ] || { echo "expected 2 archives, found ${#archives[@]}" >&2; exit 1; } + sha256sum -- "${archives[@]}" > SHA256SUMS + cat SHA256SUMS + + - name: Upload release bundle + if: inputs.dry_run + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 + with: + name: release-bundle + path: dist/* + + # Build provenance for the release's archives, stored with GitHub: + # gh attestation verify graph-node-linux-x86_64.gz -R graphprotocol/graph-node + - name: Attest binaries + if: ${{ !inputs.dry_run }} + uses: actions/attest-build-provenance@4d101475d8b20a2381f78447822ac1eab6504dd8 # v4.2.2 + with: + subject-path: dist/*.gz + + - name: Attach to release + if: ${{ !inputs.dry_run }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + set -euo pipefail + gh release upload "${{ steps.v.outputs.tag }}" --repo "$GITHUB_REPOSITORY" --clobber \ + dist/*.gz dist/SHA256SUMS + + - name: Summary + env: + DRY_RUN: ${{ inputs.dry_run || false }} + run: | + set -euo pipefail + { + if [ "$DRY_RUN" = true ]; then + echo "### Dry run for ${{ steps.v.outputs.tag }}: release left untouched" + else + echo "### graph-node ${{ steps.v.outputs.tag }}" + echo "https://github.com/${GITHUB_REPOSITORY}/releases/tag/${{ steps.v.outputs.tag }}" + fi + echo '```' + cat dist/SHA256SUMS + echo '```' + } >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b146c8048b4..949b906be24 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,14 +8,16 @@ on: permissions: contents: read -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - jobs: release-please: name: Release Please runs-on: ubuntu-latest timeout-minutes: 5 + concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + outputs: + release_created: ${{ steps.release.outputs.release_created }} + tag_name: ${{ steps.release.outputs.tag_name }} steps: # release-please acts as the organisation's release App, not as # GITHUB_TOKEN: what GITHUB_TOKEN creates triggers no workflows, so the @@ -58,3 +60,31 @@ jobs: else echo "Nothing to release" >> "$GITHUB_STEP_SUMMARY" fi + + publish: + name: Publish binaries + needs: release-please + if: needs.release-please.outputs.release_created == 'true' + # A called workflow gets at most its caller's permissions; id-token and + # attestations are for the build provenance. + permissions: + contents: write + id-token: write + attestations: write + uses: ./.github/workflows/publish.yml + with: + tag_name: ${{ needs.release-please.outputs.tag_name }} + + docker: + name: Docker images + needs: release-please + if: needs.release-please.outputs.release_created == 'true' + permissions: + contents: read + packages: write + id-token: write + attestations: write + uses: ./.github/workflows/docker.yml + secrets: inherit + with: + tag_name: ${{ needs.release-please.outputs.tag_name }} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 84e7062df1b..f81b62467ac 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -104,3 +104,19 @@ the changes since the last release to `CHANGELOG.md`. To release: To release a version other than the one release-please proposes, put `Release-As: X.Y.Z` in the body of a commit on master (for example in the squash-merge description of a pull request). + +## Docker images + +`.github/workflows/docker.yml` publishes `graphprotocol/graph-node` and +`graphprotocol/graph-node-debug` to Docker Hub and to `ghcr.io`, with build +provenance: + +- every push to master: the short commit SHA (7 characters) and `nightly`; +- every push to a `hotfix/**` branch: the short SHA; +- a release: `vX.Y.Z` and `latest`. + +To get an image of a commit that isn't on master (a canary), push a tag +named `canary-` pointing at it, for example +`t=canary-$(git rev-parse --short HEAD); git tag "$t" && git push origin "$t"`. +The image is tagged with the tag's name and the short SHA; the tag can be +deleted once the image is published. diff --git a/docker/build.sh b/docker/build.sh index 5dd67ea99d5..8ccf5f63770 100755 --- a/docker/build.sh +++ b/docker/build.sh @@ -1,7 +1,7 @@ #! /bin/bash # This file is only here to ease testing/development. Official images are -# built using the 'cloudbuild.yaml' file +# built by the .github/workflows/docker.yml workflow type -p podman > /dev/null && docker=podman || docker=docker diff --git a/docker/cloudbuild.yaml b/docker/cloudbuild.yaml deleted file mode 100644 index 0bf800cddad..00000000000 --- a/docker/cloudbuild.yaml +++ /dev/null @@ -1,53 +0,0 @@ -options: - machineType: "E2_HIGHCPU_32" -timeout: 1800s -steps: -- name: 'gcr.io/cloud-builders/docker' - args: ['build', '--target', 'graph-node-build', - '--build-arg', 'COMMIT_SHA=$COMMIT_SHA', - '--build-arg', 'REPO_NAME=$REPO_NAME', - '--build-arg', 'BRANCH_NAME=$BRANCH_NAME', - '--build-arg', 'TAG_NAME=$TAG_NAME', - '-t', 'gcr.io/$PROJECT_ID/graph-node-build:$SHORT_SHA', - '-f', 'docker/Dockerfile', '.'] -- name: 'gcr.io/cloud-builders/docker' - args: ['build', '--target', 'graph-node', - '--build-arg', 'COMMIT_SHA=$COMMIT_SHA', - '--build-arg', 'REPO_NAME=$REPO_NAME', - '--build-arg', 'BRANCH_NAME=$BRANCH_NAME', - '--build-arg', 'TAG_NAME=$TAG_NAME', - '-t', 'gcr.io/$PROJECT_ID/graph-node:$SHORT_SHA', - '-f', 'docker/Dockerfile', '.'] -- name: 'gcr.io/cloud-builders/docker' - args: ['build', '--target', 'graph-node-debug', - '--build-arg', 'COMMIT_SHA=$COMMIT_SHA', - '--build-arg', 'REPO_NAME=$REPO_NAME', - '--build-arg', 'BRANCH_NAME=$BRANCH_NAME', - '--build-arg', 'TAG_NAME=$TAG_NAME', - '-t', 'gcr.io/$PROJECT_ID/graph-node-debug:$SHORT_SHA', - '-f', 'docker/Dockerfile', '.'] -- name: 'gcr.io/cloud-builders/docker' - args: ['tag', - 'gcr.io/$PROJECT_ID/graph-node:$SHORT_SHA', - 'lutter/graph-node:$SHORT_SHA'] -- name: 'gcr.io/cloud-builders/docker' - entrypoint: 'bash' - args: ['docker/tag.sh'] - secretEnv: ['PASSWORD'] - env: - - 'SHORT_SHA=$SHORT_SHA' - - 'TAG_NAME=$TAG_NAME' - - 'PROJECT_ID=$PROJECT_ID' - - 'DOCKER_HUB_USER=$_DOCKER_HUB_USER' - - 'BRANCH_NAME=$BRANCH_NAME' -images: - - 'gcr.io/$PROJECT_ID/graph-node-build:$SHORT_SHA' - - 'gcr.io/$PROJECT_ID/graph-node:$SHORT_SHA' - - 'gcr.io/$PROJECT_ID/graph-node-debug:$SHORT_SHA' -substitutions: - # The owner of the access token whose encrypted value is in PASSWORD - _DOCKER_HUB_USER: "lutter" -secrets: - - kmsKeyName: projects/the-graph-staging/locations/global/keyRings/docker/cryptoKeys/docker-hub-push - secretEnv: - PASSWORD: 'CiQAdfFldbmUiHgGP1lPq6bAOfd+VQ/dFwyohB1IQwiwQg03ZE8STQDvWKpv6eJHVUN1YoFC5FcooJrH+Stvx9oMD7jBjgxEH5ngIiAysWP3E4Pgxt/73xnaanbM1EQ94eVFKCiY0GaEKFNu0BJx22vCYmU4' diff --git a/docker/tag.sh b/docker/tag.sh deleted file mode 100644 index 1abafa95afa..00000000000 --- a/docker/tag.sh +++ /dev/null @@ -1,31 +0,0 @@ -#! /bin/bash - -# This script is used by cloud build to push Docker images into Docker hub - -tag_and_push() { - tag=$1 - docker tag gcr.io/$PROJECT_ID/graph-node:$SHORT_SHA \ - graphprotocol/graph-node:$tag - docker push graphprotocol/graph-node:$tag - - docker tag gcr.io/$PROJECT_ID/graph-node-debug:$SHORT_SHA \ - graphprotocol/graph-node-debug:$tag - docker push graphprotocol/graph-node-debug:$tag -} - -echo "Logging into Docker Hub" -echo $PASSWORD | docker login --username="$DOCKER_HUB_USER" --password-stdin - -set -ex - -tag_and_push "$SHORT_SHA" - -# Builds of tags set the tag in Docker Hub, too -[ -n "$TAG_NAME" ] && tag_and_push "$TAG_NAME" -# Builds for tags vN.N.N become the 'latest' -[[ "$TAG_NAME" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]] && tag_and_push latest - -# If the build is from the master branch, tag it as 'nightly' -[ "$BRANCH_NAME" = "master" ] && tag_and_push nightly - -exit 0