|
| 1 | +name: release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + |
| 8 | +env: |
| 9 | + NODE_VERSION: '18' |
| 10 | + CACHE_KEY: ${{ secrets.CACHE_KEY }} # Change to bust caches |
| 11 | + |
| 12 | +jobs: |
| 13 | + version: |
| 14 | + runs-on: ubuntu-22.04 |
| 15 | + outputs: |
| 16 | + local_version: ${{ steps.local_version.outputs.value }} |
| 17 | + registry_version: ${{ steps.registry_version.outputs.value }} |
| 18 | + steps: |
| 19 | + - name: GitHub context |
| 20 | + env: |
| 21 | + GITHUB_CONTEXT: ${{ toJson(github) }} |
| 22 | + run: echo "$GITHUB_CONTEXT" |
| 23 | + |
| 24 | + - uses: actions/checkout@v3 |
| 25 | + |
| 26 | + - uses: actions/setup-node@v3 |
| 27 | + with: |
| 28 | + node-version: ${{ env.NODE_VERSION }} |
| 29 | + |
| 30 | + - name: Get local version |
| 31 | + id: local_version |
| 32 | + run: | |
| 33 | + version=$(cat ./package.json | jq --raw-output .version) |
| 34 | + echo $version |
| 35 | + echo "value=$version" >> $GITHUB_OUTPUT |
| 36 | +
|
| 37 | + - name: Get registry version |
| 38 | + id: registry_version |
| 39 | + env: |
| 40 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} |
| 41 | + run: | |
| 42 | + package_name="$(cat ./package.json | jq --raw-output .name)" |
| 43 | + echo $package_name |
| 44 | + version=$(npm view $package_name version || echo "0.0.0") |
| 45 | + echo $version |
| 46 | + echo "value=$version" >> $GITHUB_OUTPUT |
| 47 | +
|
| 48 | + - name: Validate version |
| 49 | + uses: actions/github-script@v6 |
| 50 | + with: |
| 51 | + result-encoding: string |
| 52 | + script: | |
| 53 | + const local_version = '${{ steps.local_version.outputs.value }}'; |
| 54 | + const registry_version = '${{ steps.registry_version.outputs.value }}'; |
| 55 | + core.info(`Repository version: ${local_version}`); |
| 56 | + core.info(`Registry version: ${registry_version}`); |
| 57 | + if (registry_version === local_version) { |
| 58 | + core.setFailed('Please bump version before merging'); |
| 59 | + } |
| 60 | +
|
| 61 | + build: |
| 62 | + runs-on: ubuntu-22.04 |
| 63 | + needs: version |
| 64 | + steps: |
| 65 | + - uses: actions/checkout@v3 |
| 66 | + |
| 67 | + - uses: actions/setup-node@v3 |
| 68 | + with: |
| 69 | + node-version: ${{ env.NODE_VERSION }} |
| 70 | + |
| 71 | + - uses: actions/cache@v3 |
| 72 | + id: cache |
| 73 | + with: |
| 74 | + path: node_modules |
| 75 | + key: ${{ runner.os }}-${{ env.CACHE_KEY }}-${{ hashFiles('package-lock.json') }} |
| 76 | + |
| 77 | + - name: Install dependencies |
| 78 | + if: steps.cache.outputs.cache-hit != 'true' |
| 79 | + run: npm i |
| 80 | + |
| 81 | + - run: npm run build |
| 82 | + |
| 83 | + - uses: actions/upload-artifact@v3 |
| 84 | + with: |
| 85 | + name: sdk-${{ needs.version.outputs.local_version }} |
| 86 | + path: dist |
| 87 | + |
| 88 | + publish: |
| 89 | + runs-on: ubuntu-22.04 |
| 90 | + needs: [version, build] |
| 91 | + steps: |
| 92 | + - uses: actions/checkout@v3 |
| 93 | + |
| 94 | + - uses: actions/setup-node@v3 |
| 95 | + with: |
| 96 | + node-version: ${{ env.NODE_VERSION }} |
| 97 | + registry-url: 'https://registry.npmjs.org' |
| 98 | + |
| 99 | + - uses: actions/download-artifact@v3 |
| 100 | + with: |
| 101 | + name: sdk-${{ needs.version.outputs.local_version }} |
| 102 | + path: dist |
| 103 | + |
| 104 | + - name: Publish |
| 105 | + env: |
| 106 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} |
| 107 | + run: npm publish --access public |
| 108 | + |
| 109 | + - name: Create release |
| 110 | + uses: actions/github-script@v6 |
| 111 | + with: |
| 112 | + github-token: '${{ github.token }}' |
| 113 | + script: | |
| 114 | + try { |
| 115 | + const response = await github.rest.repos.createRelease({ |
| 116 | + name: "${{ needs.version.outputs.local_version }}", |
| 117 | + owner: context.repo.owner, |
| 118 | + repo: context.repo.repo, |
| 119 | + tag_name: "${{ needs.version.outputs.local_version }}", |
| 120 | + generate_release_notes: true, |
| 121 | + }); |
| 122 | + } catch (error) { |
| 123 | + core.setFailed(error.message); |
| 124 | + } |
0 commit comments