Working on GitHub Actions
Tags: github, automation, linkchecker, scripts
Published: 2021-02-07 14:34:29

# What are GitHub Actions?
According to GitHub:
> GitHub Actions help you automate tasks within your software development life cycle. GitHub Actions are event-driven, meaning that you can run a series of commands after a specified event has occurred. For example, every time someone creates a pull request for a repository, you can automatically run a command that executes a software testing script.

I used GitHub actions to automate checking invalid links and to run custom scripts in a repository.

![cat gif](https://dev-to-uploads.s3.amazonaws.com/i/39b93b1vq2xalsa0uu2s.gif)
____
# Steps
Create a file `checker.yml` inside `.github/workflows`
I recommend checking out the [GitHub Documentation](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions) for basic syntax. 
____
# Using pre-defined templates
You can use pre-defined action templates and customize inputs according to your needs. You can look for them in the [GitHub marketplace](https://github.com/marketplace?type=actions).

## Link checker
This workflow will check all the files in your repository for broken and invalid links and report them.

- **urlstechie/urlchecker-action**

I used it because I had to use the link checker in various file types like `.md`, `.rst`, and even `.py`
For custom configurations, examples, and more details, refer to the [repository](https://github.com/urlstechie/urlchecker-action).

```yml
name: Check URLs

on:
  push:
  schedule:
    # Run everyday at 3 am UTC 
    - cron: 0 3 * * *

jobs:
  urlchecks:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: URLs-checker
      uses: urlstechie/urlchecker-action@master
      with:
        # A comma-separated list of file types to cover in the URL checks
        file_types: .rst,.md,.py,.ipynb

        # Choose whether to include a file with no URLs in the prints.
        print_all: false

        # Timeout in 10 seconds if url is not reached
        timeout: 10

        # How many times to retry a failed request (each is logged, defaults to 1)
        retry_count: 5
```
- **gaurav-nelson/markdown-link-check**

If you are looking to work with just a single file type, then you should go with this one.
For custom configurations, examples, and more details, refer to the [repository](https://github.com/gaurav-nelson/github-action-markdown-link-check).
```yml
name: Check invalid links

on:
  push:
  pull_request:
  schedule:
    # Run everyday at 3 am UTC 
    - cron: 0 3 * * *

jobs:
  markdown-link-check:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Run link check
      uses: gaurav-nelson/github-action-markdown-link-check@v1
      with:
        use-quiet-mode: 'yes'
        use-verbose-mode: 'yes'
```
Link to my pull request [pybamm-team/PyBaMM#1347](https://github.com/pybamm-team/PyBaMM/pull/1347)
____
# Run custom python scripts and make a pull request
You can also run custom python scripts using workflows and can add a feature to automatically make a pull request to the repository. 
```yml
name: Run custom scripts

on:
  push:
    # Add paths only if you want to run the workflow when a file is modified. 
    paths:
    - 'folder/filename.py'

jobs:
  build:

    runs-on: ubuntu-latest
    strategy:
      # List all the python versions to test it with 
      matrix:
        python-version: [3.7, 3.8]

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
      # Add all the required dependencies
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install flake8 pytest
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Run script
      run: python script.py
      # Automatically create a pull request
    - name: Create Pull Request
      uses: peter-evans/create-pull-request@v3
      with:
        delete-branch: true
        branch-suffix: short-commit-hash
        commit-message: type your commit message
        title: type your title here
        body: |
            Auto-generated pull request
```
Link to my pull request [pybamm-team/PyBaMM#1371](https://github.com/pybamm-team/PyBaMM/pull/1371)
____
# How to test them?
There are mainly two ways to test workflows:

- **Locally**

To test them locally use [nektos/act](https://github.com/nektos/act), which is free and open source.
Look at how to [install it](https://github.com/nektos/act#installation).
![code snippet](https://dev-to-uploads.s3.amazonaws.com/i/9l5no6eukzjhhmqjrcxq.png)

##### Note that GitHub actions currently do not support colors and you may see different results there.

- **Pushing it on the fork**

Another way to do it is by pushing all your files to your fork. It is recommended to create a new branch to test everything in it so that it does not interfere with your current branches.

#### Procedure
Run these commands in your terminal.
- `git checkout -b branchname`
- `git add workflow.yml`
- `git commit -m "workflow test-1"`
- `git push origin branchname`

Then go to your fork and look for `Actions`
![github action](https://dev-to-uploads.s3.amazonaws.com/i/9y664npdj9tzn5rp2njx.PNG)
____

This is just a very small use case of what Github Actions can do. You might have some other way of doing the same thing too.
If you want to discuss more on this feel free to comment below.