Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 175 additions & 2 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@ name: CD

on:
workflow_call:
inputs:
trusted-publishing:
description: >-
Publish with RubyGems trusted publishing (OIDC) instead of the org-wide RUBYGEMS_API_KEY.
The calling job must grant id-token: write, and the gem needs a trusted publisher on
rubygems.org for this workflow. See the README.
required: false
type: boolean
default: false
ruby-version:
description: >-
The Ruby that reads the gemspec and runs `rake release` when trusted-publishing is true.
The default, the latest Ruby 3.x, matches the ruby:3 image the API-key release runs in.
required: false
type: string
default: '3'
attestations:
description: >-
When trusted-publishing is true, attach Sigstore attestations to the published gem.
Sigstore's public transparency log records the repository, workflow and commit of each one.
required: false
type: boolean
default: true
jobs:
deploy: # zizmor: ignore[secrets-outside-env] reusable workflow; environments are managed by callers
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
if: ${{ !inputs.trusted-publishing && github.event.workflow_run.conclusion == 'success' }}
steps:
# persisted credentials are required here - discourse/publish-rubygems-action's entrypoint.sh runs
# `rake release`, which does a raw `git push` and never re-authenticates from GITHUB_TOKEN itself; it
Expand All @@ -28,9 +51,159 @@ jobs:
if: ${{ steps.tag-and-push-gem.outputs.new_version == 'true' }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# check, release and github_release are the trusted-publishing path. The API-key path above decides
# by the git tag, so a tag pushed before a failed `gem push` blocks that version for good. These jobs
# decide by RubyGems instead, and re-running a failed run skips whatever is already done.
check:
name: Check whether a release is needed
# A fork's pull request can come from a branch named main, so require a push to this repo's default branch.
if: >-
inputs.trusted-publishing &&
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'push' &&
github.event.workflow_run.head_repository.full_name == github.repository &&
github.event.workflow_run.head_branch == github.event.repository.default_branch
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
gem: ${{ steps.check.outputs.gem }}
version: ${{ steps.check.outputs.version }}
sha: ${{ steps.check.outputs.sha }}
release: ${{ steps.check.outputs.release }}
github_release: ${{ steps.check.outputs.github_release }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.event.workflow_run.head_sha }}
persist-credentials: false
- uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
with:
ruby-version: ${{ inputs.ruby-version }}
bundler: none
- name: Check whether this version is on RubyGems and has a GitHub release
id: check
env:
GH_TOKEN: ${{ github.token }}
SHA: ${{ github.event.workflow_run.head_sha }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
run: |
shopt -s nullglob
gemspecs=(*.gemspec)
if [ "${#gemspecs[@]}" -ne 1 ]; then
echo "::error::Expected one .gemspec at the repository root, found ${#gemspecs[@]}"
exit 1
fi
ruby -e 'spec = Gem::Specification.load(ARGV[0]) or abort("Could not load #{ARGV[0]}")
File.write(ARGV[1], "#{spec.name} #{spec.version}\n")' "${gemspecs[0]}" "$RUNNER_TEMP/gem"
read -r gem version < "$RUNNER_TEMP/gem"
if ! [[ "$gem" =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ && "$version" =~ ^[0-9]+(\.[0-9A-Za-z]+)*$ ]]; then
echo "::error::Unexpected gem name or version in ${gemspecs[0]}: '$gem' '$version'"
exit 1
fi
{
echo "gem=$gem"
echo "version=$version"
echo "sha=$SHA"
} >> "$GITHUB_OUTPUT"

head=$(gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$DEFAULT_BRANCH" --jq .object.sha)
if [ "$head" != "$SHA" ]; then
echo "::notice::$DEFAULT_BRANCH has moved on from $SHA to $head, so the CD run that follows CI on $head does the release."
echo "release=false" >> "$GITHUB_OUTPUT"
echo "github_release=false" >> "$GITHUB_OUTPUT"
exit 0
fi

status=$(curl -sS --retry 3 -o /dev/null -w '%{http_code}' "https://rubygems.org/api/v2/rubygems/$gem/versions/$version.json")
case "$status" in
200) release=false; echo "$gem $version is already on RubyGems." ;;
404) release=true; echo "$gem $version isn't on RubyGems yet, so this run releases it." ;;
*) echo "::error::RubyGems returned HTTP $status for $gem $version"; exit 1 ;;
esac

if lookup=$(gh api "repos/$GITHUB_REPOSITORY/releases/tags/v$version" 2>&1); then
github_release=false
elif grep -q 'HTTP 404' <<<"$lookup"; then
github_release=true
echo "v$version has no GitHub release yet."
else
echo "::error::Couldn't check for a v$version GitHub release: $lookup"
exit 1
fi
echo "release=$release" >> "$GITHUB_OUTPUT"
echo "github_release=$github_release" >> "$GITHUB_OUTPUT"

release: # zizmor: ignore[excessive-permissions] inherits the caller's permissions on purpose, see below
name: Release
needs: check
if: needs.check.outputs.release == 'true'
runs-on: ubuntu-latest
environment:
name: rubygems.org
url: https://rubygems.org/gems/${{ needs.check.outputs.gem }}
# No permissions block, so the job gets the caller's: contents: write to push the tag, and
# id-token: write for trusted publishing. GitHub checks every called job's permissions against
# the caller's when the run starts, skipped jobs included, so requesting id-token: write here
# would fail the run in every repo that hasn't opted in.
steps:
- name: Check that the caller granted id-token write
run: |
if [ -z "${ACTIONS_ID_TOKEN_REQUEST_URL:-}" ]; then
echo "::error::Trusted publishing needs id-token: write. Grant it in the permissions of the job that calls shared-config's cd.yml."
exit 1
fi
# `rake release` pushes the current branch along with the tag, so this has to be a branch checkout
# rather than a detached HEAD at the tested commit. The push needs no persisted credentials:
# release-gem puts the token in git's credential cache for it.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ github.event.repository.default_branch }}
persist-credentials: false
- name: Confirm the branch is still the commit CI tested
env:
SHA: ${{ needs.check.outputs.sha }}
run: |
head=$(git rev-parse HEAD)
if [ "$head" != "$SHA" ]; then
echo "::error::The default branch moved from $SHA to $head after the check job ran. The CD run that follows CI on $head releases this version."
exit 1
fi
# No bundler cache: a job that publishes the gem shouldn't restore one.
- uses: ruby/setup-ruby@7f797815d8adceb1c412e7abefdd1ab4abc47a5f # v1.335.0
with:
ruby-version: ${{ inputs.ruby-version }}
- run: bundle install
# Tags v<version>, pushes the tag and publishes the gem. It tags as the author of the last commit.
- uses: rubygems/release-gem@7f9650160c1a4e7989fdc9855807bdbd421d8b6b # v1.4.1
with:
attestations: ${{ inputs.attestations }}

# Its own job, so re-running a failed run can still create the release after the gem has shipped.
github_release:
name: Create GitHub release
needs: [check, release]
if: >-
always() && needs.check.result == 'success' && needs.check.outputs.github_release == 'true' &&
(needs.release.result == 'success' || needs.release.result == 'skipped')
runs-on: ubuntu-latest
permissions:
contents: write # create the GitHub release
steps:
- name: Create GitHub release
env:
GH_TOKEN: ${{ github.token }}
VERSION: ${{ needs.check.outputs.version }}
# RubyGems treats any version with a letter in it as a prerelease.
run: |
prerelease=()
if [[ "$VERSION" == *[A-Za-z]* ]]; then prerelease=(--prerelease); fi
gh release create "v$VERSION" --repo "$GITHUB_REPOSITORY" --verify-tag --generate-notes "${prerelease[@]}"

notify_on_failure: # zizmor: ignore[secrets-outside-env] reusable workflow; environments are managed by callers
runs-on: ubuntu-latest
needs: [deploy]
needs: [deploy, check, release, github_release]
if: ${{ failure() && github.ref == 'refs/heads/main' }}
steps:
- uses: slackapi/slack-github-action@dcb1066f776dd043e64d0e8ba94ca15cc7e1875d # v4.0.0
Expand Down
56 changes: 53 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ These workflows are called from individual gem repos via `uses: rubyatscale/shar
| Workflow | Description |
|----------|-------------|
| **CI** (`ci.yml`) | Runs tests across Ruby 3.3–4.0, Sorbet type checking, and linting (RuboCop). Test and linter commands are configurable via inputs. |
| **CD** (`cd.yml`) | Publishes the gem to RubyGems and creates a GitHub Release on successful main builds. |
| **CD** (`cd.yml`) | Publishes the gem to RubyGems and creates a GitHub Release on successful main builds, with an API key or [trusted publishing](#cd). |
| **Stale** (`stale.yml`) | Marks issues and PRs as stale after 180 days of inactivity, then closes them after 7 more days. |
| **Triage** (`triage.yml`) | Labels new issues with `triage`. |
| **CodeQL** (`codeql.yml`) | Runs [CodeQL](https://codeql.github.com) analysis for the given languages and uploads the results to the Security tab. |
Expand Down Expand Up @@ -49,6 +49,56 @@ jobs:
| `test-command` | `bundle exec rspec` | Command to run tests |
| `linter-command` | `bundle exec rubocop` | Command to run the linter |

### CD

Bumping the gem's version is the release. Once CI passes on main, CD publishes the version if it isn't on RubyGems yet, tags it `v<version>`, and creates its GitHub release with generated notes.

By default it publishes with the org-wide `RUBYGEMS_API_KEY`. Set `trusted-publishing: true` to publish with [RubyGems trusted publishing](https://guides.rubygems.org/trusted-publishing/) instead, which needs no stored key:

```yaml
# .github/workflows/cd.yml
name: CD

on: # zizmor: ignore[dangerous-triggers] deploy-after-CI-passes is the standard rubyatscale release pattern; the called workflow only tags/publishes on main
workflow_run:
workflows: [CI]
types: [completed]
branches: [main]

jobs:
call-workflow-from-shared-config:
permissions:
contents: write
id-token: write
uses: rubyatscale/shared-config/.github/workflows/cd.yml@main # zizmor: ignore[unpinned-uses,secrets-inherit] internal reusable workflow tracked at @main by convention so shared-config updates propagate automatically; environments are managed by callers
secrets: inherit
with:
trusted-publishing: true
```

`workflows:` names the CI workflow's `name:`, not its filename.

Before turning it on, a gem owner adds a GitHub Actions trusted publisher at `https://rubygems.org/gems/<gem>/trusted_publishers`:

| Field | Value |
|-------|-------|
| Repository owner | `rubyatscale` |
| Repository name | the gem's repo, e.g. `packs` |
| Workflow filename | `cd.yml` |
| Environment | `rubygems.org` |
| Workflow repository owner | `rubyatscale` |
| Workflow repository name | `shared-config` |

The workflow filename is this repo's `cd.yml`, the workflow that does the publishing, so the workflow repository fields are what tie the publisher to it. The release job runs in the caller's `rubygems.org` environment, which GitHub creates on the first release. A release tags as the author of the last commit.

The release job takes the caller's permissions instead of declaring its own, because GitHub fails the whole run when any called job asks for more than the caller grants, even a job that would be skipped. So grant `id-token: write` only together with `trusted-publishing: true`. Without it, the release job stops with an error that says so.

| Input | Default | Description |
|-------|---------|-------------|
| `trusted-publishing` | `false` | Publish with trusted publishing (`true`), or with `RUBYGEMS_API_KEY` (`false`) |
| `ruby-version` | `3` | With trusted publishing, the Ruby that reads the gemspec and runs `rake release`. The latest 3.x, like the `ruby:3` image the API-key path runs in |
| `attestations` | `true` | With trusted publishing, attach [Sigstore](https://www.sigstore.dev) attestations to the gem. Sigstore's public transparency log records the repository, workflow and commit of each one |

### CodeQL

The job requests `actions: read`, `contents: read` and `security-events: write`, so the calling job must grant all three.
Expand Down Expand Up @@ -118,8 +168,8 @@ Callers track `@main`, so when this repo upgrades zizmor-action, new audits can

The **CD** workflow requires the following secrets in the calling repo:

- `GUSTO_GIT_EMAIL` / `GUSTO_GIT_NAME` — Git identity for tagging
- `RUBYGEMS_API_KEY` — API key for publishing to RubyGems
- `GUSTO_GIT_EMAIL` / `GUSTO_GIT_NAME` — Git identity for tagging (API-key publishing only)
- `RUBYGEMS_API_KEY` — API key for publishing to RubyGems (API-key publishing only)
- `SLACK_WEBHOOK_URL` — Incoming webhook URL for failure notifications (used by both CI and CD)

## Security
Expand Down
Loading