diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..be8cb95 --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,24 @@ +name: Check +on: + pull_request: +permissions: + contents: read +concurrency: + group: check-${{ github.event.pull_request.number }} + cancel-in-progress: true +jobs: + test: + runs-on: macos-15-intel # arm64 Mac runners are short; queued jobs are dropped after 15 min + timeout-minutes: 15 + env: + DEVELOPER_DIR: /Applications/Xcode_16.4.app/Contents/Developer + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + - name: Source and release checks + run: | + python3 scripts/check-release.py + python3 -m unittest discover -s Tests/ReleaseTests + - name: Host tests + run: swift test -j 2 diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..b2f87ba --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,73 @@ +name: Publish +# A new version in VERSION releases itself: on every push to main, if its +# vX.Y.Z tag does not exist yet, the Release workflow builds this commit, +# tags it and publishes the release. Otherwise nothing runs. +on: + push: + branches: [main] +permissions: + contents: read +concurrency: + group: publish-main + cancel-in-progress: false +jobs: + version: + runs-on: ubuntu-24.04 + timeout-minutes: 3 + outputs: + tag: ${{ steps.check.outputs.tag }} + steps: + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + sparse-checkout: VERSION + sparse-checkout-cone-mode: false + - name: Is VERSION released? + id: check + env: + GH_TOKEN: ${{ github.token }} + run: | + version=$(tr -d '[:space:]' < VERSION) + if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "VERSION must be X.Y.Z, not '$version'." >&2 + exit 1 + fi + tag="v$version" + gh api "repos/$GITHUB_REPOSITORY/git/matching-refs/tags/$tag" --jq '.[].ref' > "$RUNNER_TEMP/refs.txt" + if grep -Fxq "refs/tags/$tag" "$RUNNER_TEMP/refs.txt"; then + echo "$tag exists; nothing to release." + tag='' + fi + echo "tag=$tag" >> "$GITHUB_OUTPUT" + release: + needs: version + if: needs.version.outputs.tag != '' + uses: ./.github/workflows/release.yml + with: + version: ${{ needs.version.outputs.tag }} + from_main: true + permissions: + contents: write + id-token: write + attestations: write + artifact-metadata: write + # Fails the run when a release was due but any of its jobs failed, was + # cancelled or skipped, or the release is not published. + published: + needs: [version, release] + if: always() && needs.version.outputs.tag != '' + runs-on: ubuntu-24.04 + timeout-minutes: 2 + env: + GH_TOKEN: ${{ github.token }} + RESULT: ${{ needs.release.result }} + TAG: ${{ needs.version.outputs.tag }} + steps: + - run: | + if [[ "$RESULT" != success ]]; then + echo "Release $TAG: $RESULT; nothing published." >&2 + exit 1 + fi + gh release view "$TAG" --repo "$GITHUB_REPOSITORY" --json isDraft,isPrerelease,assets \ + --jq '"draft=\(.isDraft) prerelease=\(.isPrerelease) assets=\(.assets | length)"' | tee "$RUNNER_TEMP/release.txt" + grep -q '^draft=false prerelease=false assets=[1-9]' "$RUNNER_TEMP/release.txt" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c54d214..f1788e2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -8,6 +8,15 @@ on: description: Package version (private runs retain a draft release) required: true default: preview + workflow_call: # from publish.yml when VERSION changes on main + inputs: + version: + description: Release tag (vX.Y.Z) to create at this commit + type: string + required: true + from_main: + type: boolean + default: false permissions: contents: read concurrency: @@ -25,7 +34,8 @@ jobs: release_kind: ${{ steps.plan.outputs.release_kind }} attest: ${{ steps.plan.outputs.attest }} env: - VERSION: ${{ github.event_name == 'push' && github.ref_name || inputs.version }} + VERSION: ${{ inputs.version || github.ref_name }} + FROM_MAIN: ${{ inputs.from_main }} REPOSITORY_PRIVATE: ${{ github.event.repository.private }} DEVELOPER_DIR: /Applications/Xcode_16.4.app/Contents/Developer HOMEBREW_NO_AUTO_UPDATE: '1' @@ -152,18 +162,22 @@ jobs: path: out/releases/* if-no-files-found: error retention-days: 2 - - name: Retain draft release + # Published only here, after every job above succeeded, with all assets + # in one call (gh uploads them to a draft and publishes it at the end). + - name: Publish release if: needs.app.outputs.release_tag != '' env: GH_TOKEN: ${{ github.token }} RELEASE_TAG: ${{ needs.app.outputs.release_tag }} RELEASE_KIND: ${{ needs.app.outputs.release_kind }} run: | - options=(--repo "$GITHUB_REPOSITORY" --target "$GITHUB_SHA" --draft + options=(--repo "$GITHUB_REPOSITORY" --target "$GITHUB_SHA" --title "Resize APFS $VERSION") if [[ "$RELEASE_KIND" == private-preview ]]; then - options+=(--prerelease --notes "Private validation build from $GITHUB_SHA. No artifact attestation was generated.") + options+=(--draft --prerelease --notes "Private validation build from $GITHUB_SHA. No artifact attestation was generated.") else - options+=(--verify-tag --generate-notes) + # A pushed tag must exist already; from main, this creates it at $GITHUB_SHA. + if [[ "$GITHUB_REF" == refs/tags/* ]]; then options+=(--verify-tag); fi + options+=(--latest --generate-notes) fi gh release create "$RELEASE_TAG" out/releases/* "${options[@]}" diff --git a/README.md b/README.md index 284c3eb..a0d7f5b 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,12 @@ Open `ResizeAPFS.xcodeproj`, or run `./scripts/run-simulator.sh`. Host tests: `./scripts/build-device-ios12.sh` and `./scripts/package-release.sh preview`. Details: [Device/README.md](Device/README.md). +Pull requests are checked with the host tests (`Check` workflow). To release, raise +the version in `VERSION` in a pull request. When it is merged, the `Publish` workflow +builds that commit, tags it `vX.Y.Z` and publishes the release with all assets, as +the latest, in about 15 minutes. A push to `main` whose version is already tagged +releases nothing. A hand-pushed `v*` tag still builds and publishes as before. + ## License [MIT](LICENSE). See [third-party notices](THIRD_PARTY_NOTICES.md). diff --git a/Tests/ReleaseTests/test_release_plan.py b/Tests/ReleaseTests/test_release_plan.py index b92d6f6..6f9404b 100644 --- a/Tests/ReleaseTests/test_release_plan.py +++ b/Tests/ReleaseTests/test_release_plan.py @@ -35,6 +35,22 @@ def test_manual_run_on_tag_is_not_a_tag_push(self): p = module.plan('workflow_dispatch', 'preview', 'true', 'refs/tags/v0.1.0', '126') self.assertEqual(p['release_tag'], 'private-preview-126') + def test_version_on_main_releases_its_tag(self): + p = module.plan('push', 'v0.4.9', 'false', 'refs/heads/main', '127', 'true') + self.assertEqual(p['release_tag'], 'v0.4.9') + self.assertEqual(p['release_kind'], 'release') + self.assertEqual(p['attest'], 'true') + + def test_release_from_main_needs_main_and_a_plain_version(self): + for args in [ + ('push', 'v0.4.9', 'false', 'refs/heads/other', '127', 'true'), + ('workflow_dispatch', 'v0.4.9', 'false', 'refs/heads/main', '127', 'true'), + ('push', 'preview', 'false', 'refs/heads/main', '127', 'true'), + ('push', '0.4.9', 'false', 'refs/heads/main', '127', 'true'), + ]: + with self.subTest(args=args), self.assertRaises(ValueError): + module.plan(*args) + def test_invalid_inputs_fail_before_building(self): for args in [ ('push', 'preview', 'true', 'refs/heads/main', '123'), diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..cb498ab --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.4.8 diff --git a/scripts/release-plan.py b/scripts/release-plan.py index 825a65e..71947cf 100644 --- a/scripts/release-plan.py +++ b/scripts/release-plan.py @@ -4,14 +4,20 @@ import re -def plan(event, version, private, ref, run_id): +def plan(event, version, private, ref, run_id, from_main=''): if not re.fullmatch(r'[A-Za-z0-9][A-Za-z0-9._-]*', version): raise ValueError('Invalid release version.') if private not in {'true', 'false'}: raise ValueError('Repository visibility must be specified.') tag = '' kind = 'none' - if event == 'push' and ref == 'refs/tags/' + version and version.startswith('v'): + if from_main == 'true': + # Publish workflow: VERSION changed on main; the release creates the tag. + if event != 'push' or ref != 'refs/heads/main' or not re.fullmatch(r'v\d+\.\d+\.\d+', version): + raise ValueError('Only a vX.Y.Z version pushed to main may release from main.') + tag = version + kind = 'private-preview' if private == 'true' else 'release' + elif event == 'push' and ref == 'refs/tags/' + version and version.startswith('v'): tag = version kind = 'private-preview' if private == 'true' else 'release' elif event == 'workflow_dispatch': @@ -30,7 +36,7 @@ def plan(event, version, private, ref, run_id): try: result = plan(os.environ.get('GITHUB_EVENT_NAME', ''), os.environ.get('VERSION', ''), os.environ.get('REPOSITORY_PRIVATE', ''), os.environ.get('GITHUB_REF', ''), - os.environ.get('GITHUB_RUN_ID', '')) + os.environ.get('GITHUB_RUN_ID', ''), os.environ.get('FROM_MAIN', '')) except ValueError as error: raise SystemExit(str(error)) output = ''.join(f'{key}={value}\n' for key, value in result.items())