Develop Continuous Integration / Continuous Delivery workflows using GitHub Actions.
Discuss CI/CD using Visualize Git.
Using what you learned from the previous section...
1. CI: Build, test, and audit this app across the LTS and Current version of Node.js upon every push and every pull request.
Hints
-
Uh oh, are the tests broken? 😄
-
npm
can do quite a bit, including auditing software for known vulnerabilities. -
Explore workflow
strategy
.
Solution
- Start with the "Node.js CI" template workflow, target the right versions of node, add
npm audit
:
name: Continuous Integration
on: [push, pull_request]
jobs:
ci:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 13.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm audit
- run: npm run build --if-present
- run: npm test
env:
CI: true
npm audit fix
- Fix the broken test.
2. CD: Publish this library to GitHub Packages upon push to the master
branch.
Hints
-
Is there another starter template workflow that can help?
-
GitHub Packages used scoped NPM packages – you'll need to adjust the package name in
package.json
(andpackage-lock.json
vianpm install
) to your username. -
Can you combine CI and CD into one workflow?
Solution
name: Continuous Integration and Delivery
on: [push, pull_request]
jobs:
ci:
runs-on: [ubuntu-latest]
strategy:
matrix:
node-version: [12.x, 13.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm audit
- run: npm run build --if-present
- run: npm test
env:
CI: true
cd:
needs: ci
if: github.ref == 'refs/heads/master' && github.event_name == 'push'
runs-on: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 12
registry-url: https://npm.pkg.github.com/
scope: '@my-username'
- run: npm i
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
- Hook one of your own projects up to GitHub Actions CI/CD