|
| 1 | +name: Publish @smakss/random-string to GitHub Packages |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ['Release @smakss/random-string'] |
| 6 | + types: [completed] |
| 7 | + branches: [master] |
| 8 | + |
| 9 | +concurrency: |
| 10 | + group: github-packages-${{ github.event.workflow_run.head_sha }} |
| 11 | + cancel-in-progress: false |
| 12 | + |
| 13 | +jobs: |
| 14 | + publish: |
| 15 | + if: github.event.workflow_run.conclusion == 'success' |
| 16 | + runs-on: ubuntu-latest |
| 17 | + permissions: |
| 18 | + contents: read |
| 19 | + packages: write |
| 20 | + |
| 21 | + steps: |
| 22 | + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 23 | + with: |
| 24 | + ref: ${{ github.event.workflow_run.head_sha }} |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Find semantic-release tag |
| 28 | + id: release |
| 29 | + env: |
| 30 | + RELEASE_SHA: ${{ github.event.workflow_run.head_sha }} |
| 31 | + run: | |
| 32 | + tag="$(git describe --tags --exact-match "$RELEASE_SHA" 2>/dev/null || true)" |
| 33 | + case "$tag" in |
| 34 | + v[0-9]*) |
| 35 | + echo "version=${tag#v}" >> "$GITHUB_OUTPUT" |
| 36 | + ;; |
| 37 | + *) |
| 38 | + echo "version=" >> "$GITHUB_OUTPUT" |
| 39 | + ;; |
| 40 | + esac |
| 41 | +
|
| 42 | + - name: Configure GitHub Packages registry |
| 43 | + if: steps.release.outputs.version != '' |
| 44 | + uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 |
| 45 | + with: |
| 46 | + node-version-file: .nvmrc |
| 47 | + registry-url: https://npm.pkg.github.com/ |
| 48 | + scope: "@smakss" |
| 49 | + |
| 50 | + - name: Setup pnpm and dependencies |
| 51 | + if: steps.release.outputs.version != '' |
| 52 | + uses: pnpm/setup@fbda4c85fc2e1e08721cd8763afea8f48d60f024 # v3.0.0 |
| 53 | + with: |
| 54 | + node-version-file: false |
| 55 | + cache: true |
| 56 | + require-lockfile: true |
| 57 | + |
| 58 | + - name: Set package version from release tag |
| 59 | + if: steps.release.outputs.version != '' |
| 60 | + env: |
| 61 | + RELEASE_VERSION: ${{ steps.release.outputs.version }} |
| 62 | + run: | |
| 63 | + node -e ' |
| 64 | + const fs = require("node:fs"); |
| 65 | + const packageJson = JSON.parse(fs.readFileSync("package.json", "utf8")); |
| 66 | + packageJson.version = process.env.RELEASE_VERSION; |
| 67 | + fs.writeFileSync("package.json", `${JSON.stringify(packageJson, null, 2)}\n`); |
| 68 | + ' |
| 69 | +
|
| 70 | + - name: Build package |
| 71 | + if: steps.release.outputs.version != '' |
| 72 | + run: pnpm run generate |
| 73 | + |
| 74 | + - name: Check if version is already published |
| 75 | + id: registry |
| 76 | + if: steps.release.outputs.version != '' |
| 77 | + env: |
| 78 | + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 79 | + PACKAGE_NAME: "@smakss/random-string" |
| 80 | + RELEASE_VERSION: ${{ steps.release.outputs.version }} |
| 81 | + run: | |
| 82 | + if npm view "${PACKAGE_NAME}@${RELEASE_VERSION}" version --registry=https://npm.pkg.github.com/ >"$RUNNER_TEMP/npm-view.log" 2>&1; then |
| 83 | + echo "published=true" >> "$GITHUB_OUTPUT" |
| 84 | + elif grep -Eq 'E404|404 Not Found' "$RUNNER_TEMP/npm-view.log"; then |
| 85 | + echo "published=false" >> "$GITHUB_OUTPUT" |
| 86 | + else |
| 87 | + cat "$RUNNER_TEMP/npm-view.log" >&2 |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | +
|
| 91 | + - name: Publish to GitHub Packages |
| 92 | + if: steps.release.outputs.version != '' && steps.registry.outputs.published != 'true' |
| 93 | + run: pnpm publish --no-git-checks --registry=https://npm.pkg.github.com/ |
| 94 | + env: |
| 95 | + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments