Hello, Habr. This article is about how to automate your project with github actions.
Automation will be divided into:
In our repository (how we got to this can be read here ) for there are the following scripts:
npm run build
npm test
npm run codestyle
.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
Let's analyze its contents
name: Node CI
string containing the name of the workflow
on: push
we will run the workflow on the push
jobs:
this is the task to be completed
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
You can notice an interesting construction ${{ something }}
I call this context call. Where something
some kind of expression, in our case matrix.node-version
, that is, we get a value in the matrix
object called node-version
. matrix
stores the value of the variables specified in the current run, in our case, with three starts, the values 8.x
, 10.x
, 12.x
will lie there
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
Well, here everything is similar to the first
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 ##
Of the interest here, ${{ secrets.SOMETHING }}
is a call to the SOMETHING
element in the SOMETHING
object
Here I turn to two secret meanings:
npm
ACCESS_TOKEN
Secrets can be created in the project settings
And no less interesting lines are
if: github.event.prerelease == true if: github.event.prerelease == false
If allows you to tell github which steps to take and which not.
To do this, we take the value github.event.prerelease
, github.event
contains the json object received in the webhook release, and if it is true
then we publish the beta,
if false
then we publish stable and dock
That's how simple it is to automate everything with github actions
Final repository
Npm library
Documentation Example
Github actions documentation
Work with context
Work with secret values
Description of webhook release