Automating workflows using GitHub Actions #36419
TauqeerAhmad5201
started this conversation in
Discover
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am a Computer Science undergrad 💻 and totally a student community person who was fascinated 😮 by developer tools and DevOps when I first got into computer programming. My journey started from hardware engineering to developing web applications and backend scripts. Back then, I was learning React.js and developing projects. In hosting webpages using GitHub Pages, I got a glimpse of GitHub Actions. Being curious, I read the YAML file under the workflow directory and the script for CI/CD. Everything started making real sense to me. That moment made me learn more about CI/CD and workflows and how GitHub Actions can serve us. Keep reading to learn how you can use GitHub Actions to automate your workflows.
Lights, camera, and ACTIONS! 📸
Let’s start with the obvious question - What are GitHub Actions? It is a simple continuous integration and continuous delivery (CI/CD) platform used to automate developer workflows.
GitHub Actions uses YAML syntax to define the workflow. Each workflow is stored as a separate YAML file in your code repository, in a directory named .github/workflows.
To put it simply- It’s an action or collection of actions that gets triggered when an event has occurred.
Wait! Then what about CI/CD? Actually, CI/CD (Continuous Integration/Continuous Development) is one of the many workflows that we can automate using GitHub Actions.
What is a workflow?
A workflow is configured to begin once a specific trigger occurs, like when someone opens a pull request. It is an automated process that will run one or more jobs. For more information, see Triggering a workflow. A workflow typically consists of the following components:
Understanding how a workflow works ❓
Now that we know the basics. ✔️ Let’s get started! To keep things super simple, let us consider a real-life situation: collecting milk from the doorstep after it is delivered by a milkman.
Here, the Milk Collection named workflow will be executed when milk will be kept on the door by the milkman.
Once the event gets triggered, making it follow the jobs specified. In this case, there is one job
specified: milkCollection
.Note: There can be multiple jobs which can follow previous workflows, for example,
milkPayment
for paying the bill, or milkMessageAlert which will notify the delivery of milk:milkCollection
will run once we specify the environment under which it should run. Otherwise, it's quite possible it might start working for my neighbor's home, so precisely specifying my home as the environment.Specifying the environment helps the later steps to be executed as per the requirement, allowing the job to finish properly.
Specifying the order of events. Several
steps
should be considered before starting any of the work. For instance, in order to collect milk from our doorstep, we need to leave the current room we’re in, put on slippers, move towards the gate, and then open it.Source: Dreamstime Stock Images
Once we’ve reached the gate and are able to collect the milk, we can now perform the action of placing it into the fridge.
BOOOMMM 💥!!
Source: Insider
Before moving ahead, you should know how and when we use Events!
An event is any activity that happens in a GitHub repository and triggers a workflow run. It can include opening an issue, creating a pull request, making a commit, merging a PR, and/or joining a contributor. For instance, in our use case above ➡️
on: [when kept on door by milkman]
is an event that triggers the workflow(process of delivering the milk). Here’s a complete list of events that triggers workflows.How can GitHub Actions help automate this workflow? 5 simple steps to set up a workflow
In order to automate your workflow and add the Actions template, let's build pipelines and workflows in our repository to have a practical demonstration. The key is to build breakpoints in a workflow with dependencies with conditional inputs.
Situation: We have an open-source repository and we wish to greet every new contributor.
We are all set with a repository, ready for open-source contribution. People can fork it to contribute, but to enable that, we will set up a workflow into the YAML file which will do the required actions.
For setting up a workflow file .yml extension, follow these steps —
1️⃣ Step 1: Click on the Actions tab, which is present in the repo.
2️⃣ Step 2: Click on ‘set up a workflow yourself’.
Note: GitHub will automatically create a
.github/workflows directory
for handling the YAML files.You can use this workflow for your own setup. Just remember that in order to trigger a workflow, a pull_request_target or issues event should take place.
Note:
actions/first-interaction@v1
is being used. ‘Actions’ has its document which is a script, helping it quickly write a script in your workflow that uses the GitHub API and the workflow run context. Read more about it here.3️⃣ Step 3: Make it available to the public and ready for a Pull Request.
The picture shows how a contributor is creating its pull request into the repository ‘actions-opensource’ where it will be triggering the workflow.
As soon as workflow is triggered, actions will be checking the required dependencies.
4️⃣ Step 4: GitHub-actions bot in action 🎊
After creating a PR successfully, the GitHub-actions bot will greet them with a welcome message since it is the very first Pull Request from the contributor.
The GitHub-actions bot can also be customized into any other greeting message, for instance when someone creates their first issue in this repo.
5️⃣ Step 5: The Pull Request gets accepted or updated based on the solution.
The final step is the acceptance of the Pull Request.
Hurray! 🎊 You’ve got the usage of workflow precisely using GitHub Actions. You have learnt how a workflow is set up and its structure. Keeping a note for this blog, the workflow was triggered by a pull request made into a repository. The triggering process will also be observed when an issue is raised in the repository.
Some common issues that can be faced while or after setting up the workflows:
workload_identity_provider
orcredentials_json
- This simply happens whenenv.SERVICE_JSON
is defined in the workflow for JSON and the fork tries to inject secrets using pull requests.Hoping that you can now automate your workflows using GitHub Actions and enjoy the real sense of the CI/CD pipeline. 🚀
TL;DR:
References:
Beta Was this translation helpful? Give feedback.
All reactions