/mygitactions

In this tutorial, I go over everything you need to know to get started with Github actions.

Primary LanguagePython

image GitHub Actions CI/CD

actions-logo

🤖 Introduction

GitHub Actions is the built-in CI/CD tool for GitHub, CI/CD stands for Continuous Integration and Continuous Delivery, allows us to automate the testing of our code to make sure it meets certain criteria after all the tests are passed you can enable actions to automate the delivery of your code this can significantly reduce the time it takes for you to deliver updates to your application.

📝 Overview

In the lab portion of this tutorial we'll go over how to implement our own GitHub action and implement a Linter that will run checks against our code to make sure that it meets specific criteria.

GitHub actions workflow file walkthrough

Let's go over the terminology you need to know to understand a GitHub actions workflow file so we have a workflow yaml file GitHub actions workflow file walkthrough which specifies events jobs runners steps and actions.

Blank diagram-13-2

On the event that someone pushes new code it's going to run the job Super-lint, That job is going to run on ubuntu container hosted on GitHub, Then it's going to go through two steps: the first step is to check out the code and the next step is to run the Linter.

NOTE: Linter is it's just something that we use to check to make sure that our code is conforming to certain standards super linter is made up of multiple linters so it doesn't matter which code you use in your repository superlinter is going to understand it and make sure you conform to the standards of that language.

Step 1️⃣ : GitHub actions Lab - Creating Repository and setting up Action

  1. Create a new repository

Navigate to your GitHub account (if you don't have one, go to github.com and create an account), go to "Repository" and click "New"

Screenshot 2024-01-31 at 13 14 02

  1. We can call this whatever we want, i'll name it mygetactions and then scroll down and let's add a README.md file just to have a file to work with and then "Create repository"

Screenshot 2024-01-31 at 13 16 31

  1. The next thing we want to do is create a workflow, to do that just go "Add file" and click "Create new file"

Screenshot 2024-01-31 at 13 17 25

  1. We need to be very specific with our naming, the proper directory structure for your workflow files needs to be first .github/workflows and then you can name your file whatever you want, i'll name it superlinter.yml

Screenshot 2024-01-31 at 13 20 50

  1. Then i'm going to copy and paste the code, you can see this is the code that we went over earlier, basically it's listening for a push event and when the push event happens it's going to first check out our code and then run the superlinter against it to make sure that it conforms to the linting standards
name: Super-Linter

on: push

jobs:
  super-lint:
    name: Lint code base
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Run Super-Linter
        uses: github/super-linter@v4
        env:
          DEFAULT_BRANCH: main
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Screenshot 2024-01-31 at 13 21 59

  1. We can go ahead and commit, you can create a new branch or just commit to the main branch i'm going to commit to the main branch

Screenshot 2024-01-31 at 13 23 36

NOTE: This directory structure needs to be exactly like this if your workflow is not triggering it's probably because the directory structure isn't named correctly

  1. Let's go back to the main repository click "Code" and you should notice that there's this little status icon and you can see it's yellow, basically what this is saying is it's currently running the workflow and checking the code if all the checks pass then this is going to turn green, if the checks fail then it's going to turn red, click "Actions" tab

Screenshot 2024-01-31 at 13 25 34

  1. It will take few seconds and run the workflow, and the workflow that it's running is super linter, it will:

Set up the job Pull the code from GitHub Super Linter Checkout the code Run Super-Linter Post Chechout code Complete job

Screenshot 2024-01-31 at 13 30 18

  1. If we head on back to code here we can see we get a nice little check mark to show that all the workflow checks have passed

Screenshot 2024-01-31 at 13 32 24

NOTE: This is another really cool thing that you can actually use downstream you could have your servers monitor the GitHub API and just check the status of the repository, if the status was a green check mark it could automatically go to that repository and pull down your code and implement it into staging or production or whatever you want.

Step 2️⃣ : Let's test and push some code that we know is going to generate some Linting Errors

  1. Go to "Add file" then click "Create new file"
  2. Let's name it main.py
  3. Copy and paste this simple python code below:

NOTE: There's some syntax problems with this code that is not going to pass the Linting check (for example, there should actually be two lines between each function, the code style isn't very consistent this is what's considered messy code and the Linter is not gonna like, it it's gonna fail it )

    def hello():
         print("hi")

def bye():
  print("bye")

print(hello())
  1. Let's go ahead and save this commit it and watch to see what the Linter does
  2. Let's refresh and we can see that it is triggered now if we just click the yellow icon, you can it's in progress check and we can just go details

Screenshot 2024-01-31 at 13 43 23

  1. Our Super-Linter has run and we can see that there is an error in our workflow right here on the run Super-Linter, let's go into it and i'll show you sort of how to troubleshoot these errors

Screenshot 2024-01-31 at 13 56 47

  1. Basically the code and the + are the lines that it would add and the - are the lines it would remove.

Screenshot 2024-01-31 at 13 56 47

  1. Let's check and fix the Errors, go to "Edit"
    def hello():
    print("hi")


def bye():
    print("bye")


print(hello())
  1. We can see that everything passed, if you go to actions and check the details of the latest job and you can see that the Super Linter ran and everything looks good.

Screenshot 2024-01-31 at 14 06 26

  1. Let's just explore the GitHub actions a little bit more we'll go up to "Actions" and you can see that we have a history of all our workflow jobs, first one was our first run successfully and other one was the second run we added the python file with the bad formatting and then last one after that we fixed the errors.

Screenshot 2024-01-31 at 14 08 52

NOTE: if you wanted to go through a guided setup instead of just writing your file from scratch like we did you can go new workflow right here and you can see that there's a lot of templated options. There's a marketplace with a lot of canned workflows that you can just grab and then you can just modify the file to meet your needs so that's a really good option

Screenshot 2024-01-31 at 14 10 08