DEV Community

Cover image for Working on GitHub Actions
Priyanshu Agarwal
Priyanshu Agarwal

Posted on • Updated on

Working on GitHub Actions

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


Steps

Create a file checker.yml inside .github/workflows
I recommend checking out the GitHub Documentation 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.

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.

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
Enter fullscreen mode Exit fullscreen mode
  • 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.

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'
Enter fullscreen mode Exit fullscreen mode

Link to my pull request pybamm-team/PyBaMM#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.

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
Enter fullscreen mode Exit fullscreen mode

Link to my pull request pybamm-team/PyBaMM#1371


How to test them?

There are mainly two ways to test workflows:

  • Locally

To test them locally use nektos/act, which is free and open source.
Look at how to install it.
code snippet

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


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.

Top comments (0)