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
To use GitLab CI, you need to create the .gitlab-ci.yml
file in the repository root directory. In this file, you can define the compile, test, and deploy scripts that need to be run.
After adding the .gitlab-ci.yml
file, when the code is pushed, 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:
- Developer pushes code
- Trigger GitLab CI to start
- 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 GitLab
Install GitLab and Runner into the specified application in turn.
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:
- 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)
- First visit GitLab, Menu -> Admin -> Overview -> Runners -> Register an instance runner -> Copy Registration token.
- Enter the runner component, click the web terminal in the upper right corner to enter, execute the following command to register,
<token>
replace 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 |
- 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:
- Developers submit code to the GitLab repository.
- Trigger the creation of the GitLab pipeline, and the Runner executes the stages defined by
.gitlab-ci.yml
. - Push the created image to the existing image repository for use in the subsequent Deploy process.
- 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 in the project root directory .gitlab-ci.yml
with the following content:
# 定义 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_script
is executed after the push image is completed, build the component 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 to configure the 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
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。