编程知识 cdmana.com

Getting started with GitHub actions

image

Preface

GitHub Actions yes GitHub Features launched , We can build automated workflows for our projects . For example, code checking , Automated packaging , test , Release and so on . The entrance is in the project Pull Requests side .

image

This article is mainly to go through one of the small pits , Let everyone in the first time use can run smoothly . It mainly uses GitHub,Docker,SpringBoot,Nginx Technology . Partners who don't know much about these technologies , You can check the information by yourself first .

Practical steps

establish SpringBoot engineering

Use IDEA Create a SpringBoot engineering ,SpringBoot from 2.3.0.M1 The version starts with Gradle structure . So we also choose Gradle As a build tool .

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 to Migrating Spring Boot’s Build to Gradle

stay SpringBoot In Engineering , We write a simple controller to test , The code is very simple , Here's an example .

@RestController
public class DemoController {
    @GetMapping("/")
    public String sayHello() {
        return "Hello GitHub Actions!";
    }
}

To write Dockerfile file

stay SpringBoot Create in project Dockerfile file , For image packaging .

# FROM  Specify base image 
FROM openjdk:8-jdk-alpine
# ARG  Build parameters 
ARG JAR_FILE=build/libs/*.jar
# COPY  Copy file 
COPY ${JAR_FILE} app.jar
#  Exposed port 
EXPOSE 8080
# CMD  Container start command 
CMD java -jar /app.jar

I don't know yet Dockerfile You can go to Use Dockerfile Custom image Take a look at the basics .

establish Docker Registry

In this step, partners can freely choose cloud service providers such as Alibaba cloud or Tencent cloud to open corresponding services , After that, our packaged image will be uploaded to our own private Docker Registry in . This step is very important. We must open it and then proceed to the following steps .

After opening the service , Read the service provider's documentation a little bit , Have a general understanding of the service .

The cloud service provider used in this article is Alibaba cloud , You can click on here Open and configure .

Upload code

Take us SpringBoot Project upload to GitHub Repository in , About how to create GitHub Repository, I believe you all have muscle memory !

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

Get into SpringBoot Project directory , Enter the commands above , You can easily upload the code to your GitHub Repository.

To configure GitHub Actions

We click Actions tab , choice Java with Gradle The template is configured as the basic template .

image

# GitHub Actions  The name of 
name: GitHub Actions SpringBoot Demo

#  When  master  Branching  push  perhaps  pull request  Event  action
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

#  Appoint  jobs
jobs:
  # build job
  build:

    #  Specify to run in  ubuntu  On 
    runs-on: ubuntu-latest
    steps:

    #  Pull the warehouse code to  ubuntu  On the mirror 
    - uses: actions/checkout@v2

    #  Appoint  java  edition 
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1
      with:
          java-version: 1.8

    #  to  gradle  Give permission to run 
    - name: Grant execute permission for gradlew
      run: chmod +x gradlew

    #  adopt  gradle  Package our project 
    - name: Build with gradle
      run: ./gradlew build

    #  adopt  Dockerfile  Build a mirror image 
    - name: Build docker image
      run: docker build . -t {{ image_name:image_version }}
    
    #  Log in to our own  docker registry
    - name: Login to aliyun docker image registry
      run: echo "${{ secrets.REGISTRY_PASSWORD }}" |  docker login --username=xxxxxx registry.cn-shenzhen.aliyuncs.com --password-stdin

    #  Upload  docker  Mirror image 
    - name: Push image to aliyun docker image registry
      run: docker push {{ image_name:image_version }}

In the configuration file ${{ secrets.XXX }} Variable , yes GitHub To protect the functionality provided by our key . It can be in the warehouse Settings Under the Secrets Define variables in , And then according to ${{ secrets.XXX }} The format of , You can get the value of the variable .

Come here , We're done GitHub Actions Configuration file writing for . You guys need to pay attention to yaml File format , Don't make format mistakes .

Trigger GitHub Actions

After writing the configuration file , We click start commit Submit , After successful submission, it will be triggered automatically Actions Function to build .

image

Click on Actions tab , Find the triggered Actions, Wait a moment to see the build result . If something goes wrong in the middle of the build , You can also see the reason directly here and go to the configuration file to check .

image

After building , We can go to the background of the corresponding cloud service provider to check our uploaded Docker Mirror image , Reconfirm that the build was successful !

Deploy to server

Let's make sure our servers are installed Nginx,Docker. If not already installed , You can refer to the relevant information for installation . After successful installation , Then proceed to the following steps .

Here we are going to prepare a simple server deployment bash Script , Finish closing and deleting the old container , Pull the new image and build a new container operation . Here is a simple example .

#  Pull new image 
docker pull {{ your image_name:image_version }}
#  Stop the old container 
docker stop {{ your container name }}
#  Delete stopped containers 
docker container prune -f

#  Build a new container 
docker run \
-p 8080:8080 \
-v /data:/data \
--name {{ your container name }} \
-d {{ your image_name:image_version }}

We need to write the above GitHub Actions Add some content to the configuration file , The main function is to push the end of Docker After mirroring , Trigger remote server pull image rebuild , And start the container with the new image . complete GitHub Actions The configuration file is shown below .

# GitHub Actions  The name of 
name: GitHub Actions SpringBoot Demo

#  When  master  Branching  push  perhaps  pull request  Event  action
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

#  Appoint  jobs
jobs:
  # build job
  build:

    #  Specify to run in  ubuntu  On 
    runs-on: ubuntu-latest
    steps:

    #  Pull the warehouse code to  ubuntu  On the mirror 
    - uses: actions/checkout@v2

    #  Appoint  java  edition 
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1
      with:
          java-version: 1.8

    #  to  gradle  Give permission to run 
    - name: Grant execute permission for gradlew
      run: chmod +x gradlew

    #  adopt  gradle  Package our project 
    - name: Build with gradle
      run: ./gradlew build

    #  adopt  Dockerfile  Build a mirror image 
    - name: Build docker image
      run: docker build . -t {{ image_name:image_version }}
    
    #  Log in to our own  docker registry
    - name: Login to aliyun docker image registry
      run: echo "${{ secrets.REGISTRY_PASSWORD }}" |  docker login --username=xxxxxx registry.cn-shenzhen.aliyuncs.com --password-stdin

    #  Upload  docker  Mirror image 
    - name: Push image to aliyun docker image registry
      run: docker push {{ image_name:image_version }}

    #  Connect to the remote server 
    - name: Connect to server
      uses: webfactory/ssh-agent@v0.4.1
      with:
        ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }}
    
    #  initialization  knownhosts
    - name: Setup knownhosts
      run: ssh-keyscan {{ your host }} >> ~/.ssh/known_hosts

    #  Trigger server deployment script 
    - name: Trigger server deployment script
      run: ssh root@{{ your host }} "sh {{ your deployment bash script }}"

About Nginx aspect , I believe you are very familiar with , I'm just going to go over 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

I've completed the above steps , We went to the SpringBoot Modify the code in the project and submit it to GitHub Repository. Wait a moment to visit the project , Check the effect !

@RestController
public class DemoController {
    @GetMapping("/")
    public String sayHello() {
        return "Hello GitHub Actions!Hello World!";
    }
}

image.png

Here we have successfully completed a basic GitHub Actions practice , After that, my friends will modify the code ,Push perhaps PR To master Branch ,GitHub Actions It will automatically complete the build steps we set up . Very convenient and quick !

summary

This basic GitHub Actions SpringBoot Practice is for your reference only , If there's a hole , Also welcome to discuss with me in the comments section . The article is not well written , Please forgive me .

版权声明
本文为[Zeng is ran]所创,转载请带上原文链接,感谢

Scroll to Top