|
| 1 | +name: Auto Release and Publish via package version |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - 'package.json' |
| 9 | + |
| 10 | +jobs: |
| 11 | + |
| 12 | + Version: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + outputs: |
| 15 | + version_changed: ${{ steps.detect.outputs.version_changed }} |
| 16 | + current_version: ${{ steps.detect.outputs.current_version }} |
| 17 | + steps: |
| 18 | + - name: Checkout Code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 2 |
| 22 | + |
| 23 | + - name: Detect version change |
| 24 | + id: detect |
| 25 | + run: | |
| 26 | + CURRENT_VERSION=$(jq -r .version package.json) |
| 27 | + echo "Current version: $CURRENT_VERSION" |
| 28 | + |
| 29 | + git checkout HEAD~1 -- package.json 2>/dev/null || true |
| 30 | + PREVIOUS_VERSION=$(jq -r .version package.json 2>/dev/null || echo "0.0.0") |
| 31 | + echo "Previous version: $PREVIOUS_VERSION" |
| 32 | + |
| 33 | + if [[ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]]; then |
| 34 | + echo "version_changed=true" >> $GITHUB_OUTPUT |
| 35 | + echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 36 | + else |
| 37 | + echo "version_changed=false" >> $GITHUB_OUTPUT |
| 38 | + fi |
| 39 | +
|
| 40 | + Release: |
| 41 | + needs: Version |
| 42 | + if: ${{ needs.Version.outputs.version_changed == 'true' }} |
| 43 | + runs-on: ubuntu-latest |
| 44 | + steps: |
| 45 | + - name: Checkout Code |
| 46 | + uses: actions/checkout@v4 |
| 47 | + |
| 48 | + - name: Create Tag and Release |
| 49 | + uses: actions/github-script@v6 |
| 50 | + env: |
| 51 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 52 | + with: |
| 53 | + script: | |
| 54 | + const version = '${{ needs.Version.outputs.current_version }}'; |
| 55 | + const tagName = `v${version}`; |
| 56 | + |
| 57 | + await github.rest.git.createRef({ |
| 58 | + owner: context.repo.owner, |
| 59 | + repo: context.repo.repo, |
| 60 | + ref: `refs/tags/${tagName}`, |
| 61 | + sha: context.sha |
| 62 | + }); |
| 63 | + |
| 64 | + await github.rest.repos.createRelease({ |
| 65 | + owner: context.repo.owner, |
| 66 | + repo: context.repo.repo, |
| 67 | + tag_name: tagName, |
| 68 | + name: `${tagName}`, |
| 69 | + body: "Install: https://www.npmjs.com/package/oflow-interface", |
| 70 | + draft: false, |
| 71 | + prerelease: false |
| 72 | + }); |
| 73 | + |
| 74 | + |
| 75 | + Publish: |
| 76 | + needs: Version |
| 77 | + if: ${{ needs.Version.outputs.version_changed == 'true' }} |
| 78 | + runs-on: ubuntu-latest |
| 79 | + steps: |
| 80 | + |
| 81 | + - name: Checkout Code |
| 82 | + uses: actions/checkout@v4 |
| 83 | + |
| 84 | + - name: Setup Node.js Environment |
| 85 | + uses: actions/setup-node@v4 |
| 86 | + with: |
| 87 | + node-version: "20" |
| 88 | + registry-url: https://registry.npmjs.org/ |
| 89 | + cache: "yarn" |
| 90 | + |
| 91 | + - name: Install Dependencies |
| 92 | + run: yarn install --frozen-lockfile |
| 93 | + |
| 94 | + - name: Run Unit Tests |
| 95 | + run: yarn test |
| 96 | + |
| 97 | + - name: Build Project |
| 98 | + run: yarn build |
| 99 | + |
| 100 | + - name: Publish to NPM |
| 101 | + run: npm publish |
| 102 | + env: |
| 103 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
0 commit comments