|
| 1 | +name: API client release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + paths: |
| 7 | + - .github/workflows/api-client-release.yml |
| 8 | + - packages/api-client/** |
| 9 | + |
| 10 | +concurrency: |
| 11 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 12 | + cancel-in-progress: false |
| 13 | + |
| 14 | +permissions: |
| 15 | + contents: write |
| 16 | + id-token: write |
| 17 | + |
| 18 | +jobs: |
| 19 | + release: |
| 20 | + if: github.repository_owner == 'modrinth' && github.ref == 'refs/heads/main' |
| 21 | + # npm Trusted Publishing requires a GitHub-hosted runner. |
| 22 | + runs-on: ubuntu-latest |
| 23 | + env: |
| 24 | + FORCE_COLOR: 3 |
| 25 | + PACKAGE_DIR: packages/api-client |
| 26 | + PACKAGE_NAME: '@modrinth/api-client' |
| 27 | + BUMP_TYPE: minor |
| 28 | + steps: |
| 29 | + - name: Check out code |
| 30 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 31 | + with: |
| 32 | + fetch-depth: 0 |
| 33 | + |
| 34 | + - name: Check for api-client changes |
| 35 | + id: changes |
| 36 | + run: | |
| 37 | + if [ "${{ github.event.before }}" = "0000000000000000000000000000000000000000" ]; then |
| 38 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 39 | + exit 0 |
| 40 | + fi |
| 41 | +
|
| 42 | + if git diff --quiet "${{ github.event.before }}" "$GITHUB_SHA" -- "$PACKAGE_DIR"; then |
| 43 | + echo "changed=false" >> "$GITHUB_OUTPUT" |
| 44 | + else |
| 45 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 46 | + fi |
| 47 | +
|
| 48 | + - name: Setup Node |
| 49 | + if: steps.changes.outputs.changed == 'true' |
| 50 | + uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0 |
| 51 | + with: |
| 52 | + node-version-file: .nvmrc |
| 53 | + registry-url: https://registry.npmjs.org |
| 54 | + |
| 55 | + - name: Enable Corepack |
| 56 | + if: steps.changes.outputs.changed == 'true' |
| 57 | + run: corepack enable |
| 58 | + |
| 59 | + - name: Get pnpm store path |
| 60 | + if: steps.changes.outputs.changed == 'true' |
| 61 | + id: pnpm-store |
| 62 | + run: echo "store-path=$(pnpm store path --silent)" >> "$GITHUB_OUTPUT" |
| 63 | + |
| 64 | + - name: Restore pnpm cache |
| 65 | + if: steps.changes.outputs.changed == 'true' |
| 66 | + uses: actions/cache/restore@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5 |
| 67 | + with: |
| 68 | + path: ${{ steps.pnpm-store.outputs.store-path }} |
| 69 | + key: pnpm-cache-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('**/pnpm-lock.yaml') }} |
| 70 | + restore-keys: | |
| 71 | + pnpm-cache- |
| 72 | +
|
| 73 | + - name: Install dependencies |
| 74 | + if: steps.changes.outputs.changed == 'true' |
| 75 | + run: pnpm install --frozen-lockfile --filter @modrinth/api-client... |
| 76 | + |
| 77 | + - name: Resolve release version |
| 78 | + if: steps.changes.outputs.changed == 'true' |
| 79 | + id: version |
| 80 | + run: | |
| 81 | + CURRENT_VERSION="$(node --print "require('./${PACKAGE_DIR}/package.json').version")" |
| 82 | +
|
| 83 | + set +e |
| 84 | + PUBLISHED_VERSION_JSON="$(pnpm view "${PACKAGE_NAME}" version --json)" |
| 85 | + VIEW_STATUS="$?" |
| 86 | + set -e |
| 87 | +
|
| 88 | + if [ "$VIEW_STATUS" -eq 0 ]; then |
| 89 | + PUBLISHED_VERSION="$(node --eval "const raw = process.argv[1] || ''; const value = raw ? JSON.parse(raw) : ''; console.log(Array.isArray(value) ? value.at(-1) || '' : value || '')" "$PUBLISHED_VERSION_JSON")" |
| 90 | + else |
| 91 | + PUBLISHED_VERSION="" |
| 92 | + fi |
| 93 | +
|
| 94 | + VERSION_RESULT="$(node --eval "const [current, published, bumpType] = process.argv.slice(1); const parse = (version) => { const match = /^([0-9]+)\\.([0-9]+)\\.([0-9]+)$/.exec(version || ''); if (!match) throw new Error('Unsupported semver version: ' + version); return match.slice(1).map(Number); }; const compare = (left, right) => { const a = parse(left); const b = parse(right); for (let i = 0; i < 3; i++) { if (a[i] !== b[i]) return a[i] - b[i]; } return 0; }; const bump = (version, type) => { const next = parse(version); if (type === 'major') { next[0]++; next[1] = 0; next[2] = 0; } else if (type === 'minor') { next[1]++; next[2] = 0; } else if (type === 'patch') { next[2]++; } else { throw new Error('Unsupported bump type: ' + type); } return next.join('.'); }; const shouldBump = Boolean(published) && compare(current, published) <= 0; const next = shouldBump ? bump(published, bumpType) : current; console.log(JSON.stringify({ current, published, next, shouldBump }));" "$CURRENT_VERSION" "$PUBLISHED_VERSION" "$BUMP_TYPE")" |
| 95 | +
|
| 96 | + NEXT_VERSION="$(node --eval "console.log(JSON.parse(process.argv[1]).next)" "$VERSION_RESULT")" |
| 97 | + SHOULD_BUMP="$(node --eval "console.log(JSON.parse(process.argv[1]).shouldBump ? 'true' : 'false')" "$VERSION_RESULT")" |
| 98 | +
|
| 99 | + if [ "$SHOULD_BUMP" = "true" ]; then |
| 100 | + node --eval "const fs = require('node:fs'); const path = process.argv[1]; const version = process.argv[2]; const pkg = JSON.parse(fs.readFileSync(path, 'utf8')); pkg.version = version; fs.writeFileSync(path, JSON.stringify(pkg, null, '\t') + '\n');" "$PACKAGE_DIR/package.json" "$NEXT_VERSION" |
| 101 | + fi |
| 102 | +
|
| 103 | + echo "current_version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT" |
| 104 | + echo "published_version=$PUBLISHED_VERSION" >> "$GITHUB_OUTPUT" |
| 105 | + echo "version=$NEXT_VERSION" >> "$GITHUB_OUTPUT" |
| 106 | + echo "bumped=$SHOULD_BUMP" >> "$GITHUB_OUTPUT" |
| 107 | +
|
| 108 | + - name: Build api-client |
| 109 | + if: steps.changes.outputs.changed == 'true' |
| 110 | + run: pnpm --filter @modrinth/api-client build |
| 111 | + |
| 112 | + - name: Check package contents |
| 113 | + if: steps.changes.outputs.changed == 'true' |
| 114 | + working-directory: packages/api-client |
| 115 | + run: pnpm pack --dry-run |
| 116 | + |
| 117 | + - name: Configure git |
| 118 | + if: steps.changes.outputs.changed == 'true' && steps.version.outputs.bumped == 'true' |
| 119 | + run: | |
| 120 | + git config user.name "github-actions[bot]" |
| 121 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 122 | +
|
| 123 | + - name: Commit version bump |
| 124 | + if: steps.changes.outputs.changed == 'true' && steps.version.outputs.bumped == 'true' |
| 125 | + run: | |
| 126 | + git add "$PACKAGE_DIR/package.json" |
| 127 | + git commit -m "Release @modrinth/api-client v${{ steps.version.outputs.version }} [skip ci]" |
| 128 | +
|
| 129 | + - name: Push version bump |
| 130 | + if: steps.changes.outputs.changed == 'true' && steps.version.outputs.bumped == 'true' |
| 131 | + env: |
| 132 | + GH_ACCESS_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }} |
| 133 | + run: | |
| 134 | + if [ -n "$GH_ACCESS_TOKEN" ]; then |
| 135 | + git remote set-url origin "https://x-access-token:${GH_ACCESS_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" |
| 136 | + fi |
| 137 | +
|
| 138 | + git push origin HEAD:main |
| 139 | +
|
| 140 | + - name: Publish api-client |
| 141 | + if: steps.changes.outputs.changed == 'true' |
| 142 | + working-directory: packages/api-client |
| 143 | + run: pnpm publish --access public --provenance --no-git-checks |
0 commit comments