Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the CI/CD function of Gitlab

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use the CI/CD function of Gitlab. It is very detailed and has a certain reference value. Friends who are interested must finish it!

SpringBoot practical e-commerce project mall (50k+star) address: https://github.com/macrozheng/mall

Installation automates deployment through the CI/CD feature of Gitlab. We need to install services such as Gitlab, Gitlab Runner and Maven. Install Gitlab first let's install Gitlab, friends who don't know the installation and use of Gitlab can refer to "build your own Git warehouse in 10 minutes".

Use the following command to run the Gitlab service. Note that the hostname attribute is added here so that we can access Gitlab through the domain name (to avoid some unnecessary trouble). The environment variable GITLAB_ROOT_PASSWORD can directly set the password of the root account in Gitlab.

Docker run-- detach\-- hostname git.macrozheng.com\-- publish 10443 publish 1080 publish 80-- publish 1022 hostname git.macrozheng.com 22\-- name gitlab\-- restart always\-- volume / mydata/gitlab/config:/etc/gitlab\-- volume / mydata/gitlab/logs:/var/log/gitlab\-- volume / mydata/gitlab/data:/var/opt/gitlab\-e GITLAB_ROOT_PASSWORD=12345678\ gitlab/gitlab-ce:latest

We need to access Gitlab through the domain name git.macrozheng.com. If you do not have a domain name, you can do so by modifying the local host file.

192.168.7.134 git.macrozheng.com

Since our Gitlab runs on port 1080, we want to access it without adding ports, we can use Nginx to reverse proxy, friends who are not familiar with Nginx can take a look at these wonderful uses of Nginx, you must not know! "add the git.conf configuration file to the configuration folder of Nginx, as follows:

