Introduction to CI/CD with GitHub Actions
Configuring basic workflow scripts to be familiar with GitHub Actions as a CI/CD tool.
Introduction
Github Actions can be considered as a platform to automate developer workflows. At the same time, GitHub Actions is a CI/CD tool. If you’re new to DevOps, you might think about what’re these workflows are.
If you’re a developer and have used GitHub already, you’d probably use GitHub events such as Pull Requests, Issues, Merging and etc. Once you get an issue you could give it a label and also assign it to a developer who works with you or you could fix it by yourself. So, there’re some particular actions you could take based on the GitHub event.
In this scenario, you complete the task manually. But with GitHub Actions, you could automate the task to be completed based on the criteria you define through the workflow scripts. So, altogether the GitHub Event and the action(s) you take can be identified as a workflow. In simple terms, workflow is a chain of these GitHub events and actions we could take based on the event(s).
These workflow scripts are written in YAML where the YAML stands for YAML ain't Markup Language
and is used for data serialization. Also, YAML is case-sensitive.
Run your First GitHub Actions workflow
Generally, a repository can have multiple workflows and workflows define the event that triggers the action; and also define which action to run. In a GitHub repository workflow path is .github/workflows/<workflow-file-name.yml>
.
But for this example, we just run the default workflow in a GitHub repository. All you need to do is, create a GitHub repository in GitHub and then go to the Actions
tab. There you’d see some workflow templates that you could use, but for this to be a simple exercise, we choose Simple workflow
. Go inside that template and change the file name as you prefer.
After that, you don’t need to edit the content of the file and then click the start commit
button and commit your file. Once you’re done, go back to Actions
tab and you’d see your workflow has queued and then started running.
Then go inside the workflow and you could see the steps that have been taking place.
Run your own GitHub Actions workflow
Let’s create our very own GitHub Actions workflow. As I mentioned earlier GitHub workflows are written in YAML and these workflow scripts have their own attributes. So what I suggest is to head over to the below link and refer to the Understanding the workflow file
section. Once you have just a basic idea you’d understand the pattern easily.
Even before you check into this reference, you can run my workflow script in your GitHub repo and see the output. I will create a new repo called my_own_workflow
and create a YAML file in the below path[when creating YAML files, always make sure you maintain the correct indentation]
.github/workflows/second.yml
Then you can commit your file and as you did in the previous step, you can go back to Actions
tab and see how the workflow runs. Make sure to go inside the workflow and check the output. This workflow basically, prints the Hello World
and the Date
.
You can also run multiple jobs in the same workflow file and all these jobs run parallel. But in the software development process, we might have to depend on the other jobs to be completed prior to a particular job. On such occasions, we can create our job to be dependent on previous jobs as follow.
GitHub Marketplace
GitHub Marketplace is the place where you could find pre-built tools to add to your workflow for improvement. On most occasions, you can get rid of writing large workflow scripts because of using these marketplace tools.
Suppose you have a python code file in your GitHub repo and you could use Python Syntax Checker
marketplace tool to check the syntax of your python code. If it has errors it will be notified by the tool. Let’s create a new repo called python_code_check
and add our python file as below.
Then let’s create the workflow as below.
The last two lines were taken from the Python Syntax Checker tool in the marketplace. You can use this Python Syntax Checker link to navigate to that page and click the Use latest version
button and get the installation commands copied and add them to the workflow script as above.
Then commit the file and head over to Actions
tab.
You can see the Python Syntax Checker
performed its’ task and display the output. To verify the process of this tool, Let’s make an error in the python file as below and run the workflow again to see the output.
Now the workflow gives an error and it shows the place where the syntax error has occurred. Likewise, you can use many tools in the GitHub Marketplace to improve your workflow.
Bonus Tip for you:
You can add a workflow status badge in your README.md
file in the repo to show that your workflow has been passed. It’s always beautiful to have badges in your repo, isn’t it? So, all you need to do is edit the README.md
as below.
![](https://github.com/<owner-name>/<repo-name>/actions/workflows/<workflow-file-name>.yml/badge.svg)
References
Thank you for reading!
Hope you enjoyed, liked and found the information useful in this article.
> Stay tuned for more posts related to DevOps <
Check out my DevOps List on Medium.
Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.
If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