Practice sharing! GitLab CI/CD Quick Start

Students who have used GitLab must also be familiar with GitLab CI/CD. GitLab CI/CD is a built-in tool in GitLab, which can help us run a series of scripts to build, test and verify the code each time the code is pushed. Change and deploy.

Rainbond itself integrates the entire CI/CD process by default. Users only need to provide source code, and subsequent construction and operation are completely handled by Rainbond. The entire process is defined by Rainbond without user intervention. This has both advantages and disadvantages. The advantage is that the user's operation is simplified and there is no need to learn CI/CD related knowledge; the disadvantage is that the user cannot customize in the CI/CD process, such as integrating code detection or running a script, which is part of Rainbond's source code construction process. is not customizable.

This article tells you how to use GitLab CI/CD to build, test, deploy Spring Boot applications, and run the products on Rainbond.

Introduction to GitLab CI

Using GitLab CI requires creating .gitlab-ci.ymlfiles . In this file, you can define the compile, test, and deploy scripts that need to be run.

After adding the .gitlab-ci.ymlfile , when pushing code, GitLab Runner automatically executes the Pipeline you define and displays the CI process and results on the GitLab CI page.

The basic flow of GitLab CI is as follows:

  1. Developer pushes code
  2. Trigger GitLab CI to start
  3. runner executes predefined scripts

GitLab CI/CD Quick Start

Deploy GitLab and Runner

One-click deployment of GitLab and Runner through the open source application store, add -> create components based on the application store -> search in the open source application store to install GitLab and Runner into the specified application in GitLabturn .

Configure Runner on Rainbond

Before Rainbond v5.8, Rainbond did not support Runner type components very well. Because if the Runner runs in the form of a container, it needs to mount the docker.sock file of the host, so that it can schedule the docker environment of the host and create a container to perform tasks. In Rainbond v5.8, it supports to modify the YAML of the component, you can customize the Volumes and mount the local docker.sock.

After installing the Runner through the application store, you can see the Kubernetes properties in the Runner component -> other settings, and Rainbond's application model has been compatible with the Kubernetes properties.

Register Runner with GitLab:

  1. Enter orchestration mode, connect the runner to GitLab and update the runner component. (If it prompts that GitLab has not opened the internal port, select port 80)

  2. First visit GitLab, Menu -> Admin -> Overview -> Runners -> Register an instance runner -> Copy Registration token.

  3. Enter the runner component, click the web terminal in the upper right corner to enter, execute the following command to register, and <token>replace it with the Registration token copied in the previous step.

gitlab-runner register \
  --non-interactive \
  --executor "docker" \
  --docker-image alpine:latest \
  --url "http://127.0.0.1" \
  --registration-token "NxNuoRXuzYy3GnFbkhtK" \
  --description "docker-runner" \
  --tag-list "newdocker" \
  --run-untagged="true" \
  --locked="false" \
  --docker-volumes /var/run/docker.sock:/var/run/docker.sock \
  --docker-privileged="true" \
  --access-level="not_protected"

Parameter Description

Parameter Value Describe
--executor docker The executor type is docker.
--url http://127.0.0.1 GitLab addr
--registration-token <token> GitLab token
--tag-list newdocker Define the label/name of the runner
--locked false runner is enabled
--run-untagged true Run a Job without the specified label
--docker-volumes file_path Mount the file to the runner
--docker-privileged true runner running mode: privileged mode
  1. After the registration is complete, you can see the online runner on the GitLab page

GitLab CI/CD To Rainbond

The whole process can be divided into:

  1. Developers submit code to the GitLab repository.
  2. Triggers the creation of a GitLab pipeline, and the Runner executes the .gitlab-ci.ymldefined stages.
  3. Push the created image to the existing image repository for use in the subsequent Deploy process.
  4. Through the method of custom API of Rainbond, trigger the automatic construction of platform components and enter the Deploy stage.

Practical steps

premise:

  • Existing Rainbond environment
  • Prepare the image repository, DockerHub used in this article
  • The code project used in this article is Java-Maven-Demo

1. There are components that have been deployed based on images on Rainbond

2. Import the sample code into GitLab.

3. Write the .gitlab-ci.yml file:

Create the following .gitlab-ci.ymlcontent :

# 定义 job 的执行顺序
stages:
  - test
  - package
  - push
# 定义基础镜像
image: maven:3.6.3-jdk-8
job-test:
  stage: test
  tags: 
    - newdocker
  script:
    - echo "===============开始执行代码测试任务==============="
    - mvn test
job-package:
  stage: package
  tags: 
    - newdocker
  script:
    - echo "===============开始执行打包任务==============="
    - ls
    - mvn clean package
    - cp Dockerfile target/Dockerfile
  cache:
    key: devops
    paths:
      - target/ 
job-push:
  stage: push
  image: docker:dind
  cache:
    key: devops
    paths:
      - target/
  tags:
    - newdocker
  script:
    - docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD}
    - docker build -t ${IMAGE_DOMAIN}/java-maven:latest .
    - docker push ${IMAGE_DOMAIN}/java-maven:latest
  after_script:  
    - curl -d '{"secret_key":"${RAINBOND_SECRET}"}' -H "Content-type:application/json" -X POST http://${RAINBOND_IP}:7070/console/custom/deploy/3321861bcadf0789af71898f23e8e740

after_scriptIt is executed after the push image is completed. The components are built through the Rainbond API, and Rainbond will get the latest image to build and run. <RAINBOND_SECRET> can be seen in Components -> Build Sources -> Automatic Builds. For details, please refer to the documentation Configure component to automatically build and deploy

4. Submit the code to test the automatic build ,

Modify the code and submit it. After submitting, you can see the details of the ongoing and completed tasks in the project's CI/CD -> Jobs.

5. View the Rainbond component build

Autobuild information can be seen in the component's action log.

write at the end

GitLab CI is very extensible and can integrate many third-party tools. Combined with Rainbond as a CD, you can run the product on Rainbond to form a Pipeline suitable for your own code projects.

Rainbond will implement Pipeline in the future v5.9.x version. Students who have ideas for Rainbond to implement Pipeline can submit proposals on the issue https://github.com/goodrain/rainbond/issues

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/rainbond/blog/5572193