Recently, I’ve been using GitHub Actions for my company’s internal project release process, and it’s been a great experience.
The main goal of this article is to help newcomers to GitHub Actions to quickly build automated tests and package and push Docker images.
Create project
This article focuses on Go as an example, but of course other languages are similar and have little to do with the language itself.
Here we start by creating a project on GitHub and writing a few simple pieces of code main.go.
The content is very simple, it just prints the version number; it is accompanied by a unit test main_test.go.
|
|
We can execute go test to run the unit test.
Automated Testing
Of course, the above process can be fully automated using Actions.
First, we need to create a .github/workflows/.yml
* configuration file in the root path of the project, adding the following.
To briefly explain.
-
name
needless to say, is to create a noun for the current workflow. -
on
refers to the event under which it is triggered, in this case when the code is pushed, more event definitions can be found in the official documentation. -
jobs
defines the tasks, and here there is only one task named test.
This task is run in the ubuntu-latest environment and is only run when the main branch is pushed or when a tag is pushed.
It will run using actions/checkout@v2
, an action wrapped by someone else, but of course using the official pull code action provided here.
- Based on this logic, we have the flexibility to share and use other people’s actions to simplify the process, and this is where GitHub Action is very scalable.
The final run is to run your own command, which naturally triggers the unit test.
- If it is Java, you can change it to mvn test.
It is very convenient to automatically run unit tests whenever we push code on the main branch or when code from other branches is merged in.
The results are consistent with our local runs.
Auto Publish
Next, consider automating the packaging of Docker images and uploading them to Docker Hub; to do this, first create the Dockerfile.
Here the use of ldflags can be passed during the compilation of some parameters into the packaging program, such as packaging time, go version, git version, etc..
The VERSION is simply passed into the main.version variable so that it can be retrieved at runtime.
Then continue writing docker.yml to add automatic packaging of Docker and pushing to the docker hub.
|
|
A deploy job has been added.
The condition to run is that the single test process of the previous step is running and a new tag is generated before the subsequent steps are triggered.
|
|
In this step we need to log in to DockerHub, so first we need to configure the user_name and access_token of the hub in the GitHub project.
Once configured, the variable can be used in the action.
Here we use the official login action (docker/login-action) provided by docker.
One thing to note is that we need to change the image name to lowercase, otherwise it will fail to upload, for example, if my name has an uppercase J letter, it will report an error when uploading directly.
So perform this step to convert to lowercase before uploading.
Finally, use these two variables to upload to Docker Hub.
In the future, Action will automatically perform the single test, build, and upload process as soon as we tag it.
Summary
GitHub Actions is very flexible, and most of the features you need are readily available in the marketplace.
For example, you can use ssh to log in to your own server and execute commands or scripts, so there’s a lot of room for imagination.
It’s like building blocks, and you can use it flexibly to accomplish your needs.
Reference https://crossoverjie.top/2021/03/26/go/github-actions/