GitHub Actions and Workflows
What is GitHub actions?
“GitHub workflow actions” is similar to triggers in MySQL. a set of constraints that get executed if any changes were done to the repository where the files get modified or updated then git automatic actions will be executed.
Mainly these Actions used to automate the tasks. When so many developers are working on projects and they frequently contributed to the main repository using a pull request, the owner of that project should always validate those pull requests before merging them. But as already mentioned when the number of contributors is higher, it is practically impossible to handle these requests manually. this is where GitHub Actions come to the picture.
Event
Inside Git repository things that happen are known as events, these events can be,
- Pull request
- Creating an issue
- Add contributors to the repository.
- Merging requests to a branch.
And there are so many other events in Git that can trigger actions,
Check out some of the most common events in Git which can be used to create workflow actions.
https://docs.github.com/en/actions/reference/events-that-trigger-workflows
CI/CD Pipelines
This is one example where we can use GitHub actions. This means that when we are developing software in one environment and in the end we want to deploy it in a different environment but this transition will involve so many other environments and frameworks to accomplish this task. therefore if we use a manual procedure configure all these frameworks and environments by our self it will take so much time and confusing if we have to perform this task multiple time during the development phase. Instead what we can do is we can create an automated workflow that will provide us the appropriate environment dealing with all the SDK configurations.
How to create a workflow
In order to create a workflow, we need the following requirements.
- We need to have a Git repository (An existing one or a new repository).
- Once come to the repository we can navigate towards the actions using the actions button right next to “Pull request”.
- There will be suggestions and workflow which associate with other programs (3rd party).
Following are some of the pre-defined workflows available in GitHub.
We can also create our own workflow using a provided template.
This is a template that provides when click on the above link to create a custom workflow.
# This is a basic workflow to help you get started with Actionsname: CI# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
pull_request:
branches: [ main ]# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called “build”
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
— uses: actions/checkout@v2# Runs a single command using the runners shell
— name: Run a one-line script
run: echo Hello, world!# Runs a set of commands using the runners shell
— name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
name:
is the name of the workflow and with on:
we can specifically mention the event that we going to use for this event, in the above example what I have given is a push event, therefore, we can specify which branch (Main branch or another branch) that we will be using in order to trigger this workflow.
jobs:
in this section, we will specify the set of actions that we need to execute when running the workflow.
uses: actions/checkout@v2
before running the test we have to execute this line in order to check out the repository, checking out simply means that we choosing a branch to perform the test. So these instruction are given inside a folder structure .github/workflows/action.yml
since these files are common pre-written action.yml
the file can get from the official repositories and use in our own workflows.
Download the action.yml file (ref:github.com/)
When we need to create an environment with a particular language. we can use used:
with with:
keywords. the example is given below for setting up a java environment.
runs-on: ubuntu-latest
above line code define which OS the workflow will use when executing, GitHub actually provides three Operating systems to test our programs as follows.
- ubuntu-latest
- windows-latest
- macOS-latest
we can use these commands individually or as a combination so that test will carry out for each OS system parallelly.
uses: action/setup-java@v1with:java-version: 1.8
Another keyword that we can use is run:
which can use to execute or run command during the testing process. for example when we need to run a Gradle build.
name: Make the gradlew file runnable run: chmod +x gradlewname: Run the Gradle file.run: ./gradlew build
then we can save the configurations and commit the changes with the newly added file.
To make sure everything working correctly we can create another branch in the repository and try to merge it with the main branch.
Following is the output of the test result when executing the event.
Manually run workflows
even though these are meant to run automatically we can also run them when we need them. (it is possible to run all the workflows configured in a repository).
Conclusion
In this article, I have briefly explained the basic need and how to make use of these actions provided by Git, Check out the links below if you need more information.
Official repositories for the Actions — https://github.com/actions
GitHub documentation on Git actions and workflow — https://docs.github.com/en/actions