|
| 1 | +name: Publish |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + id-token: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + publish: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + environment: publish |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Check out |
| 20 | + uses: actions/checkout@v4 |
| 21 | + |
| 22 | + - name: Set up Node |
| 23 | + uses: actions/setup-node@v4 |
| 24 | + with: |
| 25 | + node-version: 24 |
| 26 | + registry-url: https://registry.npmjs.org |
| 27 | + |
| 28 | + - name: Set up Yarn |
| 29 | + run: | |
| 30 | + corepack enable |
| 31 | + corepack prepare yarn@4.5.0 --activate |
| 32 | +
|
| 33 | + - id: pkg |
| 34 | + name: Read Package Version |
| 35 | + run: | |
| 36 | + echo "name=$(node -p \"require('./package.json').name\")" >> $GITHUB_OUTPUT |
| 37 | + echo "version=$(node -p \"require('./package.json').version\")" >> $GITHUB_OUTPUT |
| 38 | +
|
| 39 | + - id: version-cache |
| 40 | + name: Restore Version Cache |
| 41 | + uses: actions/cache/restore@v4 |
| 42 | + with: |
| 43 | + path: .github/.cache/publish-version |
| 44 | + key: publish-version-${{ steps.pkg.outputs.name }}-${{ steps.pkg.outputs.version }} |
| 45 | + restore-keys: | |
| 46 | + publish-version-${{ steps.pkg.outputs.name }}- |
| 47 | +
|
| 48 | + - id: version |
| 49 | + name: Check Version Change |
| 50 | + shell: bash |
| 51 | + run: | |
| 52 | + mkdir -p .github/.cache/publish-version |
| 53 | +
|
| 54 | + current='${{ steps.pkg.outputs.version }}' |
| 55 | + previous='' |
| 56 | +
|
| 57 | + if [ -f .github/.cache/publish-version/version.txt ]; then |
| 58 | + previous="$(cat .github/.cache/publish-version/version.txt)" |
| 59 | + fi |
| 60 | +
|
| 61 | + echo "previous=$previous" >> $GITHUB_OUTPUT |
| 62 | +
|
| 63 | + if [ "$previous" = "$current" ]; then |
| 64 | + echo 'changed=false' >> $GITHUB_OUTPUT |
| 65 | + echo 'package.json version unchanged, skip publish.' |
| 66 | + exit 0 |
| 67 | + fi |
| 68 | +
|
| 69 | + printf '%s' "$current" > .github/.cache/publish-version/version.txt |
| 70 | + echo 'changed=true' >> $GITHUB_OUTPUT |
| 71 | + echo "Version changed: $previous -> $current" |
| 72 | +
|
| 73 | + - name: Install |
| 74 | + if: steps.version.outputs.changed == 'true' |
| 75 | + run: yarn |
| 76 | + |
| 77 | + - name: Build |
| 78 | + if: steps.version.outputs.changed == 'true' |
| 79 | + run: yarn build |
| 80 | + |
| 81 | + - name: Publish To npm |
| 82 | + if: steps.version.outputs.changed == 'true' |
| 83 | + run: yarn yakumo publish --registry https://registry.npmjs.org |
| 84 | + env: |
| 85 | + NPM_CONFIG_PROVENANCE: true |
| 86 | + YARN_NPM_CONFIG_PROVENANCE: true |
| 87 | + |
| 88 | + - name: Save Version Cache |
| 89 | + if: steps.version.outputs.changed == 'true' && success() |
| 90 | + uses: actions/cache/save@v4 |
| 91 | + with: |
| 92 | + path: .github/.cache/publish-version |
| 93 | + key: publish-version-${{ steps.pkg.outputs.name }}-${{ steps.pkg.outputs.version }} |
0 commit comments