Files

159 lines
12 KiB
Markdown

# Setting up GitHub for releases
Releases go through CI and the co-signer only once GitHub is set up for them, and until then `scripts/publish.sh --local` publishes them from the maintainer's machine, as [Releasing](maintaining.md#releasing) says. This page is the setup, in the order to do it. Each step gives the clicks in GitHub's settings and, where GitHub's API can do it, the `gh api` command that does the same. Read each command before you run it, check it against GitHub's documentation, and replace each `<…>` with its value. The commands assume the repos are `devvmhq/devvm` and `devvmhq/homebrew-tap`, under the organization `devvmhq`.
Step 4 needs your user ID, which `gh api user --jq .id` prints, and step 7 the App ID from step 2.
## 1. The release keys
devvm checks a release with three release keys once they're built into it ([Release keys](maintaining.md#release-keys)). Until you make them, `packaging/keys` has none and `install.sh`'s three key lines are empty, so devvm and the install script check each release with the old release key alone, and `scripts/publish.sh --local` publishes it signed with that key, as before. CI's Release workflow, `scripts/publish.sh` without `--local`, `--sign-legacy` and the co-signer refuse to run without the keys, and each refusal points here. Make each key where its holder keeps it, and never two on one machine:
```sh
minisign -G -W -p packaging/keys/release-ci.pub -s ci.key # CI's, with no password: step 4 puts it in GitHub
sudo minisign -G -W -p release-cosign.pub -s /etc/devvm-cosign/release.key # on the co-signer's machine (step 3)
minisign -G -p release-recovery.pub -s /path/on/an/offline/drive/recovery.key # the recovery key, with a password, kept offline
```
Commit the three `.pub` files to `packaging/keys/` as `release-ci.pub`, `release-cosign.pub` and `release-recovery.pub`, and copy each key into `install.sh`'s `release_ci_key`, `release_cosign_key` and `release_recovery_key` lines, all in one commit, because a devvm with one or two of the keys can't upgrade. `TestReleaseKeys` checks that the keys are all three, each a different minisign key and none of them the old release key, and `TestInstallScriptKeys` checks that `install.sh`'s lines match them. `scripts/release.sh --check-tag` refuses a tag with one or two of the keys, a key marked `PLACEHOLDER` in its comment, or lines in `install.sh` that differ. The first release built with the keys still carries the old key's signature, so every devvm before it, which has no release keys, upgrades to it. Keep `ci.key` only until step 4, then delete it, because a lost CI key is replaced by a release that the other two keys sign.
## 2. The GitHub App
The co-signer calls GitHub as an app of its own, never with your GitHub token, and the app can read the repo, write releases and the tap, and read workflow runs' artifacts, and nothing else ([The GitHub App](cosigner.md#the-github-app)).
1. Open `https://github.com/organizations/devvmhq/settings/apps/new` (for a user account, Settings, then Developer settings, then GitHub Apps, then New GitHub App).
2. Name it, such as `devvm-cosign`, with `https://devvm.dev` as its homepage, and clear **Webhook: Active**.
3. Under **Repository permissions**, set **Contents** to Read and write and **Actions** to Read-only. GitHub adds **Metadata**, Read-only. Leave every other permission, and every organization and account permission, at No access.
4. Under **Where can this GitHub App be installed?**, choose **Only on this account**, then **Create GitHub App**.
5. Note the **App ID** and the **Client ID** at the top of its page: the config's `[app] id` takes either, and step 7 needs the App ID.
6. Under **Private keys**, choose **Generate a private key**, and copy the `.pem` file to the co-signer's machine as `/etc/devvm-cosign/app.pem`, root's, mode 0600. Delete the downloaded copy.
7. Choose **Install App**, then the `devvmhq` account, then **Only select repositories**, and select `devvm` and `homebrew-tap` alone. The installation's ID is the number at the end of the page's URL afterwards, `…/installations/<ID>`, which is the config's `[app] installation`.
Check it:
```sh
gh api orgs/devvmhq/installations --jq '.installations[] | {id, app_slug, repository_selection, permissions}'
gh api orgs/devvmhq/installations/<installation ID>/repositories --jq '.repositories[].full_name' # devvm and homebrew-tap alone
```
## 3. The co-signer's machine
Set it up as [Setting it up](cosigner.md#setting-it-up) says, with the release key from step 1 and the app's key from step 2, then step 7 below for its notify command. Its `init` records what GitHub has published, so run `devvm-cosign@init` once the setup on this page is done and before the first release goes through it.
## 4. The release environment
CI's Release workflow runs in the environment `release`, which waits for your approval on each run and holds CI's release key and the tag signers ([CI's release workflow](maintaining.md#cis-release-workflow)). Only runs for `v*` tags may use it, and administrators can't skip the approval.
1. In `devvm`'s **Settings**, open **Environments**, then **New environment**, and name it `release`.
2. Tick **Required reviewers** and add yourself. Leave **Prevent self-review** clear, because you push the tags you approve. Approve only a run for a tag you just pushed: the run is that tag's own workflow, so approving it gives CI's release key to the tag's code, and the tag's signature check runs inside it.
3. Clear **Allow administrators to bypass configured protection rules**.
4. Under **Deployment branches and tags**, choose **Selected branches and tags**, then **Add deployment branch or tag rule**, choose **Tag**, and enter `v*`.
5. Under **Environment secrets**, add `RELEASE_CI_KEY`, the whole of `ci.key` from step 1, both lines, then delete `ci.key`.
6. Under **Environment variables**, add `RELEASE_TAG_SIGNERS`: the SSH public keys that sign release tags, one per line, each after a name, as git's allowed signers file has them, such as `cj ssh-ed25519 AAAA…`. The key is the one `packaging/release-signer` pins the fingerprint of, and `git config user.signingkey` names.
The same with `gh`:
```sh
gh api --method PUT repos/devvmhq/devvm/environments/release --input - <<'EOF'
{
"wait_timer": 0,
"prevent_self_review": false,
"can_admins_bypass": false,
"reviewers": [{ "type": "User", "id": <your user ID> }],
"deployment_branch_policy": { "protected_branches": false, "custom_branch_policies": true }
}
EOF
gh api --method POST repos/devvmhq/devvm/environments/release/deployment-branch-policies -f name='v*' -f type=tag
gh secret set RELEASE_CI_KEY --env release --repo devvmhq/devvm < ci.key
printf 'cj %s\n' "$(cat ~/.ssh/<your signing key>.pub)" | gh variable set RELEASE_TAG_SIGNERS --env release --repo devvmhq/devvm
```
Check it with `gh api repos/devvmhq/devvm/environments/release`: `can_admins_bypass` is false, `protection_rules` lists you as the reviewer, and `gh api repos/devvmhq/devvm/environments/release/deployment-branch-policies` lists `v*` as a tag.
## 5. The tag ruleset
Only you can create, move or delete a `v*` tag, so nobody else can start a release build, or retag a release. GitHub's rulesets take roles, teams and apps as bypasses, not a single user, so the bypass is the repository's admin role, which is you while you're the only admin. Check that first, `gh api repos/devvmhq/devvm/collaborators --jq '.[] | select(.permissions.admin) | .login'`, which lists organization owners too, and check `gh api repos/devvmhq/devvm/rulesets` for a tag ruleset there already, to edit rather than add.
1. In `devvm`'s **Settings**, open **Rules**, then **Rulesets**, then **New ruleset**, then **New tag ruleset**.
2. Name it `Release tags`, and set **Enforcement status** to Active.
3. Under **Bypass list**, add **Repository admin**, with **Always allow**.
4. Under **Target tags**, add the pattern `v*`.
5. Under **Tag rules**, tick **Restrict creations**, **Restrict updates**, **Restrict deletions** and **Block force pushes**, then **Create**.
The same with `gh`:
```sh
gh api --method POST repos/devvmhq/devvm/rulesets --input - <<'EOF'
{
"name": "Release tags",
"target": "tag",
"enforcement": "active",
"conditions": { "ref_name": { "include": ["refs/tags/v*"], "exclude": [] } },
"rules": [{ "type": "creation" }, { "type": "update" }, { "type": "deletion" }, { "type": "non_fast_forward" }],
"bypass_actors": [{ "actor_id": 5, "actor_type": "RepositoryRole", "bypass_mode": "always" }]
}
EOF
```
`actor_id` 5 is the repository admin role.
## 6. Immutable releases
Once a release is published, nobody can change its files or move its tag, so the files the co-signer checked are the ones users download. A draft can still change, which is how the co-signer and `--sign-legacy` add their files before it's published.
In `devvm`'s **Settings**, under **General**, then **Releases**, tick **Enable release immutability**. With `gh`, `gh api --method PUT repos/devvmhq/devvm/immutable-releases`, and `gh api repos/devvmhq/devvm/immutable-releases` checks it.
## 7. The tap's ruleset
Only the co-signer's app writes the tap's `main`, so nobody else can point Homebrew at other archives.
1. In `homebrew-tap`'s **Settings**, open **Rules**, then **Rulesets**, then **New ruleset**, then **New branch ruleset**.
2. Name it `main`, and set **Enforcement status** to Active.
3. Under **Bypass list**, add the app from step 2, with **Always allow**.
4. Under **Target branches**, choose **Include default branch**.
5. Under **Branch rules**, tick **Restrict updates**, **Restrict deletions** and **Block force pushes**, then **Create**.
The same with `gh`, with the App ID from step 2:
```sh
gh api --method POST repos/devvmhq/homebrew-tap/rulesets --input - <<'EOF'
{
"name": "main",
"target": "branch",
"enforcement": "active",
"conditions": { "ref_name": { "include": ["~DEFAULT_BRANCH"], "exclude": [] } },
"rules": [{ "type": "update" }, { "type": "deletion" }, { "type": "non_fast_forward" }],
"bypass_actors": [{ "actor_id": <App ID>, "actor_type": "Integration", "bypass_mode": "always" }]
}
EOF
```
To fix the tap by hand, turn the ruleset's enforcement off, make the change, and turn it on again, which admins can do. The co-signer raises an alert for a tap formula it didn't write.
## 8. Where the co-signer tells you
A run that fails, a refusal and an alert reach you through the config's `notify` command ([Refusals and alerts](cosigner.md#refusals-and-alerts)). A script that posts over HTTPS works under the co-signer's units, such as this one to ntfy, saved as `/usr/local/bin/devvm-cosign-notify`, root's, mode 0755, with `notify = ["/usr/local/bin/devvm-cosign-notify"]` in the config:
```sh
#!/bin/sh
# What failed comes on standard input.
exec curl -fsS --max-time 30 -H 'Title: devvm co-signer' --data-binary @- https://ntfy.sh/<a long random topic>
```
An ntfy.sh topic is readable by anyone who knows its name, so use a long random one, or a server of your own with an ntfy access token. GitHub tells you the rest: it emails you when the Release workflow waits for your approval, and when a run fails.
## 9. The first release through CI
Make it a release candidate, so a mistake costs little:
```sh
git tag -s v<version> && scripts/publish.sh # push main and the tag
gh run watch --repo devvmhq/devvm # approve the run in the Actions tab first
scripts/publish.sh --sign-legacy # once the co-signer's draft is there
gh release view v<version> --repo devvmhq/devvm # published, with SHA256SUMS and its three signatures
```
The three signatures are CI's, the co-signer's and the old key's, `SHA256SUMS.ci.minisig`, `SHA256SUMS.cosign.minisig` and `SHA256SUMS.minisig`.
Then `scripts/publish.sh --local` isn't needed any more, and a release it published would be one the co-signer alerts on.