diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2a673ac..44dfc03 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,3 +67,32 @@ jobs: toolkit-ref: ${{ github.sha }} reminders: | This is a test run. + + release-files: + runs-on: ubuntu-latest + timeout-minutes: 5 + steps: + - name: Make test files + run: | + mkdir dist + echo windows > dist/example-windows-x64.zip + echo linux > dist/example-linux-x64.AppImage + + - uses: actions/upload-artifact@v4 + with: + name: release-test-files + path: dist + retention-days: 1 + + release-dry-run: + needs: release-files + permissions: + contents: write + uses: ./.github/workflows/release.yml + with: + tag: v0.0.0-testing + artifacts: release-test-* + config: release-notes/example.toml + notes: A dry run from the toolkit's CI. + dry-run: true + toolkit-ref: ${{ github.sha }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..e6cb5cf --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,141 @@ +name: Release + +# Reusable: turns a tag into a GitHub release. It gathers the files your +# earlier jobs uploaded as artifacts, writes SHA256SUMS, writes the notes +# (with release_notes.py when you give a config) and publishes. A tag ending +# in -testing becomes a pre-release. dry-run builds everything but publishes +# nothing. + +on: + workflow_call: + inputs: + tag: + description: "The tag to release (default: the tag that started the workflow)" + type: string + default: "" + artifacts: + description: "Which of this run's artifacts to publish (a name pattern; empty: none)" + type: string + default: "*" + config: + description: "Your release-notes TOML (see release-notes/example.toml); empty: plain notes" + type: string + default: "" + whats-new: + description: "Your what's-new file; {version} is filled in. A missing file is skipped." + type: string + default: "docs/release-notes/{version}.md" + notes: + description: "Extra text for a test build's notes" + type: string + default: "" + prerelease: + description: "Make a pre-release even without -testing in the tag" + type: boolean + default: false + dry-run: + description: "Build the files and notes, publish nothing" + type: boolean + default: false + toolkit-ref: + description: "The toolkit version to run the scripts from" + type: string + default: v0 + +permissions: + contents: write + +jobs: + release: + runs-on: ubuntu-latest + timeout-minutes: 20 + env: + TAG: ${{ inputs.tag || github.ref_name }} + steps: + - uses: actions/checkout@v4 + + - uses: actions/checkout@v4 + with: + repository: Horrible-Dev/toolkit + ref: ${{ inputs.toolkit-ref }} + path: .toolkit + + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Gather the files + if: ${{ inputs.artifacts != '' }} + uses: actions/download-artifact@v4 + with: + pattern: ${{ inputs.artifacts }} + merge-multiple: true + path: ${{ runner.temp }}/dist + + - name: Checksums + working-directory: ${{ runner.temp }} + run: | + mkdir -p dist + cd dist + files=$(find . -maxdepth 1 -type f ! -name SHA256SUMS -printf '%P\n' | sort) + if [ -n "$files" ]; then + # shellcheck disable=SC2086 + sha256sum $files > SHA256SUMS + cat SHA256SUMS + else + echo "No files to publish." + fi + + - name: Notes + env: + CONFIG: ${{ inputs.config }} + NEW_FILE_PATTERN: ${{ inputs.whats-new }} + EXTRA: ${{ inputs.notes }} + FORCE_PRERELEASE: ${{ inputs.prerelease }} + COMMIT: ${{ github.sha }} + REPO_NAME: ${{ github.event.repository.name }} + run: | + version=${TAG#v} + version=${version%-testing} + testing=false + case "$TAG" in *-testing) testing=true ;; esac + [ "$FORCE_PRERELEASE" = true ] && testing=true + new_file=${NEW_FILE_PATTERN//\{version\}/$version} + notes="$RUNNER_TEMP/notes.md" + if [ -n "$CONFIG" ]; then + set -- --config "$CONFIG" --version "$version" --tag "$TAG" --commit "$COMMIT" \ + --dir "$RUNNER_TEMP/dist" --whats-new "$new_file" --output "$notes" + [ "$testing" = true ] && set -- "$@" --testing --notes "$EXTRA" + python3 .toolkit/release-notes/release_notes.py "$@" + name=$(python3 -c 'import sys, tomllib; print(tomllib.load(open(sys.argv[1], "rb"))["name"])' "$CONFIG") + else + { + [ "$testing" = true ] && printf '**Test build.** It might break, and the next test build replaces it.\n\n' + [ -n "$EXTRA" ] && printf '%s\n\n' "$EXTRA" + [ -f "$new_file" ] && { cat "$new_file"; printf '\n\n'; } + printf 'Commit: %s\n' "$COMMIT" + } > "$notes" + name=$REPO_NAME + fi + { + echo "TITLE=$name ${TAG#v}" + echo "TESTING=$testing" + } >> "$GITHUB_ENV" + cat "$notes" >> "$GITHUB_STEP_SUMMARY" + + - name: Publish + if: ${{ !inputs.dry-run }} + env: + GH_TOKEN: ${{ github.token }} + run: | + set -- "$TAG" --title "$TITLE" --notes-file "$RUNNER_TEMP/notes.md" --verify-tag + [ "$TESTING" = true ] && set -- "$@" --prerelease + shopt -s nullglob + files=("$RUNNER_TEMP"/dist/*) + gh release create "$@" "${files[@]}" + + - name: Dry run + if: ${{ inputs.dry-run }} + run: | + echo "Dry run: would publish '$TITLE' (pre-release: $TESTING) with:" + ls -l "$RUNNER_TEMP/dist" diff --git a/README.md b/README.md index 22a8cb5..85c856b 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,46 @@ The workflow's own token can only read the calling repository. To cover others, Run the script by hand with `python3 housekeeping/housekeeping.py --repo OWNER/app --repo OWNER/website`. +## Releases + +The reusable release workflow turns a tag into a GitHub release: + +1. It gathers the files your build jobs uploaded as artifacts. +2. It writes `SHA256SUMS` for them. +3. It writes the notes. With a `config`, that's release_notes.py; without one, plain notes from your what's-new file. +4. It publishes the release. A tag ending in `-testing` becomes a pre-release. + +```yaml +# .github/workflows/release.yml +name: Release +on: + push: + tags: ["v*"] +permissions: + contents: read +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: make dist + - uses: actions/upload-artifact@v4 + with: + name: dist-linux + path: dist/ + release: + needs: build + permissions: + contents: write + uses: Horrible-Dev/toolkit/.github/workflows/release.yml@v0 + with: + artifacts: dist-* + config: .github/release-notes.toml + whats-new: docs/release-notes/{version}.md +``` + +`dry-run: true` builds the files and notes and shows them in the run's summary, without publishing. `prerelease: true` makes any tag a pre-release. `notes` adds a line to a test build's notes. + ## Credits Parts of the toolkit are adapted from ChupathingyCE's release and housekeeping tools.