
哈Ha 本文是关于如何使用github操作自动化您的项目。
自动化将分为:
- CI-将在每次提交时执行测试
- 发布-在发布时,我们在npm中发布版本
CI
在我们的存储库中 (可以在此处阅读如何了解),其中包含以下脚本:
npm run build
构建npm test
-测试npm run codestyle
代码样式检查
我们将同时在3个版本的节点(8、10、12)上运行构建和测试,并同时检查代码样式
.github/workflows/CI.yml
文件将负责此工作流程:
name: Node CI on: push jobs: buildAndTest: runs-on: ubuntu-latest strategy: matrix: node-version: [8.x, 10.x, 12.x] steps: - uses: actions/checkout@v1 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - name: Install, build, test run: | npm install npm run build npm test env: CI: true checkCodestyle: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: '8.x' - name: Install and check codestyle run: | npm install npm run codestyle env: CI: true
让我们分析一下它的内容
name: Node CI
包含工作流名称的name: Node CI
字符串
on: push
我们将在推送上运行工作流程
jobs:
这是要完成的任务
Job buildAndTest
:
buildAndTest: # runs-on: ubuntu-latest # strategy: # matrix: # node-version: [8.x, 10.x, 12.x] # steps: # - uses: actions/checkout@v1 - name: Use Node.js ${{ matrix.node-version }} # uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - name: Install, build, test # run: | npm install npm run build npm test env: # CI CI: true
您会注意到一个有趣的构造${{ something }}
我称之为上下文调用,在某些情况下something
某些表达式,在我们的例子中是matrix.node-version
,也就是说,我们在matrix
对象中获得了一个名为node-version
。 matrix
存储当前运行中指定的变量的值,在我们的例子中,以三个开始,值8.x
, 10.x
, 12.x
将位于此处
CheckCodestyle作业
checkCodestyle: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v1 with: node-version: '8.x' - name: Install and check codestyle run: | npm install npm run codestyle env: CI: true
好吧,这里的一切都类似于第一个
发布
name: Publish # workflow on: release # ( ) jobs: # test: # , runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions/setup-node@v1 with: node-version: 12 - run: npm ci - run: npm run build - run: npm test - run: npm run codestyle publish: # needs: test # runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions/setup-node@v1 with: node-version: 12 # 12 registry-url: https://registry.npmjs.org/ - run: npm run build # - name: Publish beta # if: github.event.prerelease == true # run: npm publish --tag beta env: NODE_AUTH_TOKEN: ${{secrets.npm}} - name: Publish stable # if: github.event.prerelease == false # run: npm publish --tag beta env: NODE_AUTH_TOKEN: ${{secrets.npm}} - name: Build and Deploy ds # uses: JamesIves/github-pages-deploy-action@master if: github.event.prerelease == false env: ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} BRANCH: gh-pages FOLDER: docs BUILD_SCRIPT: npm install && npm run typedoc ##
这里有趣的是, ${{ secrets.SOMETHING }}
是对SOMETHING
对象中SOMETHING
元素的调用
在这里,我提到了两个秘密含义:
npm
-NPM令牌ACCESS_TOKEN
访问gh页(github令牌)
可以在项目设置中创建秘密
同样有趣的是
if: github.event.prerelease == true if: github.event.prerelease == false
如果允许您告诉github采取哪些步骤,哪些不采取。
为此,我们取值github.event.prerelease
, github.event
包含在webhook版本中收到的json对象,如果为true
则发布beta,
如果为false
则发布稳定并停靠
这就是使用github操作自动化一切的简单程度
参考文献
最终仓库
Npm库
文件范例
Github动作文档
处理上下文
使用秘密价值
Webhook发布的描述