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.
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.
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
.
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.
- 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"
- 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"
- The next thing we want to do is create a workflow, to do that just go "Add file" and click "Create new file"
- 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 itsuperlinter.yml
- 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 }}
- 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
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
- 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
- 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
- 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
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.
- Go to "Add file" then click "Create new file"
- Let's name it
main.py
- 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())
- Let's go ahead and save this commit it and watch to see what the Linter does
- 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
- 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
- Basically the code and the
+
are the lines that it would add and the-
are the lines it would remove.
- Let's check and fix the Errors, go to "Edit"
def hello():
print("hi")
def bye():
print("bye")
print(hello())
- 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.
- 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.
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