|
| 1 | +name: Release and Publish |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +concurrency: |
| 9 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 10 | + cancel-in-progress: true |
| 11 | + |
| 12 | +env: |
| 13 | + NODE_VERSION: "20" |
| 14 | + REGISTRY_URL: "https://npm.pkg.github.com" |
| 15 | + SCOPE: "@quantfive" |
| 16 | + |
| 17 | +jobs: |
| 18 | + check-version: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + permissions: |
| 21 | + contents: read |
| 22 | + packages: read |
| 23 | + outputs: |
| 24 | + version_changed: ${{ steps.version_check.outputs.changed }} |
| 25 | + new_version: ${{ steps.version_check.outputs.version }} |
| 26 | + steps: |
| 27 | + - name: Checkout |
| 28 | + uses: actions/checkout@v4 |
| 29 | + with: |
| 30 | + fetch-depth: 0 |
| 31 | + |
| 32 | + # Scope registry to @quantfive so public deps still come from npmjs.org |
| 33 | + - name: Setup Node (scoped registry) |
| 34 | + uses: actions/setup-node@v4 |
| 35 | + with: |
| 36 | + node-version: ${{ env.NODE_VERSION }} |
| 37 | + cache: npm |
| 38 | + registry-url: ${{ env.REGISTRY_URL }} |
| 39 | + scope: ${{ env.SCOPE }} |
| 40 | + |
| 41 | + - name: Install dependencies |
| 42 | + run: npm ci |
| 43 | + env: |
| 44 | + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 45 | + |
| 46 | + - name: Check if version should be published |
| 47 | + id: version_check |
| 48 | + env: |
| 49 | + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 50 | + run: | |
| 51 | + CURRENT_VERSION=$(node -p "require('./package.json').version") |
| 52 | + LATEST_PUBLISHED=$(npm view @quantfive/codepress-engine version 2>/dev/null || echo "0.0.0") |
| 53 | + echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 54 | + echo "latest_published=$LATEST_PUBLISHED" >> $GITHUB_OUTPUT |
| 55 | +
|
| 56 | + if [ "$CURRENT_VERSION" != "$LATEST_PUBLISHED" ]; then |
| 57 | + echo "changed=true" >> $GITHUB_OUTPUT |
| 58 | + echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 59 | + else |
| 60 | + echo "changed=false" >> $GITHUB_OUTPUT |
| 61 | + fi |
| 62 | +
|
| 63 | + build-and-publish: |
| 64 | + needs: check-version |
| 65 | + if: needs.check-version.outputs.version_changed == 'true' |
| 66 | + runs-on: ubuntu-latest |
| 67 | + permissions: |
| 68 | + contents: write |
| 69 | + packages: write |
| 70 | + steps: |
| 71 | + - name: Checkout |
| 72 | + uses: actions/checkout@v4 |
| 73 | + |
| 74 | + - name: Setup Node (scoped registry) |
| 75 | + uses: actions/setup-node@v4 |
| 76 | + with: |
| 77 | + node-version: ${{ env.NODE_VERSION }} |
| 78 | + cache: npm |
| 79 | + registry-url: ${{ env.REGISTRY_URL }} |
| 80 | + scope: ${{ env.SCOPE }} |
| 81 | + |
| 82 | + - name: Setup Rust (stable + WASI) |
| 83 | + uses: dtolnay/rust-toolchain@stable |
| 84 | + with: |
| 85 | + targets: wasm32-wasip1 |
| 86 | + |
| 87 | + - name: Install deps |
| 88 | + run: npm ci |
| 89 | + env: |
| 90 | + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 91 | + |
| 92 | + - name: Build JS |
| 93 | + run: npm run build |
| 94 | + |
| 95 | + - name: Build Rust WASM plugin |
| 96 | + run: npm run build:rust |
| 97 | + |
| 98 | + # Optional: verify the pack contents before publishing |
| 99 | + - name: Dry-run pack |
| 100 | + run: npm pack --dry-run |
| 101 | + |
| 102 | + # Race safety: re-check just before publish |
| 103 | + - name: Ensure version not yet published |
| 104 | + id: recheck |
| 105 | + env: |
| 106 | + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 107 | + run: | |
| 108 | + CURRENT_VERSION=$(node -p "require('./package.json').version") |
| 109 | + FOUND=$(npm view @quantfive/codepress-engine versions --json | node -e "let a=JSON.parse(require('fs').readFileSync(0,'utf8'));console.log(a.includes(process.argv[1]))" "$CURRENT_VERSION") |
| 110 | + if [ "$FOUND" = "true" ]; then |
| 111 | + echo "already=true" >> $GITHUB_OUTPUT |
| 112 | + else |
| 113 | + echo "already=false" >> $GITHUB_OUTPUT |
| 114 | + fi |
| 115 | +
|
| 116 | + - name: Publish to GitHub Packages |
| 117 | + if: steps.recheck.outputs.already == 'false' |
| 118 | + run: npm publish |
| 119 | + env: |
| 120 | + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 121 | + |
| 122 | + - name: Create Git tag (after successful publish) |
| 123 | + if: steps.recheck.outputs.already == 'false' |
| 124 | + run: | |
| 125 | + VERSION=${{ needs.check-version.outputs.new_version }} |
| 126 | + git config --local user.email "[email protected]" |
| 127 | + git config --local user.name "GitHub Action" |
| 128 | + git tag -a "v$VERSION" -m "Release v$VERSION" |
| 129 | + git push origin "v$VERSION" |
| 130 | +
|
| 131 | + - name: Create GitHub Release |
| 132 | + uses: softprops/action-gh-release@v2 |
| 133 | + with: |
| 134 | + tag_name: v${{ needs.check-version.outputs.new_version }} |
| 135 | + name: Release v${{ needs.check-version.outputs.new_version }} |
| 136 | + generate_release_notes: true |
| 137 | + env: |
| 138 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments