What is Github actions?
Tags: github, cicd, javascript, devops
Published: 2022-03-08 20:10:49

In simple word Github actions is a platform to automate developer workflows. it's not exactly a CI/CD but CI/CD is one of many workflows it can manage. 


### Some of the workflows such as
New issue , new pull request, merge code, test, build, deploy and so on.

### Why Github actions is created? 
As a developer you definitely want to automate as much as possible of those management workflows so you can concentrate on programming and developing new features and new functionalities in your projects, and then let GitHub actions automate them when something happens to your repositories.


> _For instance when a new issue is created you can create an action to assign it to a specific contributor or yourself by default._

### Basically there are two main steps for creating a workflow
1.  [listen to event](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows)
2.  [trigger a workflow ](https://docs.github.com/en/actions/using-workflows/triggering-a-workflow)

To get a better insight into Github action, let's take a look at the CI/CD workflow.

### CI/CD with Github actions
The most common workflow for your repository would be CI/CD, it's not important what is your stack, Github Actions can cover all of them 

**Maybe you asked yourself why I should use Github CI/CD instead of a lot of other options?**
I can say when you are using Github for your repository then using the same tool instead of third-party integration besides an easy setup pipeline makes it a perfect option. 

I'm going to create a new repository with ReactJs and then add a workflow to that, you can find it [here](https://github.com/hshoja/Currency-Exchange/blob/main/.github/workflows/node.js.yml). 

```yaml
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node

name: Node.js CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:

    runs-on: ubuntu-latest

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

    steps:
    - name: Checkout
    - uses: actions/checkout@v2
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v2
      with:
        node-version: ${{ matrix.node-version }}
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test
```

### Short description of the workflow:
- **name**: [optional] define a name for your CI/CD workflow
- **on**: [required] name of Github event that triggers the workflow
- **jobs**: one or more jobs the workflow that will be triggered
- **steps**: [required] can run commands, setup tasks or run an action
  - _uses_ : selects an action
  - _run_ : runs a command-line command

### Actions
- `actions/checkout@v2`: This actions is maintained by Github, it checks out the repo and pulls it in our pipeline so the workflow can access it


### Where do all these things happen? and how do they get executed ?
workflows on Github actions will be executed on Github servers so it's managed by Github and you don't need to set up some servers or configure some plugins, Github will manage all for you.  

### Some tips:
**Tip 1**: for every single job a new fresh server will be prepared. 
**Tip 2**: Jobs run in parallel by default but you can make them sequential with `needs` for instance

```yaml
jobs:
  build:
    runs-on: ubuntu-latest
    ...
  publish:
    needs: build
    ...
```

**Tip 3**: if we want to run the workflow on different OS or on the different environments for instance different NodeJs versions we can use `matrix`

For example this workflow will run on 3 different OS

```yaml
runs-on: ${{matrix.os}}
strategy:
matrix:
os:[ubuntu-latest, windows-latest,macOS-latest]
```

**Tip 4**: these all run in parallel on 3 different Os


Hope this article can help you to use Github actions better.

refs:
https://www.youtube.com/watch?v=R8_veQiYBjI