|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + tags: |
| 6 | + - 'v*' |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: write |
| 10 | + |
| 11 | +jobs: |
| 12 | + release: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Checkout code |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 0 # 获取完整的 Git 历史,用于生成 commit log |
| 20 | + |
| 21 | + - name: Setup Node.js |
| 22 | + uses: actions/setup-node@v4 |
| 23 | + with: |
| 24 | + node-version: '18' |
| 25 | + registry-url: 'https://registry.npmjs.org' |
| 26 | + |
| 27 | + - name: Install dependencies |
| 28 | + run: npm ci |
| 29 | + |
| 30 | + - name: Build project |
| 31 | + run: npm run build |
| 32 | + |
| 33 | + - name: Run tests |
| 34 | + run: npm test |
| 35 | + continue-on-error: true |
| 36 | + |
| 37 | + - name: Get version from tag |
| 38 | + id: get_version |
| 39 | + run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT |
| 40 | + |
| 41 | + - name: Create tarball |
| 42 | + id: tarball |
| 43 | + run: | |
| 44 | + # npm pack 会输出生成的文件名 |
| 45 | + TARBALL=$(npm pack) |
| 46 | + echo "TARBALL=$TARBALL" >> $GITHUB_OUTPUT |
| 47 | +
|
| 48 | + - name: Generate commit log |
| 49 | + id: commits |
| 50 | + run: | |
| 51 | + CURRENT_TAG="${GITHUB_REF#refs/tags/}" |
| 52 | + echo "Current tag: $CURRENT_TAG" |
| 53 | +
|
| 54 | + # 获取所有 tag 并按版本排序,找到上一个 tag |
| 55 | + PREV_TAG=$(git tag -l 'v*' --sort=-v:refname | grep -A 1 "^${CURRENT_TAG}$" | tail -n 1) |
| 56 | +
|
| 57 | + # 如果 PREV_TAG 等于 CURRENT_TAG,说明没有找到上一个 tag |
| 58 | + if [ "$PREV_TAG" = "$CURRENT_TAG" ] || [ -z "$PREV_TAG" ]; then |
| 59 | + echo "No previous tag found, using recent commits" |
| 60 | + COMMITS=$(git log --pretty=format:"- %s (%h)" --no-merges -20) |
| 61 | + else |
| 62 | + echo "Previous tag: $PREV_TAG" |
| 63 | + # 获取从上一个 tag 到当前 tag 的所有 commit(不包含上一个 tag 的 commit) |
| 64 | + COMMITS=$(git log --pretty=format:"- %s (%h)" --no-merges ${PREV_TAG}..${CURRENT_TAG}) |
| 65 | + fi |
| 66 | +
|
| 67 | + echo "Commits:" |
| 68 | + echo "$COMMITS" |
| 69 | +
|
| 70 | + echo "COMMITS<<EOF" >> $GITHUB_OUTPUT |
| 71 | + echo "$COMMITS" >> $GITHUB_OUTPUT |
| 72 | + echo "EOF" >> $GITHUB_OUTPUT |
| 73 | +
|
| 74 | + - name: Create GitHub Release |
| 75 | + uses: softprops/action-gh-release@v2 |
| 76 | + with: |
| 77 | + name: v${{ steps.get_version.outputs.VERSION }} |
| 78 | + body: | |
| 79 | + ## 更新内容 |
| 80 | +
|
| 81 | + ${{ steps.commits.outputs.COMMITS }} |
| 82 | + files: | |
| 83 | + ${{ steps.tarball.outputs.TARBALL }} |
| 84 | + draft: false |
| 85 | + prerelease: false |
| 86 | + env: |
| 87 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 88 | + |
| 89 | + - name: Publish to npm |
| 90 | + run: npm publish |
| 91 | + env: |
| 92 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 93 | + continue-on-error: true |
0 commit comments