name: Build project release task # The fleet release chain, hosted here once and reached by every release repo through a caller stub, per docs/reusable-workflows.md "Adopting the Release Chain". # NBGV runs once in get-version and threads the version to every target. # The validate-release job gates branch<->version consistency once before any build or publish. # The github-release job is verbatim in shape: pattern download, exists gate, target_commitish, consume-then-delete. # The expect_release_assets input collapses the fleet's three no-asset release shapes (Docker-only, PyPI-only, source-only) into one flag. # # Each build target uses a caller hook or the hub default for that target. # The one exception is build-release-asset, a file target the hub cannot know, whose caller hook is required. # The Docker target calls the hub's reusable Docker task at this workflow's commit. on: workflow_call: inputs: # Whether to create a GitHub release. github: required: false type: boolean default: false # Whether to push the docker image to Docker Hub. dockerhub: required: false type: boolean default: false # Git ref to check out / version (empty = default checkout ref). ref: required: false type: string default: '' # Logical branch driving config / tags / prerelease for every target. # Required, with no fallback, because each publish run builds a single branch, the trigger ref. # A silent fallback would therefore mislabel the build. branch: required: true type: string # Smoke mode: a reduced, never-published build for fast PR feedback. # Forwarded to every target and hard-disables every push below, so a smoke run can never publish regardless of the publish flags. smoke: required: false type: boolean default: false # Per-target presence gates, defaulting to true so everything builds, except enable_release_asset below. # A PR smoke run sets these from the paths-filter so only changed targets build. enable_docker: required: false type: boolean default: true enable_nuget: required: false type: boolean default: true enable_pypi: required: false type: boolean default: true enable_dotnet_publish: required: false type: boolean default: true # A file target the hub ships no leaf for, built by the caller's build-release-asset hook, which has no hub default to fall back to. enable_release_asset: required: false type: boolean default: false # Set false for a repo that produces no release-asset-* files, such as a Docker-only, PyPI-only, or source-only one. # A NuGet caller keeps the default, since the nuget-build hook uploads a release-asset--nuget carrying the package. # The release is then just the tag + source zip + README + LICENSE. # The artifact download is skipped and the unmatched-files guard relaxes. expect_release_assets: required: false type: boolean default: true # Docker: a plain Docker Hub repository (e.g. ptr727/widget), and an optional pre-built matrix, per build-docker-task.yml. # See that file's inputs for the full docker-prepare / docker-build-base contract. docker_image: required: false type: string default: '' docker_matrix: required: false type: string default: '' docker_build_base: required: false type: boolean default: false # Project paths forwarded to the hub default hooks. dotnet_publish_project: required: false type: string default: '' # The release archive's name without .7z, derived from the project file's stem when empty. dotnet_publish_asset_name: required: false type: string default: '' nuget_project: required: false type: string default: '' pypi_project_dir: required: false type: string default: '' pypi_version_file: required: false type: string default: '' secrets: # Not needed when enable_docker is false. DOCKER_HUB_USERNAME: required: false DOCKER_HUB_ACCESS_TOKEN: required: false jobs: get-version: name: Get version information job uses: $/.github/workflows/get-version-task.yml with: ref: ${{ inputs.ref }} # Entry gate: validate branch<->version consistency once, before the build jobs. # An NBGV mis-classification then fails fast instead of after building and publishing. # The main branch must be a public release, carrying no prerelease '-'. # Every other branch must carry a prerelease '-', which guards a develop leg being published as stable. # Strip '+buildmetadata' first, since a '-' there is legitimate. # Only a '-' in the core/prerelease segment marks a prerelease. validate-release: name: Validate release version job needs: [get-version] runs-on: ubuntu-latest steps: - name: Validate branch and version consistency step env: SEMVER2: ${{ needs.get-version.outputs.SemVer2 }} BRANCH: ${{ inputs.branch }} SMOKE: ${{ inputs.smoke }} run: | set -Eeuo pipefail # Refuse a branch git would not accept as a name. # Its ':', '[' or control characters could form a workflow command where later steps print it. # Both of git's streams are discarded, since it echoes the name it checks. if ! git check-ref-format --branch "$BRANCH" > /dev/null 2>&1; then echo "::error::The branch input is not a valid git branch name, so the build is refused." exit 1 fi # Smoke builds never publish and always version as prerelease (detached PR HEAD), which would trip the main arm. if [[ "$SMOKE" == "true" ]]; then echo "Smoke build; skipping release version validation." exit 0 fi CORE_AND_PRE="${SEMVER2%%+*}" if [[ "$BRANCH" == "main" ]]; then if [[ "$CORE_AND_PRE" == *-* ]]; then echo "::error::Public (main) release version '$SEMVER2' carries a prerelease suffix; refusing to publish." exit 1 fi elif [[ "$CORE_AND_PRE" != *-* ]]; then echo "::error::Prerelease version '$SEMVER2' has no prerelease suffix (NBGV classified it public); refusing to publish." exit 1 fi dotnet-publish: name: Publish .NET project job if: ${{ inputs.enable_dotnet_publish }} needs: [get-version, validate-release] runs-on: ubuntu-latest steps: # Pin to the resolved commit so the artifacts match the release tag even if the branch advances mid-run. - name: Checkout code step uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: ref: ${{ needs.get-version.outputs.GitCommitId }} # A caller's own hook names its archive itself, so the asset name input reaches only the hub default. - name: Run caller dotnet-publish hook step if: ${{ hashFiles('.github/actions/dotnet-publish/action.yml') != '' }} uses: ./.github/actions/dotnet-publish with: branch: ${{ inputs.branch }} smoke: ${{ inputs.smoke }} semver2: ${{ needs.get-version.outputs.SemVer2 }} assembly-version: ${{ needs.get-version.outputs.AssemblyVersion }} assembly-file-version: ${{ needs.get-version.outputs.AssemblyFileVersion }} assembly-informational-version: ${{ needs.get-version.outputs.AssemblyInformationalVersion }} project-file: ${{ inputs.dotnet_publish_project }} - name: Run hub dotnet-publish default step if: ${{ hashFiles('.github/actions/dotnet-publish/action.yml') == '' }} uses: $/.github/actions/dotnet-publish-default with: branch: ${{ inputs.branch }} smoke: ${{ inputs.smoke }} semver2: ${{ needs.get-version.outputs.SemVer2 }} assembly-version: ${{ needs.get-version.outputs.AssemblyVersion }} assembly-file-version: ${{ needs.get-version.outputs.AssemblyFileVersion }} assembly-informational-version: ${{ needs.get-version.outputs.AssemblyInformationalVersion }} project-file: ${{ inputs.dotnet_publish_project }} asset-name: ${{ inputs.dotnet_publish_asset_name }} # NuGet publishing happens in the caller stub's own publish-nuget job, since a push from here carries this hub's job_workflow_ref claim. # This job just builds and uploads the artifact for that job to download, plus the GitHub release asset. build-nuget: name: Build NuGet library job if: ${{ inputs.enable_nuget }} needs: [get-version, validate-release] runs-on: ubuntu-latest steps: - name: Checkout code step uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: ref: ${{ needs.get-version.outputs.GitCommitId }} - name: Run caller build-nuget hook step if: ${{ hashFiles('.github/actions/build-nuget/action.yml') != '' }} uses: ./.github/actions/build-nuget with: branch: ${{ inputs.branch }} smoke: ${{ inputs.smoke }} semver2: ${{ needs.get-version.outputs.SemVer2 }} assembly-version: ${{ needs.get-version.outputs.AssemblyVersion }} assembly-file-version: ${{ needs.get-version.outputs.AssemblyFileVersion }} assembly-informational-version: ${{ needs.get-version.outputs.AssemblyInformationalVersion }} project-file: ${{ inputs.nuget_project }} - name: Run hub build-nuget default step if: ${{ hashFiles('.github/actions/build-nuget/action.yml') == '' }} uses: $/.github/actions/nuget-build-default with: branch: ${{ inputs.branch }} smoke: ${{ inputs.smoke }} semver2: ${{ needs.get-version.outputs.SemVer2 }} assembly-version: ${{ needs.get-version.outputs.AssemblyVersion }} assembly-file-version: ${{ needs.get-version.outputs.AssemblyFileVersion }} assembly-informational-version: ${{ needs.get-version.outputs.AssemblyInformationalVersion }} project-file: ${{ inputs.nuget_project }} # PyPI publishing happens in the caller stub's own publish-pypi job, since id-token: write is granted only at that one entry point. # This job just builds and uploads the artifact for that job to download. build-pypi: name: Build PyPI library job if: ${{ inputs.enable_pypi }} needs: [get-version, validate-release] runs-on: ubuntu-latest steps: - name: Checkout code step uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: ref: ${{ needs.get-version.outputs.GitCommitId }} # A caller hook should declare semver2, though one that does not only draws the runner's unexpected-input warning. - name: Run caller build-pypi hook step if: ${{ hashFiles('.github/actions/build-pypi/action.yml') != '' }} uses: ./.github/actions/build-pypi with: branch: ${{ inputs.branch }} smoke: ${{ inputs.smoke }} semver2: ${{ needs.get-version.outputs.SemVer2 }} assembly-file-version: ${{ needs.get-version.outputs.AssemblyFileVersion }} project-dir: ${{ inputs.pypi_project_dir }} - name: Run hub build-pypi default step if: ${{ hashFiles('.github/actions/build-pypi/action.yml') == '' }} uses: $/.github/actions/pypi-build-default with: branch: ${{ inputs.branch }} smoke: ${{ inputs.smoke }} semver2: ${{ needs.get-version.outputs.SemVer2 }} project-dir: ${{ inputs.pypi_project_dir }} version-file: ${{ inputs.pypi_version_file }} # The hook only writes files, so the upload, its smoke gate (D1.3), and its retention (D5.4) stay here for every caller. build-release-asset: name: Build release asset job if: ${{ inputs.enable_release_asset }} needs: [get-version, validate-release] runs-on: ubuntu-latest steps: - name: Checkout code step uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: ref: ${{ needs.get-version.outputs.GitCommitId }} - name: Check caller build-release-asset hook step env: ASSET_DIR: ${{ runner.temp }}/release-asset HOOK_FOUND: ${{ hashFiles('.github/actions/build-release-asset/action.yml') != '' }} run: | set -Eeuo pipefail if [[ "$HOOK_FOUND" != "true" ]]; then echo "::error::enable_release_asset is true but .github/actions/build-release-asset/action.yml is missing." exit 1 fi mkdir -p "$ASSET_DIR" - name: Run caller build-release-asset hook step uses: ./.github/actions/build-release-asset with: branch: ${{ inputs.branch }} smoke: ${{ inputs.smoke }} semver2: ${{ needs.get-version.outputs.SemVer2 }} output-directory: ${{ runner.temp }}/release-asset - name: Check release asset output step env: ASSET_DIR: ${{ runner.temp }}/release-asset run: | set -Eeuo pipefail if [[ -z "$(find "$ASSET_DIR" -mindepth 1 -maxdepth 1 -type f -print -quit)" ]]; then echo "::error::The build-release-asset hook wrote no file to the top level of $ASSET_DIR." exit 1 fi stray=$(find "$ASSET_DIR" -mindepth 1 -maxdepth 1 \( ! -type f -o -name '.*' \) -print) if [[ -n "$stray" ]]; then echo "::error::The build-release-asset hook wrote entries other than top-level regular files without a dot-prefixed name: $stray" exit 1 fi ls -l "$ASSET_DIR" - name: Upload release asset step if: ${{ !inputs.smoke }} uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: release-asset-${{ inputs.branch }}-build-release-asset path: ${{ runner.temp }}/release-asset/* if-no-files-found: error retention-days: 1 build-docker: name: Build Docker image job needs: [get-version, validate-release, dotnet-publish, build-nuget, build-pypi, build-release-asset] if: ${{ inputs.enable_docker && !failure() && !cancelled() }} uses: $/.github/workflows/build-docker-task.yml secrets: DOCKER_HUB_USERNAME: ${{ secrets.DOCKER_HUB_USERNAME }} DOCKER_HUB_ACCESS_TOKEN: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} with: push: ${{ inputs.dockerhub && !inputs.smoke }} ref: ${{ needs.get-version.outputs.GitCommitId }} branch: ${{ inputs.branch }} smoke: ${{ inputs.smoke }} semver2: ${{ needs.get-version.outputs.SemVer2 }} assembly_version: ${{ needs.get-version.outputs.AssemblyVersion }} assembly_file_version: ${{ needs.get-version.outputs.AssemblyFileVersion }} assembly_informational_version: ${{ needs.get-version.outputs.AssemblyInformationalVersion }} image: ${{ inputs.docker_image }} matrix: ${{ inputs.docker_matrix }} build-base: ${{ inputs.docker_build_base }} github-release: name: Publish GitHub release job # !inputs.smoke enforces "smoke never publishes" at the job level, so a smoke caller that also set github: true still can't create a release. # The failure()/cancelled() checks are required here, not decorative. # An if: with no status-check function gets an implicit success() added, and success() is false when any needs job was skipped, a disabled target. # That would skip this job on every run with a disabled target, including the hub's own publish-release.yml, which disables every target. # The explicit functions replace that implicit success() instead, tolerating a skipped need while still failing closed on a real failure. if: ${{ inputs.github && !inputs.smoke && !failure() && !cancelled() }} runs-on: ubuntu-latest needs: [get-version, validate-release, dotnet-publish, build-nuget, build-pypi, build-release-asset, build-docker] # The release upload, the artifact-delete cleanup, and the D4.7 dispatch and self-cancel all write with GITHUB_TOKEN. # The caller grants contents: write and actions: write when it sets github: true on a non-smoke run. # A caller publishing only to a registry, or smoke building, leaves this job disabled and grants neither. # No job-level permissions: block here, because a called job's block is validated against the caller's grant before its if: runs. # A block naming contents: write would fail every caller that does not grant it at startup. # That includes a smoke build under a read-only pull request token. steps: # Check out the exact built commit so the uploaded release files match the tag even if the branch advances mid-run. - name: Checkout code step uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: ref: ${{ needs.get-version.outputs.GitCommitId }} # Collect assets by the release-asset--* pattern so this step is target-agnostic. # Subset releases by disabling the target, not by editing this job. # A skipped needs job still lets this job run, since it only needs a real, non-smoke publish. # The release step guards fail_on_unmatched_files: true, so at least one release-asset-* must match. # A repo that drops every file-producing target relaxes that guard. # A Docker-only or PyPI-only repo is the case, its release carrying only the source zip + README + LICENSE. - name: Download release asset artifacts step if: ${{ inputs.expect_release_assets }} uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 with: pattern: release-asset-${{ inputs.branch }}-* merge-multiple: true path: ./Publish # The weekly publisher re-runs even with no new commits, so the version may already be released. # Skip the release step when a release for this tag already exists, to avoid a no-op republish. - name: Check for existing release step id: release-exists env: GH_TOKEN: ${{ github.token }} TAG: ${{ needs.get-version.outputs.SemVer2 }} run: | set -Eeuo pipefail if gh release view "$TAG" --repo "$GITHUB_REPOSITORY" >/dev/null 2>&1; then echo "exists=true" >> "$GITHUB_OUTPUT" if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then echo "Release $TAG already exists; workflow_dispatch will refresh it." else echo "Release $TAG already exists; skipping release creation (no-op republish)." fi else echo "exists=false" >> "$GITHUB_OUTPUT" fi # See WORKFLOW.md D4.7. - name: Supersede a release the token cannot tag step if: ${{ steps.release-exists.outputs.exists == 'false' }} env: GH_TOKEN: ${{ github.token }} BRANCH: ${{ inputs.branch }} run: | set -Eeuo pipefail git fetch --no-tags --depth=1 origin "refs/heads/$BRANCH" built=$(git rev-parse --verify --quiet "HEAD:.github/workflows" || true) head=$(git rev-parse --verify --quiet "FETCH_HEAD:.github/workflows" || true) if [[ "$built" == "$head" ]]; then echo "Workflows at the built commit match $BRANCH's head; creating the release." exit 0 fi built_sha=$(git rev-parse HEAD) head_sha=$(git rev-parse FETCH_HEAD) refuse() { echo "::error::$BRANCH moved to $head_sha with different workflow files and $1, so GITHUB_TOKEN cannot tag $built_sha and only a deliberate dispatch may release the head." exit 1 } pushes=$(gh api "repos/$GITHUB_REPOSITORY/activity?ref=refs/heads/$BRANCH&per_page=100" \ --jq '.[] | "\(.after) \(.before) \(.activity_type) \(.actor.login // "none")"') sha="$head_sha" for _ in $(seq 100); do [[ "$sha" != "$built_sha" ]] || break read -r _ before type actor <<<"$(awk -v sha="$sha" '$1 == sha { print; exit }' <<<"$pushes")" [[ "$type" == "push" || "$type" == "pr_merge" ]] || refuse "no push or merge recorded producing $sha" [[ "$actor" == "ptr727-codegen[bot]" || "$actor" == "dependabot[bot]" ]] || refuse "$sha pushed by '$actor'" sha="$before" done [[ "$sha" == "$built_sha" ]] || refuse "more pushes since than the check reads" [[ "$(git ls-remote origin "refs/heads/$BRANCH" | cut -f1)" == "$head_sha" ]] || refuse "a further push since the check" publisher="${GITHUB_WORKFLOW_REF%@*}" publisher="${publisher##*/}" echo "::warning::$BRANCH moved to $head_sha with different workflow files, so GITHUB_TOKEN cannot tag $built_sha. Dispatching $publisher on $BRANCH to release the head, and cancelling this run." gh workflow run "$publisher" --repo "$GITHUB_REPOSITORY" --ref "$BRANCH" gh run cancel "$GITHUB_RUN_ID" --repo "$GITHUB_REPOSITORY" sleep 300 echo "::error::Run $GITHUB_RUN_ID was not cancelled within 300s; failing so no release or package push follows." exit 1 # Left unset, make_latest flags whichever release publishes last, so a re-run of an older version would take Latest from a newer one. # A prerelease never takes the flag, and a main release takes it only where its version sorts at or above the current Latest release's tag. # A repository with no Latest release yet answers 404, which leaves this release to take the flag. - name: Decide latest release step id: latest if: ${{ steps.release-exists.outputs.exists == 'false' || github.event_name == 'workflow_dispatch' }} env: GH_TOKEN: ${{ github.token }} BRANCH: ${{ inputs.branch }} TAG: ${{ needs.get-version.outputs.SemVer2 }} run: | set -Eeuo pipefail if [[ "$BRANCH" != "main" ]]; then echo "make_latest=false" >> "$GITHUB_OUTPUT" exit 0 fi error_file=$(mktemp) if current=$(gh api "repos/$GITHUB_REPOSITORY/releases/latest" --jq .tag_name 2> "$error_file"); then highest=$(printf '%s\n%s\n' "${current#v}" "${TAG#v}" | sort -V | tail -n 1) if [[ "$highest" == "${TAG#v}" ]]; then make_latest=true else make_latest=false echo "::notice::Release $TAG sorts below the current Latest release $current, so it leaves the Latest flag there." fi elif grep -q 'HTTP 404' "$error_file"; then make_latest=true else cat "$error_file" >&2 echo "::error::Could not read the current Latest release, so this run cannot tell whether $TAG may take the flag." exit 1 fi echo "make_latest=$make_latest" >> "$GITHUB_OUTPUT" # The target_commitish input must be set explicitly, or GitHub's REST API tags the release on the default branch. # Pin it to GitCommitId so the tag is on the exact built commit, consistent with the SemVer2 tag and artifacts. # Skip when the release already exists, but always let a manual workflow_dispatch through to refresh it. # Every release, on any branch and for any target, is a tag on the built commit plus the auto-attached source zip, README, and LICENSE. # Targets amend it by uploading release-asset-* files (binaries/packages) or by pushing elsewhere (image/registry). # Setting fail_on_unmatched_files true fails loudly if a promised release-asset-* is missing or misnamed. # A no-file-target repo relaxes it (see download step). - name: Create GitHub release step if: ${{ steps.release-exists.outputs.exists == 'false' || github.event_name == 'workflow_dispatch' }} uses: softprops/action-gh-release@efb35369e0ad2afab669f228072c1b0d510eae64 # v3.0.3 with: generate_release_notes: true tag_name: ${{ needs.get-version.outputs.SemVer2 }} target_commitish: ${{ needs.get-version.outputs.GitCommitId }} prerelease: ${{ inputs.branch != 'main' }} make_latest: ${{ steps.latest.outputs.make_latest }} fail_on_unmatched_files: ${{ inputs.expect_release_assets }} files: | LICENSE README.md ./Publish/* # Surgical cleanup at the point of consumption, since the release-asset--* transfer artifacts now have durable copies on the release. # Delete them by exact pattern to free the storage quota. # The pattern is scoped to this branch's assets, leaving diagnostics and any other artifacts. # It is gated to the same condition as the create step, so it only deletes when a release was actually created or refreshed this run. # On a skipped create, an existing tag with no new commits, the fresh artifacts stay for the run, reaped by the retention-days: 1 backstop. - name: Delete consumed release asset artifacts step if: ${{ inputs.expect_release_assets && (steps.release-exists.outputs.exists == 'false' || github.event_name == 'workflow_dispatch') }} # Best-effort, since the release is already published, so a listing or delete hiccup must never red the job. # The retention-days: 1 backstop reaps anything missed. # It deletes every matching id, because a rerun can upload duplicates. continue-on-error: true # The branch reaches jq through env rather than an expression, since an expression is pasted into the script before bash parses it. env: GH_TOKEN: ${{ github.token }} BRANCH: ${{ inputs.branch }} run: | set -Eeuo pipefail if ! ids=$(gh api "repos/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID/artifacts" --paginate \ --jq '.artifacts[] | select(.name | startswith("release-asset-" + env.BRANCH + "-")) | .id'); then echo "::warning::Could not list run artifacts; retention-days backstop will reap them." ids="" fi for id in $ids; do if ! gh api --method DELETE "repos/$GITHUB_REPOSITORY/actions/artifacts/$id"; then echo "::warning::Failed to delete artifact $id; retention-days backstop will reap it." fi done