Skip to content

Commit 42b6748

Browse files
committed
Rework release-script to use GitHub releases
Instead of committing builds of the installer to the prerelease branch using a cronjob, create draft GitHub releases on workflow dispatch. The release includes the nix-installer.sh script to allow running `curl https://github.com/NixOS/experimental-nix-installer/releases/download/v0.27.0/nix-installer.sh | bash` Or to get the latest release: `curl https://github.com/NixOS/experimental-nix-installer/releases/latest/download/nix-installer.sh | bash` Alternatively, binaries for each system can be downloaded from the release directly. The version number is substituted in the nix-installer.sh file included in the release so that we don't have to bump it every time we do a release. The release job will error if there's a rev mismatch between the flake built by Hydra and the rev the job is run from. This means we'll only be able to run the job off of main. For testing, a Hydra eval ID can be specified, which skips the rev check. This will create a draft release using the artifacts from that Hydra eval even if there is a revision and/or version mismatch.
1 parent b8ed72a commit 42b6748

File tree

3 files changed

+82
-18
lines changed

3 files changed

+82
-18
lines changed

.github/workflows/release-script.yml

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
name: Generate Installer Script
22

33
on:
4-
schedule:
5-
- cron: '10 * * * *'
64
workflow_dispatch: # Allows manual triggering of the workflow
5+
inputs:
6+
testing_hydra_eval_id:
7+
description: "Eval ID of Hydra job to use artifacts from for testing"
8+
required: false
9+
default: ""
710

811
jobs:
912
build:
@@ -12,13 +15,8 @@ jobs:
1215
- name: Checkout
1316
uses: actions/checkout@v4
1417
- uses: cachix/install-nix-action@v25
15-
- name: Download Installer
16-
run: nix-shell -p python3Packages.requests --run "python assemble_installer.py" -I nixpkgs=channel:nixos-23.11
17-
- uses: stefanzweifel/git-auto-commit-action@v5
18-
with:
19-
commit_message: "Update installer script"
20-
commit_user_name: "GitHub Actions"
21-
branch: prerelease
22-
# assemble_installer.py already does a fetch and checkout
23-
skip_fetch: true
24-
skip_checkout: true
18+
- name: Create draft release
19+
# The script also depends on gh and git but those are both pre-installed on the runner
20+
run: nix-shell -p python3Packages.requests --run "python assemble_installer.py ${{ github.event.inputs.testing_hydra_eval_id }}" -I nixpkgs=channel:nixos-23.11
21+
env:
22+
GH_TOKEN: ${{ github.token }}

assemble_installer.py

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
1+
import os
12
import requests
23
import subprocess
34
import shutil
45
import sys
6+
import tempfile
7+
import tomllib
8+
9+
from string import Template
10+
11+
# A Hydra eval id must be passed as the argument to this script or an empty
12+
# string to use latest eval on Hydra
13+
# TODO: using an empty string is not the cleanest
14+
# TODO: print script usage
15+
eval_id = sys.argv[1]
516

617
response = requests.get('https://hydra.nixos.org/jobset/experimental-nix-installer/experimental-installer/evals', headers={'Accept': 'application/json'})
18+
evals = response.json()['evals']
19+
20+
if eval_id is not None and eval_id != "":
21+
eval_id_int = int(eval_id)
22+
ids = [eval['id'] for eval in evals]
23+
hydra_eval = next( eval for eval in evals if eval['id'] == eval_id_int )
24+
else:
25+
hydra_eval = evals[0]
726

8-
hydra_eval = response.json()['evals'][0]
27+
rev = subprocess.run(
28+
["git", "rev-parse", "HEAD"], stdout=subprocess.PIPE, check=True, text=True
29+
).stdout.strip()
30+
31+
if not rev in hydra_eval["flake"]:
32+
raise RuntimeError(
33+
f"Expected flake with rev {rev} but found flake {hydra_eval['flake']}"
34+
)
935

1036
installers = []
1137

@@ -22,10 +48,50 @@
2248
subprocess.call(f"nix-store -r {installer_url}", shell=True)
2349
installers.append((installer_url, system))
2450
else:
51+
print(
52+
f"Build {build_id} not finished. Check status at https://hydra.nixos.org/eval/{hydra_eval['id']}#tabs-unfinished"
53+
)
2554
sys.exit(0)
2655

27-
subprocess.run(["git", "fetch", "origin", "prerelease"], check=True)
28-
subprocess.run(["git", "checkout", "-b", "prerelease", "origin/prerelease"], check=True)
56+
with open("Cargo.toml", "rb") as f:
57+
cargo_toml = tomllib.load(f)
58+
version = cargo_toml["package"]["version"]
59+
60+
with tempfile.TemporaryDirectory() as tmpdirname:
61+
release_files = []
62+
for installer_url, system in installers:
63+
installer_file = f"{tmpdirname}/nix-installer-{system}"
64+
release_files.append(installer_file)
65+
print(f"Copying {installer_url} to {installer_file}")
66+
shutil.copy(f"{installer_url}/bin/nix-installer", installer_file)
67+
68+
# Subsitute version in nix-installer.sh
69+
original_file = "nix-installer.sh"
70+
71+
with open(original_file, "r") as nix_installer_sh:
72+
nix_installer_sh_contents = nix_installer_sh.read()
73+
74+
template = Template(nix_installer_sh_contents)
75+
updated_content = template.safe_substitute(assemble_installer_templated_version=version)
76+
77+
# Write the modified content to the output file
78+
substituted_file=f"{tmpdirname}/nix-installer.sh"
79+
with open(substituted_file, "w", encoding="utf-8") as output_file:
80+
output_file.write(updated_content)
81+
release_files.append(substituted_file)
2982

30-
for installer_url, system in installers:
31-
shutil.copy(f"{installer_url}/bin/nix-installer", f"nix-installer-{system}")
83+
subprocess.run(
84+
[
85+
"gh",
86+
"release",
87+
"create",
88+
"--notes",
89+
f"Release experimental nix installer v{version}",
90+
"--title",
91+
f"v{version}",
92+
"--draft",
93+
version,
94+
*release_files,
95+
],
96+
check=True,
97+
)

nix-installer.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fi
2525
set -u
2626

2727
# If NIX_INSTALLER_FORCE_ALLOW_HTTP is unset or empty, default it.
28-
NIX_INSTALLER_BINARY_ROOT="${NIX_INSTALLER_BINARY_ROOT:-https://raw.githubusercontent.com/NixOS/experimental-nix-installer/prerelease}"
28+
NIX_INSTALLER_BINARY_ROOT="${NIX_INSTALLER_BINARY_ROOT:-https://github.com/NixOS/experimental-nix-installer/releases/download/$assemble_installer_templated_version}"
2929

3030
main() {
3131
downloader --check

0 commit comments

Comments
 (0)