docs/2-Github/2.2-Actions.md | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
In this section we will be working with GitHub Actions in order to introduce some basic automation to our repository as well as create some of that automation ourselves with our own custom action as well as reusable workflows
The first thing we are going to do is create a new repository to work with and inside that repo add a folder .github/workflows/
to it. Inside this folder is where we will be storing the workflows we create. If you were to go into the Actions
tab, you also have the option of adding common workflows directly to your repo which will also create this directory structure for you.
Inside our newly created directory, we are going to create another file called getting-started.yml
and put this basic workflow inside of it:
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the workflow will run
on:
# Triggers the workflow on push events but only for the "main" branch
push:
branches: [ "main" ]
# 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 "simple"
simple:
# 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:
- name: Run a one-line script
run: echo password
Before this next part, a look at the GitHub Docs for GitHub Actions to learn about exactly what each of these steps do.
To complete this exercise we are going to make some changes to this file as well as work with the settings of the repo. There are a few things specifically to do:
- Add a trigger to manually run the workflow
- Take our
password
here and put it into a secret and replace it in the workflow with the correct reference - Create an additional job that will create a simple string output to use in subsequent jobs
- Create another additional job that uses a matrix strategy and will print
"<output from previous job>-<numbers 1-10, one at a time>"
Continuing with our previous workflow, we are going to make a few modifications to it. Instead of using GitHub hosted runners, we are going to use self-hosted runners instead which you will setup on your own machine. Now instead of using the outputs
function to move data from one job to the other, we are going to use the upload-artifact
and download-artifact
actions. Now our previous string output will become text in a file we pass between our jobs, so how will we get it out of the file and into a form we can use? GITHUB_ENV is one way to do it.
In this exercise we will be creating our own composite action to use inside of our workflow which will be used to create the string output we use in our matrix job. Following that we will then bundle all of this up into our own reusable workflow
Check out these docs on creating composite actions in order to be familiar with the process.
-
Now create your own composite action that will simply generate a string using the input we pass into it from our main workflow or a default string should you not pass in anything. It can be any string you'd like, but its probably best if it's something indicating that its input to the action or just the default. This action can exist in the same repo as our workflow or be inside its own separate repo, you will just have to reference it accordingly.
-
Next we will be bundling this all together into a reusable workflow. Check out these docs on creating reusable workflows in order to be familiar with the process.
Just like the composite actions, we can store this in either the same repo we have been working inside of or its own repo.
- Once you have the reusable workflow setup, create another workflow inside our repo that will be used to call our reusable one so we can see it in action. Keep in mind you wont be able to call it from a step, it must be done at the job level.
- Learn about some of the basic features available within a GitHub Workflow including how to pass information between jobs, use matrixes, etc
- Be able to create your own basic composite actions
- Be able to take a regular workflow and make it a reusable one and call it from another workflow.