Updated May 16, 2023
What are GitHub Actions
GitHub Action enables developers to follow an agile program development method to deliver results to users quickly without compromising software quality, standards, processes, and security aspects. Actions empower developers to automate the software development lifecycle and provide the flexibility to customize the Workflow during execution.
Actions facilitate Continuous Integration and Continuous Delivery of software from the GitHub repository. This process ensures the swift movement of code under development to production, resulting in several user benefits. These benefits include improved accuracy in application delivery, reduced development costs, and enhanced revenue generation for the organization.
Components of GitHub Actions
Several components in Actions help in configuring a development life cycle, including testing of software till its deployment. Some of the major components are:
Events
Workflows can be triggered or configured to run whenever an event occurs. An event is a specified activity that can happen as part of Actions or scheduled at a particular time within GitHub or happen outside of GitHub.
Type of events that triggers Workflow execution
- Upon the occurrence of a single event. Example – Whenever code is moved into any part of the GitHub repository
- Upon the occurrence of Multiple events, e.g., Whenever one of the listed events like Code push or Code Pull occurs.
- Upon the occurrence of an event with a specific activity type. e.g., Workflow can be triggered whenever code is pushed in the main branch (Activity type) or code is released under created category (Activity type).
- At a particular time as a scheduled event. g. Cronjob can be run in the backend, which will wake up at a scheduled time, trigger workflow, set up the next wake-up time, and go to sleep till that time.
- Manually triggered whenever required.
Workflow
A workflow consists of a set of logically connected procedures that are available in the GitHub repository. As mentioned earlier, these workflows are triggered by different events. Users package workflows as YAML files and can create, test, and deploy them as a project.
A typical workflow contains various Jobs with several steps, each with an action to get the results. Some of the Workflow may be simple, and some may be complex that store sensitive information. Data like certificates/passwords will be stored as secrets in the Workflow as an environment variable and used as a parameter during information retrieval.
GitHub simplifies the creation of workflows by providing them a master template workflow for users to customize and create their Workflow as required.
Jobs
Jobs that run as part of a workflow consist of several steps. These steps execute in the same runner to facilitate data sharing among them. Typically, the jobs in a workflow execute as parallel operations unless there are dependencies between them. There could be a dependent job whose execution can start only upon the completion of another job. In such a case, these two jobs cannot run in parallel. You need to configure them in actions as serial jobs, running sequentially in a sequential mode.
For example, a workflow may consist of build jobs and test jobs configured as serial-dependent jobs. If the Build job fails testing job will not be taken up.
Steps
Steps are part of a Job in GitHub Actions. These steps execute commands, tasks setup, and actions defined in the repository. A step may contain an action or a shell command. But the action runs as a step in a repository and executes as a process in the runner environment, utilizing necessary resources such as the file system and workspace. Since each step operates as its process, GitHub does not retain changes made to environment variables.
Actions
Action can be a discrete task or custom codes to interact with repository objects. You can combine actions into steps and, subsequently, combine steps into jobs as part of a workflow. You can use interfaces within GitHub or third-party interfaces (API) as part of custom code in action. Depending on the need, actions can run on machines or through docker containers.
Runner
A server hosts the application in GitHub Actions that perform assigned tasks. Users can use the runners that GitHub provided or have their runners. Runners execute a job at a time and report back the results to GitHub.
Sample Workflow
To create a sample workflow, a user only needs a GitHub repository. A directory .github/workflows should be created if it is unavailable.
Creation of a sample workflow file
You can create a file named “Github-actions-sample.yml” in the workflows folder with the following contents.
The contents
name: github actions sample
on : [Pull]
jobs :
Test-GitHub-Actions-job1:
Runs on: server-name
Steps:
- run: echo “A Sample Workflow in GitHub Actions – Part I.”
- run: echo “Event $ { { github.event_name } } is triggering this step.”
- run: echo “Server $ { { runner.os } } in GitHub hosts this test application.”
Test-GitHub-Actions-job2:
Runs on: server-name
Steps:
- run: echo “A Sample Workflow in GitHub Actions – Part II.”
- run: echo “Repository name is $ { { github.repository } } “
The above code should be committed to the GitHub repository.
Explanation
Name: github_actions_sample is the name of the Workflow, and it will appear in the GitHub actions repository
On: Pull – This code gets executed whenever some objects are pulled from GitHub
Jobs: Test-GitHub-Actions-job1 and Test-GitHub-Actions-job2, and the steps are defined in
When the job runs
Whenever some users try to pull out any objects from the repository, the above code as part of the will get executed. This code displays the Event name, server name, and repository name.
Conclusion
Overall GitHub Actions simplifies code management activities and shrinks the development time by automating several processes in the software development life cycle.
Recommended Articles
This is a guide to GitHub Actions. Here we discuss the code management activities and shrink the development time by automating several processes. You may also have a look at the following articles to learn more –