Unleashing the Magic of GitHub Actions: Automating Your Way to Programming Bliss

Unleashing the Magic of GitHub Actions: Automating Your Way to Programming Bliss

From Zero to Hero (Not really) with Code Spells and GitHub Actions!

Hello there, techies! Today, we're going to talk about GitHub actions. But hold on, before we dive into this amazing topic, let me ask you a question. Do you like automating repetitive tasks? Do you love to sit back and watch your computer do all the work for you? Well, if you're like me, then you're going to love GitHub actions!

Image Scanning with GitHub Actions – Sysdig

First things first, let's talk about what GitHub actions are. Simply put, they're little robots that you can program to do things for you automatically. They can be triggered by events, such as a new pull request or a commit, and they can perform a wide range of tasks, from building your code to deploying it.

Now, let's get into the fun stuff. To get started with GitHub actions, you need to create a YAML file in your repository called "workflow". This file defines the actions you want to take and when you want to take them. Let's take a look at an example:

name: My Awesome Workflow
on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Build
        run: make
      - name: Test
        run: make test

This workflow defines a job called "build", which runs on the latest version of Ubuntu. It then checks out the code from the repository using the "actions/checkout" action, builds the code using the "make" command, and runs the tests using the "make test" command. Easy peasy, right?

But wait, there's more! GitHub actions can also be used to create CI pipeline. Let's take a look at another example from my Open Source Organisation called Keribium (I'm the maintainer🤓):

name: Keribium CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  unit-tests:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.9"]

    steps:
      - uses: actions/checkout@v3
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
      - name: Install dependencies
        run: |
          python3 -m pip install --upgrade pip
          pip3 install -r ./requirements/requirements.txt
      - name: Run unit tests
        env:
          SECRET_KEY: ${{ secrets.SECRET_KEY }}
          DJANGO_SETTINGS_MODULE: kerver.settings.prod
        run: python manage.py test --noinput

The GitHub Actions workflow demonstrated creates a unit test job for a Python project that is triggered whenever a push or pull request event occurs in the main branch. The workflow runs on an Ubuntu latest runner and employs the strategy matrix to run the job with various Python versions. The workflow includes the installation of project dependencies as well as the execution of unit tests. SECRET_KEY and DJANGO_SETTINGS_MODULE are environment variables used by the job. The former is a secret key obtained from the GitHub secrets store, while the latter instructs the test runner to use the Django settings module.

And there you have it, folks! That's GitHub actions in a nutshell. Now you can sit back, relax, and let your little robots do all the work for you. Happy coding!

Did you find this article valuable?

Support Khushiyant by becoming a sponsor. Any amount is appreciated!