Skip to content

Harden CI and publish workflow #745

Harden CI and publish workflow

Harden CI and publish workflow #745

Workflow file for this run

name: Build
on: [push, pull_request]
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
version:
name: Version
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
ci_tag: ${{ steps.version.outputs.ci_tag }}
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
persist-credentials: false
- name: Build Reason
shell: bash
run: |
branch=${GITHUB_REF##*/}.
if [[ "$branch" = "main." ]]; then
branch=""
elif [[ "$branch" = "master." ]]; then
branch=""
elif [[ "${GITHUB_REF}" = refs/tags* ]]; then
branch=""
elif [[ "${GITHUB_REF}" = refs/pull* ]]; then
branch=""
fi
echo "GIT_BRANCH_SUFFIX=$branch" >> "$GITHUB_ENV"
echo "ref: $GITHUB_REF event: $GITHUB_EVENT_NAME branch_suffix: $branch"
- name: Setup .NET SDK for MinVer
uses: actions/setup-dotnet@0f4a354dd5547a28c70b7853dbd95d5b67886e73 # v6.1.0
with:
dotnet-version: "10.0.x"
- name: Build Version
id: version
shell: bash
run: |
dotnet tool install --global minver-cli --version 7.0.0
version=$(minver --tag-prefix v --default-pre-release-identifiers "preview.${GIT_BRANCH_SUFFIX}0" --minimum-major-minor 3.0)
# If on a non-main branch, insert branch name before the height (last numeric segment)
if [ -n "$GIT_BRANCH_SUFFIX" ]; then
branch_name="${GIT_BRANCH_SUFFIX%.}"
if [[ "$version" != *"$branch_name"* ]]; then
version=$(echo "$version" | sed -E "s/\.([0-9]+)$/.${GIT_BRANCH_SUFFIX}\1/")
fi
fi
ci_tag="${GIT_BRANCH_SUFFIX%.}"
ci_tag="${ci_tag:-main}"
ci_tag="ci-${ci_tag//\//-}"
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "ci_tag=$ci_tag" >> "$GITHUB_OUTPUT"
echo "Version: $version"
echo "### Version: $version" >> "$GITHUB_STEP_SUMMARY"
build:
needs: version
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
- macos-latest
- windows-latest
node_version:
- 24
include:
- os: ubuntu-latest
node_version: 22
name: Node ${{ matrix.node_version }} on ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Setup Node.js environment
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: ${{ matrix.node_version }}
cache: npm
- name: Set Version
shell: bash
run: node scripts/set-version.mjs "$VERSION"
env:
VERSION: ${{ needs.version.outputs.version }}
- name: Install Dependencies
run: npm ci
- name: Build
run: npm run build
- name: Lint
if: matrix.os == 'ubuntu-latest' && matrix.node_version == 24
run: npm run lint
- name: Run Tests
run: npm test
# Publish jobs rebuild from a clean checkout so elevated permissions are only
# granted to the job that needs them. They skip dependency caching on purpose.
publish-npm:
name: Publish Release Packages
if: startsWith(github.ref, 'refs/tags/v')
needs: [version, build]
runs-on: ubuntu-latest
environment: npm-release
permissions:
contents: read
id-token: write
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Setup Node.js environment
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: 24
registry-url: "https://registry.npmjs.org"
package-manager-cache: false
- name: Set Version
run: node scripts/set-version.mjs "$VERSION"
env:
VERSION: ${{ needs.version.outputs.version }}
- name: Install Dependencies
run: npm ci
- name: Build
run: npm run build
- name: Publish Release Packages
run: npm publish --workspaces --access public
publish-github:
name: Push GitHub CI Packages
if: github.event_name != 'pull_request' && startsWith(github.ref, 'refs/heads/') && contains(needs.version.outputs.version, '-')
needs: [version, build]
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Setup Node.js environment
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: 24
package-manager-cache: false
- name: Set Version
run: node scripts/set-version.mjs "$VERSION"
env:
VERSION: ${{ needs.version.outputs.version }}
- name: Install Dependencies
run: npm ci
- name: Build
run: npm run build
- name: Setup GitHub CI Node.js environment
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: 24
registry-url: "https://npm.pkg.github.com"
scope: "@exceptionless"
package-manager-cache: false
- name: Push GitHub CI Packages
shell: bash
run: |
# Re-runs of the same commit produce the same version; skip packages
# that already exist and let every other publish failure fail the job.
# Publish in the root workspace order (dependencies first) so a package is
# never published before the exact dependency version it requires.
for dir in $(node -p "require('./package.json').workspaces.filter((w) => w.startsWith('packages/')).join('\n')"); do
name=$(node -p "require('./$dir/package.json').name")
existing=$(npm view "$name@$VERSION" version 2>/dev/null || true)
if [ "$existing" = "$VERSION" ]; then
echo "::notice::$name@$VERSION already exists in GitHub Packages; skipping."
continue
fi
npm publish --workspace "$dir" --access public --tag "$CI_TAG"
done
env:
VERSION: ${{ needs.version.outputs.version }}
CI_TAG: ${{ needs.version.outputs.ci_tag }}
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}