/101-ci-cd

CI/CD with GitHub Actions @ Open Source 101: Columbia

Primary LanguageJavaScript

Open Source 101: CI/CD

🎯 Goal

Develop Continuous Integration / Continuous Delivery workflows using GitHub Actions.

💻 Steps

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
  1. Uh oh, are the tests broken? 😄

  2. npm can do quite a bit, including auditing software for known vulnerabilities.

  3. Explore workflow strategy.

Solution
  1. 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

  1. npm audit fix
  2. Fix the broken test.

2. CD: Publish this library to GitHub Packages upon push to the master branch.

Hints
  1. Is there another starter template workflow that can help?

  2. GitHub Packages used scoped NPM packages – you'll need to adjust the package name in package.json (and package-lock.json via npm install) to your username.

  3. 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}}
    

🏅 Extra credit

  • Hook one of your own projects up to GitHub Actions CI/CD