Setting up GitHub Actions for Continuous Integration
automating all of the things
- tags
- github
- actions
- automation
Contents
I wanted to see how to automate the creation of docker images using github actions. This is how I did it.
- Create a repo
- Create a docker hub api key
- Create your application
- Create dockerfile
- Add github workflow
- Push and test!
Let's go!
Create a sample repo
|
|
The roll over to GitHub and create a new repository. I'm calling mine wschenk/actionstest.
Now we can add the remote, in my case
|
|
Creating secrets
First log into docker hub, and create a new access token. Copy this, and then go to the settings of your GitHub project. Then create the following secrets.
DOCKERHUB_USERNAME | Your login |
DOCKERHUB_TOKEN | The token you just created |
Inside of the workflow we can access these like ${{ secrets.DOCKERHUB_USERNAME }}
.
Our "application"
This is pretty simple. We print the date and then run the fortune
command.
app.sh
:
|
|
And we'll test it, why not.
success_test.sh
:
|
|
fail_test.sh
:
|
|
Dockerfile
This is a pretty simple Dockerfile
that just copies stuff over and
runs the app.
|
|
Running locally
|
|
Run the test:
|
|
We can see that it "runs the tests" but returns a non-zero, i.e. failed status.
Build test and push action
This action has a number of steps:
- It runs on all
push
actions, regardless of branch. - First it
actions/checkout@v2
the repo. - Then it logs into
dockerhub
using the secrets - It uses
docker/metadata-action
to find the tags and labels for which branch was committed. - Runs
docker/build-push-action
withpush
false to create the image. - It then runs the test script from the resulting image. If this fails, the action fails.
- Assuming the tests pass, it "rebuilds" the image and pushes the tags to dockerhub
Create .github/workflows/build_test_and_push.yml:
|
|
Test it out
|
|
Since we have the fail_test.sh
script being run as part of the action,
it should build the image and then abort on the Run Test
part of the
run. If you go to your repository, click on Actions
, you should see
the failed run.
Lets create a staging branch to fix it:
|
|
Then change like 28
inside of build_test_and_push.sh
to run
/app/success_test.sh
instead. Commit, and then push to the staging
branch:
|
|
This build should succeed, and if you go to hub.docker.com under your user, you should see the image there with the staging tag.
Previously
Next