|
| 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 | + |
| 19 | + - name: Setup Node.js |
| 20 | + uses: actions/setup-node@v4 |
| 21 | + with: |
| 22 | + node-version: '18' |
| 23 | + registry-url: 'https://registry.npmjs.org' |
| 24 | + |
| 25 | + - name: Install dependencies |
| 26 | + run: npm ci |
| 27 | + |
| 28 | + - name: Build project |
| 29 | + run: npm run build |
| 30 | + |
| 31 | + - name: Run tests |
| 32 | + run: npm test |
| 33 | + continue-on-error: true |
| 34 | + |
| 35 | + - name: Get version from tag |
| 36 | + id: get_version |
| 37 | + run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT |
| 38 | + |
| 39 | + - name: Create tarball |
| 40 | + id: tarball |
| 41 | + run: | |
| 42 | + # npm pack 会输出生成的文件名 |
| 43 | + TARBALL=$(npm pack) |
| 44 | + echo "TARBALL=$TARBALL" >> $GITHUB_OUTPUT |
| 45 | +
|
| 46 | + - name: Get previous tag |
| 47 | + id: prev_tag |
| 48 | + run: | |
| 49 | + # 获取上一个 tag |
| 50 | + PREV_TAG=$(git describe --tags --abbrev=0 HEAD^ 2>/dev/null || echo "") |
| 51 | + echo "PREV_TAG=$PREV_TAG" >> $GITHUB_OUTPUT |
| 52 | +
|
| 53 | + - name: Generate commit log |
| 54 | + id: commits |
| 55 | + run: | |
| 56 | + PREV_TAG="${{ steps.prev_tag.outputs.PREV_TAG }}" |
| 57 | + if [ -z "$PREV_TAG" ]; then |
| 58 | + # 如果没有上一个 tag,获取所有 commit |
| 59 | + COMMITS=$(git log --pretty=format:"- %s (%h)" --no-merges HEAD~20..HEAD 2>/dev/null || git log --pretty=format:"- %s (%h)" --no-merges -20) |
| 60 | + else |
| 61 | + # 获取从上一个 tag 到当前的所有 commit |
| 62 | + COMMITS=$(git log --pretty=format:"- %s (%h)" --no-merges ${PREV_TAG}..HEAD) |
| 63 | + fi |
| 64 | + echo "COMMITS<<EOF" >> $GITHUB_OUTPUT |
| 65 | + echo "$COMMITS" >> $GITHUB_OUTPUT |
| 66 | + echo "EOF" >> $GITHUB_OUTPUT |
| 67 | +
|
| 68 | + - name: Create GitHub Release |
| 69 | + uses: softprops/action-gh-release@v2 |
| 70 | + with: |
| 71 | + name: v${{ steps.get_version.outputs.VERSION }} |
| 72 | + body: | |
| 73 | + ## 更新内容 |
| 74 | +
|
| 75 | + ${{ steps.commits.outputs.COMMITS }} |
| 76 | +
|
| 77 | + ## 安装方式 |
| 78 | +
|
| 79 | + ```bash |
| 80 | + # 使用 npm 全局安装 |
| 81 | + npm install -g code996 |
| 82 | +
|
| 83 | + # 或使用 npx 直接运行 |
| 84 | + npx code996 |
| 85 | + ``` |
| 86 | +
|
| 87 | + ## 下载 |
| 88 | +
|
| 89 | + - `${{ steps.tarball.outputs.TARBALL }}` - npm 包 |
| 90 | + files: | |
| 91 | + ${{ steps.tarball.outputs.TARBALL }} |
| 92 | + draft: false |
| 93 | + prerelease: false |
| 94 | + env: |
| 95 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 96 | + |
| 97 | + - name: Publish to npm |
| 98 | + run: npm publish |
| 99 | + env: |
| 100 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 101 | + continue-on-error: true |
| 102 | + |
0 commit comments