Server {listen 80; # also supports HTTP server_name git.macrozheng.com; # modifying domain name location / {proxy_pass http://192.168.7.134:1080; # setting proxy service access address index index.html index.htm;} error_page 500502 503 504 / 50x.hml; location = / 50x.html {root / usr/share/nginx/html }}

After that, we can access Gitlab through the domain name git.macrozheng.com. Enter the account password root:12345678 and log in.

Upload our SpringBoot application code to Gitlab so that Gitlab is ready! It should be noted here that if you do not specify hostname when you start Gitlab, the HTTP access address of your project will be the ID of the container, and you will not be able to access the Git repository with this address!

Installing Gitlab RunnerGitlab is just a code repository, and to implement CI/CD, you also need to install an executor equivalent to a task in Gitlab, which Gitlab calls when it needs to be executed.

First download the Docker image of gitlab-runner, choose alpine-bleeding, this version is very small!

Docker pull gitlab/gitlab-runner:alpine-bleeding

Run gitlab-runner using the following command

Docker run-- name gitlab-runner-- restart always\-v / var/run/docker.sock:/var/run/docker.sock\-v / mydata/gitlab-runner:/etc/gitlab-runner\-d gitlab/gitlab-runner:alpine-bleeding

At this point, if we look at the container log of gitlab-runner, we will find the following error. The config.toml file cannot be found. There is no need to worry about this problem. When we register gitlab-runner with Gitlab, the file will be generated automatically.

ERROR: Failed to load config stat / etc/gitlab-runner/config.toml: no such file or directory builds=0

Next, we need to register gitlab-runner to Gitlab, open the Project- > Settings- > CI/CD function, and obtain the address and token to be used for runner registration.

Next, enter the interior of the gitlab-runner container using the following command

Docker exec-it gitlab-runner / bin/bash

Register runner within the container using the following command

Gitlab-runner register

Registration will appear when the interactive interface, prompting you to enter the registration address, token, actuator type and other information, ssh executor can remotely execute Linux commands, very easy to use, it is recommended to use this!

After the registration is completed, we can find that the config.toml file has been generated, as follows. When you want to modify the runner configuration in the future, you can just change this file.

Concurrent = 1check_interval = 0 [session_server] session_timeout = 1800 [[runners]] name = "docker-runner" url = "http://192.168.7.134:1080/" token =" c2kpV6tX6woL8TMxzBUN "executor =" ssh "[runners.custom_build_dir] [runners.cache] [runners.cache.s3] [runners.cache.gcs] [runners.cache.azure] [runners.ssh] user =" root " Password = "123456" host = "192.168.7.134" port = "22"

In the CI/CD setting of Gitlab, we can find that a runner has been successfully registered!

Installing the MavenSpringBoot project package depends on Maven, and we need to install it on the server first.

Download the Linux installation package for Maven at https://maven.apache.org/down...

After the download is complete, extract it to the specified directory using the following command

Cd / mydatatar-zxvf apache-maven-3.8.1-bin.tar.gz

Modify the / etc/profile file to add the environment variable configuration:

Export MAVEN_HOME=/mydata/apache-maven-3.8.1export PATH=$PATH:$MAVEN_HOME/bin

Test whether the installation is successful by looking at the Maven version.

Mvn-vMaven home: / mydata/apache-maven-3.8.1Java version: 1.8.0U292, vendor: AdoptOpenJDK, runtime: / mydata/java/jdk1.8/jreDefault locale: en_US, platform encoding: UTF-8OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix" install JRE by default on JDKCentOS, JDK is required to use Maven.

Download JDK 8 at https://mirrors.tuna.tsinghua...

Extract the JDK to the specified directory after the download is complete

Cd / mydata/javatar-zxvf OpenJDK8U-jdk_x64_linux_xxx.tar.gzmv OpenJDK8U-jdk_x64_linux_xxx.tar.gz jdk1.8

Add the environment variable JAVA_HOME to the / etc/profile file.

Vi / etc/profile# add export JAVA_HOME=/mydata/java/jdk1.8export PATH=$PATH:$JAVA_HOME/bin# to the profile file to make the modified profile file effective. / etc/profile is ready to use, and then you can automate the deployment of SpringBoot applications through the CI/CD function of Gitlab!

First, add the. gitlab-ci.yml file to the root of the project, defining two tasks, one of which will package the application code into a Jar package and copy it to the specified directory, and the other will package the Docker image of the application and run it by running the script run.sh

# Packaging task build-job: stage: build # specify the label Only runner with this tag will execute tags:-docker script: # package with Maven-mvn clean package # to jar package, Dockerfile, Run the script to copy to the specified directory-cp target/mall-tiny-gitlab-1.0-SNAPSHOT.jar / mydata/build/mall-tiny-gitlab-1.0-SNAPSHOT.jar-cp Dockerfile / mydata/build/Dockerfile-cp run.sh / mydata/build/run.sh# deployment task deploy-job: stage: deploy tags:-docker script: # enter the specified directory and execute the run script-cd / mydata/ Build-chmod + x run.sh -. / run.sh

It is worth mentioning that by default, runner only executes Job with the same tag, which can be done here because we have set the tag to docker for both Job and runner. If you do not set a tag, you need to set up the editing interface of runner so that runner can execute Job without tags.

Since our gitlab-runner uses an ssh executor, it will log in to our designated server, execute the script command we defined in .gitlab-ci.yml, and get the code from the Git repository before that, so we also need to modify the host file on the server

Vim / etc/hosts192.168.7.134 git.macrozheng.com

The next step is to submit the script to the Git repository. After submission, you will find the executing task in Project- > CI/CD- > Pipelines.

When you open the details page of Pipeline, you can find that both of the tasks we defined have been performed successfully.

By opening the details interface of Job, we can see the log information output during the execution of the task.

If you want to execute Pipeline manually instead of submitting the trigger, you can click the Run Pipeline button on the Pipelines page.

After running successfully, you can access the project at the following address: http://192.168.7.134:8088/swa...

The above is all the contents of this article entitled "how to use the CI/CD function of Gitlab". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report