Browse Source

Add Github Actions

杜府 3 years ago
parent
commit
5e71f91bde
2 changed files with 119 additions and 0 deletions
  1. 31 0
      .github/workflows/auto-public-npm.yml
  2. 88 0
      .github/workflows/auto-releases.yml

+ 31 - 0
.github/workflows/auto-public-npm.yml

@@ -0,0 +1,31 @@
+# 当分支 main 有 push 事件并且 tag 为 v* 时触发
+# 使用 GitHub Actions secrets 存储的 NPM_TOKEN 自动将本仓库 components 目录下的所有文件发布到 npm 上
+
+name: Auto publish npm
+
+on:
+    push:
+        branches:
+            - main
+        tags:
+            - 'v*'
+
+jobs:
+    build-and-publish:
+        runs-on: ubuntu-latest
+
+        steps:
+            - name: Checkout
+              uses: actions/checkout@v2
+
+            - name: Setup Node.js environment
+              uses: actions/setup-node@v1
+              with:
+                  node-version: 18.x
+
+            - name: Publish
+              env:
+                  NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
+              run: |
+                  cd components
+                  npm publish --access=public

+ 88 - 0
.github/workflows/auto-releases.yml

@@ -0,0 +1,88 @@
+# 当分支 main 有 push 事件并且 tag 为 v* 时触发,使用 GitHub Actions 执行以下步骤:
+# 读取 tag 内容,例如 v0.0.12,作为 Releases title
+# 读取仓库内的 /doc/guide/changelog.md 中第二个 ## 之前的内容
+# 再读取仓库内的 /doc/guide/changelog_en.md 中第一个二级标题与第二个二级标题之间的内容
+# 将两个内容合并为一个字符串,中间使用 --- 分隔
+# 组成内容示例为:
+# ## 0.0.12
+# -  新增了一个功能。
+# ---
+# -  Add a new feature.
+# 将合并的内容作为 Releases describe
+# Set as the latest release 并发布本次 Release
+
+name: Auto releases
+
+on:
+    push:
+        branches:
+            - main
+        tags:
+            - 'v*'
+
+jobs:
+    release-please:
+        runs-on: ubuntu-latest
+
+        steps:
+            - uses: actions/checkout@v2
+            # 读取 tag 内容,例如 v0.0.12,作为 Releases title
+            - name: Get tag
+              id: tag
+              run: echo ::set-output name=tag::${GITHUB_REF#refs/*/}
+
+            # 读取仓库内的 /doc/guide/changelog.md 中第二个 ## 之前的内容,如
+
+            #  ## 0.0.12
+            #  -  新增了一个功能。
+            #  ## 0.0.11
+
+            # 只取第二个 ## 之前的内容,结果为:
+
+            #  ## 0.0.12
+            #  -  新增了一个功能。
+
+            - name: Get changelog
+              id: changelog
+              run: |
+                  echo ::set-output name=changelog::$(awk '/^## /{n++}{if(n==2){exit}}1' doc/guide/changelog.md)
+
+            # 读取仓库内的 /doc/guide/changelog_en.md 中第一个二级标题与第二个二级标题之间的内容,如
+
+            #  ## 0.0.12
+            #  -  Add a new feature.
+            #  ## 0.0.11
+
+            # 只取第一个二级标题与第二个二级标题之间的内容,结果为:
+
+            #  -  Add a new feature.
+
+            - name: Get changelog_en
+              id: changelog_en
+              run: |
+                  echo ::set-output name=changelog_en::$(awk '/^## /{n++}{if(n==2){exit}}1' doc/guide/changelog_en.md | sed -n '/^## /{n;p;}')
+
+            # 将两个内容合并为一个字符串,中间使用 --- 分隔
+            - name: Get body
+              id: body
+              run: |
+                  echo ::set-output name=body::"${{ steps.changelog.outputs.changelog }}\n---\n${{ steps.changelog_en.outputs.changelog_en }}"
+
+            # 组成内容示例为:
+
+            # ## 0.0.12
+            # -  新增了一个功能。
+            # ---
+            # -  Add a new feature.
+
+            # 将合并的内容作为 Releases describe,并作为 latest release 发布本次 Release
+            - name: Release
+              uses: actions/create-release@v1
+              env:
+                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+              with:
+                  tag_name: ${{ steps.tag.outputs.tag }}
+                  release_name: ${{ steps.tag.outputs.tag }}
+                  body: ${{ steps.body.outputs.body }}
+                  draft: false
+                  prerelease: false