github actions 1

github actions

name: Node.js CI

on:
  push:
    branches: [ master ]
    
jobs:
  build-and-test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x]

    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
    - run: npm i
    - run: npm run build
    - run: npm run test

Here is what the above code is Doing:
1. We’re using the `push` event to trigger the workflow.
2. We’re using the `ubuntu-latest` runner.
3. We’re using the `matrix` strategy to run the workflow on multiple versions of Node.js.
4. We’re using the `actions/checkout@v2` action to checkout the code.
5. We’re using the `actions/setup-node@v2` action to setup Node.js.
6. We’re running `npm i` to install the dependencies.
7. We’re running `npm run build` to build the code.
8. We’re running `npm run test` to run the tests.

Now, let’s commit the changes and push them to GitHub.

“`bash
git add .
git commit -m “Add GitHub Actions workflow”
git push origin master
“`

If you go to the Actions tab on GitHub, you should see the workflow running.

![GitHub Actions](./images/github-actions.png)

## Deploying to GitHub Pages

Now that we have a workflow that builds and tests our code, let’s add a step to deploy the code to GitHub Pages.

First, let’s install the `gh-pages` package.

“`bash
npm i gh-pages
“`

Next, let’s add a `deploy` script to the `package.json` file.

“`json
{
“name”: “github-actions-demo”,
“version”: “1.0.0”,
“description”: “”,
“main”: “index.js”,
“scripts”: {
“build”: “webpack”,
“test”: “jest”,
“deploy”: “gh-pages -d dist”
},
“keywords”: [],
“author”: “”,
“license”: “ISC”,
“devDependencies”: {
“@babel/core”: “^7.12.3”,
“@babel/preset-env”: “^7.12.1”,
“babel-loader”: “^8.1.0”,
“jest”: “^26.4.2”,
“webpack”: “^4.44.1”,
“webpack-cli”: “^3.3.12”
},
“dependencies”: {
“gh-pages”: “^3.1.0”
}
}
“`

Now, let’s add a step to the workflow to deploy the code to GitHub Pages.

“`yaml
name: Node.js CI

on:
push:
branches: [ master ]

jobs:
build-and-test:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x]

steps:
– uses: actions/checkout@v2
– name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: ‘npm’
– run: npm i
– run: npm run build
– run: npm run test
– run: npm run deploy

“`

Now, let’s commit the changes and push them to GitHub.

“`bash
git add .
git commit -m “Add GitHub Pages deployment”
git push

Similar Posts