I am very curious when it comes to Continuous Integration Tools, such as Concourse CI, Drone CI, Gitlab CI, etc and I have posted quite a bit on the software mentioned.
In this post we will have a look at Github Actions which is described from their page as:
"GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want."
What I really love about Github Actions is their continuous integrations workflow templates, which makes it really easy to get you going with almost any services that you would like to use.
When you head over to actions on your repository, you will see a couple of workflows that you could use, which will look like this:
And the templates are available to view on actions/starter-workflows but you can also create your own and there's the actions marketplace where anyone can build and submit their workflows for anyone to use.
You can use Github's shared runners or you can host your own runner.
Hello World Action
In this post we will demonstrate a basic usage of Github Actions to get a feel of what it can do.
First I created a new github repository, ruanbekker/hello-world-actions, which is nice and empty:
If you hit the "Actions" tab at the top, you will see more or less the following:
Let's go ahead and use the template Github provided us and select "Set up this workflow" and you will be presented with something more or less like this:
If we take the workflow:
name: CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run a one-line script
run: echo Hello, world!
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
We are naming our workflow CI
and we are defining that it will be triggered on push
and pull_request
actions but only on the main
branch.
We have one job named, build
and we are using a github runner with the first step, actions/checkout@v2
will clone the github repository and store it under $GITHUB_WORKSPACE
.
Then we have a one liner
script followed by a multi line
script which will run sequentially.
Commit that file, then you should see your repository should look more or less like this:
When you hit the "Actions" tab, you will see your workflows:
Because we committed this file to the main branch our workflow triggered and if we select our workflow and our build job, we can get output of our workflow:
Secrets
Github Actions allow you to store secrets, which you can reference in your workflow as environment variables.
Head over to the settings of your repository and select "Secrets":
As you can see we have no secrets stored at this moment. Let's create a secret with the name of SECRET_STRING
and the value of 12345
. This secret will be encrypted when you create it:
Which will result in:
This secret can be exposed as an environment variable in our workflow using:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: secret string
env:
SECRET_STRING : ${{ secrets.SECRET_STRING }}
Let's run a validate script using an if statement in our workflow. Edit the .github/workflows/blank.yml
and replace it with the following:
name: CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: validate if our value is the same as our secret
env:
SECRET_STRING : ${{ secrets.SECRET_STRING }}
run: |
if [ ${SECRET_STRING} == "12345" ]
then
echo "the secret (${SECRET_STRING}) has been validated"
else
echo "the string that you provided is not correct"
fi
Although this is not the best example, it's just a way to demonstrate what the values are in our container's environment.
Once we commit the changes to our main branch, we can have a look at our actions output and you will see the value has been validated:
This was a basic introduction to Github Actions and I will post in the future on different ways you can use Github Actions under the /github-actions tag
Thank You
Thanks for reading, if you like my content, please feel free to share or reach out to me on Twitter to say hi on @ruanbekker or visit me on my website: ruan.dev
Comments