diff --git a/.github/workflows/dist.yml b/.github/workflows/dist.yml index 5c95399..0aa11d4 100644 --- a/.github/workflows/dist.yml +++ b/.github/workflows/dist.yml @@ -2,12 +2,14 @@ name: Distribute Binaries on: - push: - tags: [cargo-changeset@v*] workflow_dispatch: permissions: - contents: write + contents: read + +concurrency: + group: dist-${{ github.ref }} + cancel-in-progress: false jobs: build: @@ -56,10 +58,16 @@ jobs: if: ${{ !matrix.use_cross }} run: cargo build --release --package cargo-changeset --target ${{ matrix.target }} - - name: Extract version from tag + - name: Determine archive version id: version shell: bash - run: echo "version=${GITHUB_REF_NAME#cargo-changeset@v}" >> "$GITHUB_OUTPUT" + run: | + if [[ "$GITHUB_REF" == refs/tags/cargo-changeset@v* ]]; then + version="${GITHUB_REF_NAME#cargo-changeset@v}" + else + version="0.0.0-${GITHUB_SHA::7}" + fi + echo "version=$version" >> "$GITHUB_OUTPUT" - name: Package binary (Unix) if: ${{ matrix.archive_ext == 'tar.gz' }} @@ -98,27 +106,33 @@ jobs: release: name: Create GitHub Release + if: ${{ startsWith(github.ref, 'refs/tags/cargo-changeset@v') }} runs-on: ubuntu-latest needs: build + permissions: + contents: write steps: - - uses: actions/checkout@v6 - - - name: Extract version from tag - id: version - run: echo "version=${GITHUB_REF_NAME#cargo-changeset@v}" >> "$GITHUB_OUTPUT" - - name: Download all artifacts uses: actions/download-artifact@v8 with: pattern: binary-* merge-multiple: true - - name: Create GitHub Release - run: | - gh release create "$GITHUB_REF_NAME" \ - --title "cargo-changeset v${{ steps.version.outputs.version }}" \ - --generate-notes \ - cargo-changeset-${{ steps.version.outputs.version }}-*.tar.gz \ - cargo-changeset-${{ steps.version.outputs.version }}-*.zip + - name: Create or update GitHub Release env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_REPO: ${{ github.repository }} + run: |- + shopt -s failglob + version="${GITHUB_REF_NAME#cargo-changeset@v}" + assets=(cargo-changeset-"$version"-*.tar.gz cargo-changeset-"$version"-*.zip) + if gh release view "$GITHUB_REF_NAME" > /dev/null 2>&1; then + echo "Release $GITHUB_REF_NAME exists, replacing its assets" + gh release upload "$GITHUB_REF_NAME" --clobber "${assets[@]}" + else + gh release create "$GITHUB_REF_NAME" \ + --verify-tag \ + --title "cargo-changeset v$version" \ + --generate-notes \ + "${assets[@]}" + fi diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index a6b538c..4d8f457 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,9 +1,6 @@ name: Publish Docker Image on: - push: - tags: - - "cargo-changeset@v*" workflow_dispatch: env: @@ -38,9 +35,10 @@ jobs: with: images: | ${{ env.GHCR_IMAGE }} - ${{ env.DOCKERHUB_IMAGE }} + name=${{ env.DOCKERHUB_IMAGE }},enable=${{ github.repository == 'lukidoescode/cargo-changeset' }} - name: Log in to GHCR + if: ${{ github.repository == 'lukidoescode/cargo-changeset' }} uses: docker/login-action@v4 with: registry: ghcr.io @@ -48,6 +46,7 @@ jobs: password: ${{ secrets.GITHUB_TOKEN }} - name: Log in to Docker Hub + if: ${{ github.repository == 'lukidoescode/cargo-changeset' }} uses: docker/login-action@v4 with: username: ${{ secrets.DOCKERHUB_USERNAME }} @@ -57,6 +56,7 @@ jobs: uses: docker/setup-buildx-action@v4 - name: Build and push by digest + if: ${{ github.repository == 'lukidoescode/cargo-changeset' }} id: build uses: docker/build-push-action@v7 with: @@ -65,13 +65,24 @@ jobs: labels: ${{ steps.meta.outputs.labels }} outputs: type=image,"name=${{ env.GHCR_IMAGE }},${{ env.DOCKERHUB_IMAGE }}",push-by-digest=true,name-canonical=true,push=true + - name: Build without pushing outside the upstream repository + if: ${{ github.repository != 'lukidoescode/cargo-changeset' }} + uses: docker/build-push-action@v7 + with: + context: . + platforms: ${{ matrix.platform }} + labels: ${{ steps.meta.outputs.labels }} + outputs: type=cacheonly + - name: Export digest + if: ${{ github.repository == 'lukidoescode/cargo-changeset' }} run: | # zizmor: ignore[template-injection] mkdir -p /tmp/digests digest="${{ steps.build.outputs.digest }}" touch "/tmp/digests/${digest#sha256:}" - name: Upload digest + if: ${{ github.repository == 'lukidoescode/cargo-changeset' }} uses: actions/upload-artifact@v7 with: name: digests-${{ matrix.digest-name }} @@ -80,6 +91,7 @@ jobs: retention-days: 1 merge: + if: ${{ github.repository == 'lukidoescode/cargo-changeset' }} runs-on: ubuntu-latest needs: build environment: docker publish diff --git a/.github/workflows/lint-github-workflows-and-actions.yml b/.github/workflows/lint-github-workflows-and-actions.yml index c690fba..b95f417 100644 --- a/.github/workflows/lint-github-workflows-and-actions.yml +++ b/.github/workflows/lint-github-workflows-and-actions.yml @@ -33,6 +33,19 @@ jobs: run: ${{ steps.get_actionlint.outputs.executable }} -color # zizmor: ignore[template-injection] shell: bash + test-dispatch-script: + name: Test release dispatch script + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - uses: actions/checkout@v6 + with: + persist-credentials: false + + - name: Run dispatch-publish.sh tests + run: bash .github/workflows/scripts/test-dispatch-publish.sh + zizmor: name: Security audit workflows and actions (zizmor) runs-on: ubuntu-latest diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index fb0418b..e062dbd 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -2,8 +2,6 @@ name: Publish Crate on: - push: - tags: ['*@v*'] workflow_dispatch: permissions: @@ -25,6 +23,7 @@ jobs: run: echo "name=${GITHUB_REF_NAME%@v*}" >> "$GITHUB_OUTPUT" - name: Publish with retry + if: ${{ github.repository == 'lukidoescode/cargo-changeset' }} env: CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} run: |- @@ -52,3 +51,9 @@ jobs: sleep "$delay" attempt=$(( attempt + 1 )) done + + # Packages every workspace crate so that unpublished versions of + # workspace dependencies resolve locally instead of against crates.io. + - name: Publish dry run outside the upstream repository + if: ${{ github.repository != 'lukidoescode/cargo-changeset' }} + run: cargo publish --workspace --dry-run diff --git a/.github/workflows/scripts/dispatch-publish.sh b/.github/workflows/scripts/dispatch-publish.sh index 87e0622..89beb25 100644 --- a/.github/workflows/scripts/dispatch-publish.sh +++ b/.github/workflows/scripts/dispatch-publish.sh @@ -1,10 +1,29 @@ #!/usr/bin/env bash -# Dispatches publish (and for the CLI crate: dist + docker) workflows for each -# tag pointing at HEAD, in workspace dependency order. A 60-second pause is -# inserted between depth tiers so crates.io has time to index each tier before +# Dispatches the publish workflow for each tag pointing at HEAD, in workspace +# dependency order. The cargo-changeset tag additionally dispatches the dist +# (prebuilt binaries) and docker workflows. A 60-second pause is inserted +# between depth tiers so crates.io has time to index each tier before # dependents are published. +# +# A failed dispatch does not stop the remaining ones. Failures are summarised +# at the end and make the script exit non-zero. set -euo pipefail +: "${GITHUB_REPOSITORY:?GITHUB_REPOSITORY must be set}" + +failures=() + +dispatch() { + local workflow=$1 + local tag=$2 + + echo "Dispatching $workflow for $tag" + if ! gh workflow run "$workflow" --repo "$GITHUB_REPOSITORY" --ref "refs/tags/$tag"; then + echo "::error::Failed to dispatch $workflow for $tag" + failures+=("$workflow for $tag") + fi +} + tags=$(git tag --points-at HEAD) if [ -z "$tags" ]; then @@ -30,7 +49,7 @@ for _ in range(len(names)): if depths[n] <= depths[d]: depths[n] = depths[d] + 1 -for name in sorted(names, key=lambda n: depths[n]): +for name in sorted(names, key=lambda n: (depths[n], n)): print(depths[name], name) ") @@ -46,12 +65,22 @@ while IFS=" " read -r depth crate; do fi current_depth=$depth - echo "Dispatching publish for $tag (depth=$depth)" - gh workflow run publish.yml --repo "$GITHUB_REPOSITORY" --ref "refs/tags/$tag" + echo "Processing $tag (depth=$depth)" + dispatch publish.yml "$tag" if [ "$crate" = "cargo-changeset" ]; then - echo "Dispatching docker for $tag (depth=$depth)" - gh workflow run docker.yml --repo "$GITHUB_REPOSITORY" --ref "refs/tags/$tag" + dispatch dist.yml "$tag" + dispatch docker.yml "$tag" fi done <<< "$depth_order" + +if [ "${#failures[@]}" -gt 0 ]; then + echo "::error::${#failures[@]} workflow dispatch(es) failed:" + for failure in "${failures[@]}"; do + echo " - $failure" + done + exit 1 +fi + +echo "All workflow dispatches succeeded." diff --git a/.github/workflows/scripts/test-dispatch-publish.sh b/.github/workflows/scripts/test-dispatch-publish.sh new file mode 100755 index 0000000..28c259d --- /dev/null +++ b/.github/workflows/scripts/test-dispatch-publish.sh @@ -0,0 +1,215 @@ +#!/usr/bin/env bash +# Tests dispatch-publish.sh against a synthetic Cargo workspace whose crate +# names and dependency tiers mirror the real workspace. `gh` and `sleep` are +# replaced by stubs that record their invocations, so nothing is dispatched. +# +# Requires git, cargo and python3 on PATH. Temporary files go to $TMPDIR. +set -euo pipefail + +script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +script_under_test="$script_dir/dispatch-publish.sh" +repository="example-owner/example-repo" + +work_dir=$(mktemp -d "${TMPDIR:-/tmp}/dispatch-publish-test.XXXXXX") +trap 'rm -rf "$work_dir"' EXIT + +stub_dir="$work_dir/stubs" +stub_log="$work_dir/stub.log" +mkdir -p "$stub_dir" + +cat > "$stub_dir/gh" <<'EOF' +#!/usr/bin/env bash +echo "gh $*" >> "$STUB_LOG" +while IFS= read -r pattern; do + if [ -n "$pattern" ] && [[ "gh $*" == *"$pattern"* ]]; then + echo "stub gh: simulated failure" >&2 + exit 1 + fi +done <<< "${GH_STUB_FAIL_MATCH:-}" +EOF + +cat > "$stub_dir/sleep" <<'EOF' +#!/usr/bin/env bash +echo "sleep $*" >> "$STUB_LOG" +EOF + +chmod +x "$stub_dir/gh" "$stub_dir/sleep" + +export STUB_LOG="$stub_log" +export GIT_AUTHOR_NAME="Test" GIT_AUTHOR_EMAIL="test@example.com" +export GIT_COMMITTER_NAME="Test" GIT_COMMITTER_EMAIL="test@example.com" +export GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null + +failed_assertions=0 +current_case="" + +fail() { + echo " FAIL [$current_case]: $1" + failed_assertions=$((failed_assertions + 1)) +} + +assert_equals() { + local description=$1 expected=$2 actual=$3 + if [ "$expected" != "$actual" ]; then + fail "$description" + diff <(echo "$expected") <(echo "$actual") | sed 's/^/ /' || true + fi +} + +assert_contains() { + local description=$1 needle=$2 haystack=$3 + if [[ "$haystack" != *"$needle"* ]]; then + fail "$description (missing: $needle)" + fi +} + +count_lines() { + local pattern=$1 + grep -cF -- "$pattern" "$stub_log" || true +} + +write_crate() { + local repo=$1 name=$2 + shift 2 + mkdir -p "$repo/crates/$name/src" + { + printf '[package]\nname = "%s"\nversion = "0.1.0"\nedition = "2021"\n\n[dependencies]\n' "$name" + for dependency in "$@"; do + printf '%s = { path = "../%s" }\n' "$dependency" "$dependency" + done + } > "$repo/crates/$name/Cargo.toml" + touch "$repo/crates/$name/src/lib.rs" +} + +# Tiers: 0 = changeset-core; 1 = changeset-parse, changeset-version; +# 2 = changeset-operations; 3 = cargo-changeset. +create_workspace_repo() { + local repo=$1 + mkdir -p "$repo" + printf '[workspace]\nmembers = ["crates/*"]\nresolver = "2"\n' > "$repo/Cargo.toml" + write_crate "$repo" changeset-core + write_crate "$repo" changeset-parse changeset-core + write_crate "$repo" changeset-version changeset-core + write_crate "$repo" changeset-operations changeset-parse changeset-version + write_crate "$repo" cargo-changeset changeset-operations changeset-core + git -C "$repo" init --quiet --initial-branch=main + git -C "$repo" add -A + git -C "$repo" commit --quiet -m "Initial commit" + git -C "$repo" tag -a "changeset-core@v0.0.1" -m "older release" + git -C "$repo" commit --quiet --allow-empty -m "Release" +} + +tag_head() { + local repo=$1 + shift + for tag in "$@"; do + git -C "$repo" tag -a "$tag" -m "$tag" + done +} + +run_script() { + local repo=$1 + : > "$stub_log" + set +e + script_output=$(cd "$repo" && PATH="$stub_dir:$PATH" GITHUB_REPOSITORY="$repository" bash "$script_under_test" 2>&1) + script_exit=$? + set -e + stub_calls=$(cat "$stub_log") +} + +dispatch_line() { + echo "gh workflow run $1 --repo $repository --ref refs/tags/$2" +} + +all_tags=( + "cargo-changeset@v0.1.8" + "changeset-core@v0.0.5" + "changeset-operations@v0.2.2" + "changeset-parse@v0.0.5" + "changeset-version@v0.0.5" +) + +expected_full_release=$(cat <` | Remove a crate from the queue (repeatable) | | `-l, --list` | List crates marked for graduation | +### `cargo changeset additional-packages` + +Manage [non-Rust package](#additional-packages) declarations in `Cargo.toml`. Requires a Cargo workspace. Without flags, each subcommand prompts interactively. + +| Subcommand | Description | +|------------|-------------| +| `add` | Declare a package. Flags: `--name`, `--path`, `--influence ` (repeatable), `--manifest-file`, `--manifest-format`, `--version-field-path` | +| `remove --name ` | Remove a declaration | +| `edit --name ` | Change a declaration; takes the same flags as `add` (`--influence` replaces all existing globs) | +| `list` | List all declarations | +| `dependencies add` | Track a dependency's version in a manifest. Flags: `--package`, `--dependency`, `--manifest-file`, `--manifest-format` (detected from the extension if omitted), `--version-field-path` | +| `dependencies remove` | Stop tracking a dependency. Flags: `--package`, `--dependency` | +| `dependencies list` | List tracked dependencies. Flag: `--package` | + ## Configuration Configuration is stored in `Cargo.toml` under `[workspace.metadata.changeset]` for workspaces or `[package.metadata.changeset]` for single-crate projects. All keys use kebab-case. Running `cargo changeset init` writes these values for you. @@ -368,6 +382,49 @@ none-bump-behavior = "promote-to-patch" | `comparison-links-template` | string | _(auto-detected)_ | Custom comparison URL template. `{repository}` is `/` from the `origin` remote, `{base}` and `{target}` are the release tags; overrides forge detection and also works for self-hosted hosts; ignored when `comparison-links = "disabled"` | | `dependency-bump-changelog-template` | string | ``"Updated dependency `{dependency}` to v{version}"`` | Template for auto-generated dependency bump changelog entries | | `ignored-files` | array | `[]` | Glob patterns for files to ignore in change detection | +| `additional-packages` | array of tables | `[]` | Non-Rust packages versioned alongside crates (workspaces only) — see [Additional Packages](#additional-packages) | + +### Additional Packages + +Some releases include artifacts that are not Cargo crates, such as a Helm chart, an npm package, or a Docker configuration. Declare them as additional packages, and they take part in `add`, `status`, `verify`, and `release` like any crate: they can be selected in a changeset, receive a version bump, and have their version field written into a manifest file. Additional packages require a Cargo workspace. + +Declare each package under `[[workspace.metadata.changeset.additional-packages]]`: + +```toml +[[workspace.metadata.changeset.additional-packages]] +name = "my-helm-chart" +path = "charts/my-chart" +influence = ["charts/my-chart/**"] + +[workspace.metadata.changeset.additional-packages.manifest] +file-path = "charts/my-chart/Chart.yaml" +format = "yaml" +version-field-path = "version" + +[[workspace.metadata.changeset.additional-packages.dependencies]] +dependency-name = "my-service" + +[workspace.metadata.changeset.additional-packages.dependencies.version-tracking-manifest] +file-path = "charts/my-chart/Chart.yaml" +format = "yaml" +version-field-path = "appVersion" +``` + +| Field | Description | +|-------|-------------| +| `name` | Package name used in changesets; `additional-packages add` rejects names that collide with a crate or another additional package, so keep names unique when editing `Cargo.toml` by hand | +| `path` | Package directory, relative to the workspace root | +| `influence` | Glob patterns matched against changed file paths relative to the workspace root; `verify` requires changeset coverage when a changed file matches one. Files inside a crate's directory belong to that crate and are never matched | +| `manifest.file-path` | File holding the package version | +| `manifest.format` | `toml`, `yaml`, or `json` | +| `manifest.version-field-path` | Dot-separated path to the version field, e.g. `version` or `package.version` | +| `dependencies` | Optional list of version-tracking dependencies (see below) | + +A version-tracking dependency copies the released version of another workspace package (`dependency-name`, a crate or an additional package) into a field of a manifest file, described by `file-path`, `format`, and `version-field-path` under `version-tracking-manifest`. When the tracked package is released, the dependent additional package is bumped automatically with a `patch` release, unless the dependent already has its own changeset in that release, in which case the changeset's bump is used. The bump also propagates transitively to further dependents. + +The reverse also works: a crate can track an additional package's version by declaring `[[package.metadata.changeset.additional-package-dependencies]]` in its own `Cargo.toml`, with the same `dependency-name` and `version-tracking-manifest` fields. Unknown, duplicate, and circular tracking declarations are rejected. + +Use `cargo changeset additional-packages` to manage these entries without editing `Cargo.toml` by hand. ### Changeset File Format