Prepearing for GitHub certification - Automate your workflow with GitHub Actions
Table of Contents
- Prepearing for GitHub certification - Automate your workflow with GitHub Actions
- Trigger the workflow on push or pull request,
- but only for the master branch
- Also trigger on page_build, as well as release created events
- Reference a specific commit
- Reference the major version of a release
- Reference a minor version of a release
- Reference a branch
How does GitHub Actions automate development tasks?
GitHub decreases the time from idea to deployment
- Communication - eg. code reviews in pull requests, GitHub issues, project boards, wikis, notification, etc.
- Automation - GitHub Actions enables to automate workflows in the software development process (CI/CD).
What is GitHub Actions?
- GitHub Actions are packages scripts to automate tasks.
- Can be configured to trigger complex workflows eg. each check in to a specific branch, at timed intervals or manually
- Scripts that adhere to a yml data format
Using open source GitHub actions
- Some recommended community standards with open source should include:
- README
- code of conduct
- contributing file
- issue templates
- Recommendations before using open source GitHub actions:
- Review actions
action.yml
file for inputs, outputs and make sure the code does what it says it does - Check if the GH action is in the GitHub Marketplace - not mandatory
- Check if action is verified in the FH Marketülace - means GitHub has approved the use of this action
- Includes the version of the action you are using by specifying a Git ref, SHA, or tag
- Review actions
Two types of GitHub actions
- Container Actions:
- the environment is part of the actions code
- Can only be run in Linux environment that GitHub hosts
- Supports different languages
- JavaScript Actions:
- don’t include environment in the code
- need to specify the environment to execute these actions
- can run in a VM in the cloud or on-prem
- support Linux, macOS, and Windows environments.
The anatomy of a GitHub action
- Samples of actions:
- Actions that performs a git checkout
- Container action
- To get more info check out my other post: Create and publish custom GitHub actions
What is a GitHub Actions workflow?
- A GitHub Actions workflow is a process that you setup in your repository to automate software development life cycle tasks.
- You can build, test, package, release, or deploy any project on GitHub
- Workflow consists on actions in a .yml file in the
.github/workflows
directory in your GH repository - Sample:
on:
attribute is a trigger that specifies when the workflow will run- In the sample it runs when there is a push event
- Can specify single
on: push
or an arry of eventson: [push, pull_request]
or an event configuration map that schedules workflow or restricts the execution of a workflow to specific files, tags, or branch changes
- event will trigger on all activities types unless specified otherwise
- More on events
- Workflow must have one job - a job is a section of the workflow that will be associated with a runner
- runner - machine/server where a job will run (GH hosted or self-hosted).
- runner is specified with
runs-on:
attribute - More info on workflow syntax
GitHub-hosted versus self-hosted runners
- GitHub-hosted runners or self-hosted runners. If you use a GitHub-hosted runner, each job runs in a fresh instance of a virtual environment that is specified by the GitHub-hosted runner type you define,
runs-on: {operating system-version}
. - Self-hosted runners, you need to apply the self-hosted label, its operating system, and the system architecture
runs-on: [self-hosted, linux, ARM32]
GitHub Actions may have usage limits
Identify the components of GitHub Action
Workflow
- Is an automated process that you add to your repository
- Needs to have at least one job
- Can be triggered by different events
Jobs
- A section of the workflow that will be associated with a runner
- Can be GH-hosted or self-hosted
- Can run on a machine or container
Steps
- Is an individual task that can run commands in a job
Actions
- Are the standalone commands that are executed
- Can reference GH actions
- User your own actions
- Run commands
run: npm install -g bats
to be executed on the runner
Configure a GitHub Actions workflow
Configure workflows to run for scheduled events
- The
schedule
event allows you to trigger a workflow to run at specific UTC times using POSIX cron syntax
Configure workflows to run for manual events
- Manually trigger a workflow by using the
workflow_dispatch
event - Can choose which branch to run on or set optional
inputs
that GH will present as form elements - This event allows you to run the workflow using the GitHub REST API or by selecting the Run workflow button
- Can use the GitHub API to trigger a webhook event called
repository_dispatch
. - Trigger a workflow for an activity that occurs outside of GitHub
- This manual event requires you to do two things:
- send a POST request to the GitHub endpoint
/repos/{owner}/{repo}/dispatches
with the webhook event names in the request body - configure your workflow to use the repository_dispatch event
- send a POST request to the GitHub endpoint
Configure workflows to run for webhook events
- Run workflow when specific webhook event occur on GitHub
- Webhook events
Use conditional keywords
- Can use conditional
if
keyword - Evaluation expression needs specific syntax {% highlight yml %} ${{
}} {% endhighlight %}
- Note: some expressions GitHub automatically evaluates that’s why
is missing.
Disable and delete workflows
- It’s possible to disable a workflow temporarily
- It’s possible to cancel a workflow
- You can delete the workflow if wanted
Use an organization’s templated workflow
- Can define a workflow template in the organizations
.github
repository
Use a specific version of an action
- It is recommended to refer a specific version of action