Getting started with GitHub actions


Getting started with GitHub actions


GitHub actions is a feature of GitHub, which can build automatic workflow for our project. For example, code checking, automatic packaging, testing, release version and so on. The entry is next to the project pull requests.

Getting started with GitHub actions

This article is mainly through one of the pits, so that you can run smoothly when you use it for the first time. It mainly uses GitHub, docker, springboot, nginx and other technologies. If you don’t know much about these technologies, you can check the information first.

Practice steps

Create springboot project

Use idea to create a spring boot project. Spring boot is built with gradle from version 2.3.0.m1. So we also choose gradle as the build tool here.

We made a fairly significant change to Spring Boot in 2.3.0.M1. It was the first release of the project to be built with Gradle rather than Maven.

Please refer toMigrating Spring Boot’s Build to Gradle

In the springboot project, we write a simple controller for testing. The code is very simple, and an example is given here.

public class DemoController {
    public String sayHello() {
        return "Hello GitHub Actions!";

Write dockerfile file

Create dockerfile file in springboot project for image packaging.

#From specifies the underlying image

For those who don’t know about dockerfile, please go toUsing dockerfile to customize imageLearn the basics.

Creating docker registry

In this step, partners can freely choose Alibaba cloud or Tencent cloud and other cloud service providers to open corresponding services, and then our packaged images will be uploaded to our own private docker registry. This step is very important, we must open well, and then proceed to the following steps.

After opening a good service, you can read the service provider’s documents a little and have a general understanding of the service.

The cloud service provider used in this article is Alibaba cloud. You can clickhereOpen and configure.

Upload code

Upload our springboot project to GitHub repository. I believe you guys have muscle memory about how to create GitHub repository!

git init
git add .
git commit -m "first commit"
git remote add origin {{ your origin url }}
git push -u origin master

Enter the spring boot project directory and enter the above commands to easily upload the code to your GitHub repository.

Configure GitHub actions

We click the actions tab and select the java with gradle template as the basic template for configuration.

Getting started with GitHub actions

#The name of GitHub actions

In the configuration file${{ secrets.XXX }}Variable is the function provided by GitHub to protect our key. You can define variables in Secrets under settings in the warehouse, and then follow the${{ secrets.XXX }}You can get the value of the variable.

So far, we have finished the GitHub actions configuration file. Friends need to pay attention to the format of yaml file, do not have format errors.

Trigger GitHub actions

After completing the preparation of the configuration file, we click Start commit to submit it. After the submission is successful, the actions function will be automatically triggered to build.

Getting started with GitHub actions

Click the actions tab to find the triggered actions and wait for a moment to see the construction result. If there is an error in the middle of the build, you can also see the reason directly here and go to the configuration file for troubleshooting.

Getting started with GitHub actions

After the completion of the construction, we can go to the background of the corresponding cloud service provider to check the docker image we uploaded and confirm whether the construction is successful again!

Deploy to server

Let’s make sure that nginx and docker are installed on our servers. If it has not been installed, you can refer to the relevant information for installation. After successful installation, proceed to the following steps.

Here we need to prepare a simple server deployment bash script to close and delete the old container, pull the new image and build a new container. Here is a simple example for reference.

#Pull new image

We need to add some content to the GitHub actions configuration file written above. The main function is to trigger the remote server to pull the image to rebuild after pushing the docker image, and start the container with the new image. The complete GitHub actions configuration file is shown below.

#The name of GitHub actions

As for nginx, I believe you guys should be very familiar with it. I just want to repeat it here. Paste a simple configuration file directly.

server {
    listen 80;
    server_name {{ your domain }};

    location / {
           proxy_pass http://localhost:8080/;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
           proxy_set_header X-Forwarded-Port $server_port;

Test GitHub actions

After completing the above steps, we modify the code in the springboot project and submit it to the GitHub repository. Visit the project later to check the effect!

public class DemoController {
    public String sayHello() {
        return "Hello GitHub Actions!Hello World!";

Getting started with GitHub actions

Here we have successfully completed a basic GitHub actions practice. After the partners modify the code, push or PR to the master branch, GitHub actions will automatically complete the construction steps we set. Very convenient and fast!


This basic GitHub actions spring boot practice is for your reference only. If you encounter any pitfalls, please discuss with me in the comments area. The article is not well written, please understand.

This work adoptsCC agreementReprint must indicate the author and the link of this article


Recommended Today

What is “hybrid cloud”?

In this paper, we define the concept of “hybrid cloud”, explain four different cloud deployment models of hybrid cloud, and deeply analyze the industrial trend of hybrid cloud through a series of data and charts. 01 introduction Hybrid cloud is a computing environment that integrates multiple platforms and data centers. Generally speaking, hybrid cloud is […]