Moment For Technology

Still using Jenkins? Check out Gitlab's CI/CD features for fun!

Posted on Dec. 2, 2022, 5:29 p.m. by Brooke Wright
Category: The back-end Tag: The back-end java git

SpringBoot Actual E-commerce project Mall (50K + STAR)

Abstract

Previously, I wrote an article titled "Goodbye Jenkins! A few lines to automate deployment, this artifact is a bit of a hit!" , about the use of Gogs+Drone to achieve automated deployment. I recently discovered that Gitlab's CI/CD functionality can also be deployed automatically and is quite simple to use! If you're using Gitlab as a Git repository, try out its CI/CD functionality. This article still takes SpringBoot's automatic deployment as an example, practices Gitlab's CI/DI function, hopes to be helpful to everyone!

The installation

To automate the deployment through Gitlab's CI/CD function, we need to install Gitlab, Gitlab Runner, and Maven services.

Install Gitlab

First of all, let's install Gitlab. If you don't know about Gitlab installation and use, you can refer to "10 minutes to build your own Git repository".

  • Run the Gitlab service with the following command, the note here is addedhostnameProperty, so that we can access Gitlab through the domain name (to avoid some unnecessary trouble),GITLAB_ROOT_PASSWORDThis environment variable allows you to directly set the password of the root account in Gitlab;
docker run --detach \
  --hostname git.macrozheng.com \
  --publish 10443:443 --publish 1080:80 --publish 1022: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
Copy the code
  • We need to get throughgit.macrozheng.comThis domain name is used to access Gitlab. If you don't have a domain name, you can do this by modifying the host file on your host.
192.168.7.134 git.macrozheng.com
Copy the code
  • Because our Gitlab runs on1080Nginx can be used as a reverse proxy. If you are not familiar with Nginx, you can see itYou don't know what Nginx does!, in the Nginx configuration foldergit.confThe configuration file contains the following contents:
server { listen 80; # support HTTP server_name git.macrozheng.com; # modify domain location / {proxy_pass http://192.168.7.134:1080; # set proxy service access address index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; }}Copy the code
  • And then we can go throughgit.macrozheng.comThis domain name to access Gitlab, enter the account passwordroot:12345678You can log in;

  • Upload our SpringBoot application code to Gitlab and Gitlab is ready! The thing to note here is that if you didn't specify it when you started GitlabhostnameIf your project HTTP access address is the container ID, you will not be able to access the Git repository using this address!

Install Gitlab Runner

Gitlab is just a code repository. To implement CI/CD, you need to install Gitlab-Runner. Gitlab-runner is the executor of tasks in Gitlab, which Gitlab calls when a task needs to be executed.

  • downloadgitlab-runnerDocker image, selectalpine-bleeding, this version is very small!
docker pull gitlab/gitlab-runner:alpine-bleeding
Copy the code
  • Run the following commandgitlab-runner;
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
Copy the code
  • Now if we look atgitlab-runnerThe following error will be found if the container log isconfig.tomlFile cannot be found, this problem need not worry when we willgitlab-runnerThis file is automatically generated when you register with Gitlab;
ERROR: Failed to load config stat /etc/gitlab-runner/config.toml: no such file or directory  builds=0
Copy the code
  • So what we need to do is we need togitlab-runnerRegister with Gitlab and open itProject-Settings-CI/CDFunction to obtain the address and token that runner needs to use for registration;

  • Next, use the following command to entergitlab-runnerThe interior of a container;
docker exec -it gitlab-runner /bin/bash
Copy the code
  • In the container, use the following command to register runner;
gitlab-runner register
Copy the code
  • Registration will appear interactive interface, prompting you to enter the registration address, token, executor type and other information, SSH executor can remotely execute Linux commands, very easy to use, recommended to use this!

  • After registration, we can find outconfig.tomlThe file has been generated, and the content is as follows. In the future, when you want to modify the configuration of Runner, you can directly change this file.
concurrent = 1 check_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"Copy the code
  • In the CI/CD Settings of Gitlab, we can find that a runner has successfully registered!

Install Maven

The SpringBoot project package relies on Maven, which needs to be installed on the server first.

  • Download Maven Linux installation package, download address: maven.apache.org/download.cg...

  • After the download is complete, run the following command to decompress the package to the specified directory:
cd/ mydata tar ZXVF - apache maven - 3.8.1 - bin. Tar. GzCopy the code
  • Modify the/etc/profileFile to add environment variable configuration:
exportMAVEN_HOME = / mydata/apache maven -- 3.8.1export PATH=$PATH:$MAVEN_HOME/bin
Copy the code
  • Test the installation by looking at the Maven version.
mvn -v
Copy the code
Maven home: /mydata/apache-maven-3.8.1 Java version: 1.8.0_292, vendor: AdoptOpenJDK, Runtime: /mydata/ Java /jdk1.8/jre Locale: en_US, platform Encoding: UTF-8 OS name:"linux", version: "3.10.0-957. El7. X86_64", arch: "amd64", family: "unix"
Copy the code

Install the JDK

JRE is installed on CentOS by default, but JDK is required to use Maven.

  • Download the JDK 8, download address: mirrors.tuna.tsinghua.edu.cn/AdoptOpenJD...

  • After the download is complete, decompress the JDK to the specified directory;
cd/mydata/ Java tar -zxvf openjdk8U-jdk_x64_linux_XXX.tar. gz mv openjdk8u-jdk_x64_linux_XXX.tar. gz jdk1.8Copy the code
  • in/etc/profileAdd environment variables to the fileJAVA_HOME.
vi /etc/profile
# add to profile file
exportJAVA_HOME = / mydata/Java/jdk1.8export PATH=$PATH:$JAVA_HOME/bin
# Make the modified profile take effect
. /etc/profile
Copy the code

use

All set, the next step is to automate the deployment of SpringBoot applications with Gitlab's CI/CD capabilities!

  • Start by adding it in the project's root directory.gitlab-ci.ymlFile, defines two tasks, one that packages the application code into a Jar package and copies it to the specified directory, and the other that runs the scriptrun.shPackage your app's Docker image and run it;
# Package tasks
build-job:
  stage: build
  # specifies the tag. Only runners with this tag will execute
  tags:
    - docker
  script:
    # Package with Maven
    - mvn clean package
    # copy jar package, Dockerfile, run script to specified directory
    - cp The target/mall - tiny - gitlab - 1.0 - the SNAPSHOT. The jar / mydata/build/mall - tiny - gitlab - 1.0 - the SNAPSHOT. The jar
    - cp Dockerfile /mydata/build/Dockerfile
    - cp run.sh /mydata/build/run.sh

# Deploy task
deploy-job:
  stage: deploy
  tags:
    - docker
  script:
    Go to the specified directory and execute the run script
    - cd /mydata/build
    - chmod +x run.sh
    - ./run.sh
Copy the code
  • It is worth mentioning here that by default runner will only execute jobs with the same tag, since we have tagged both Job and Runner asdocker, so we can implement it here. If you do not set the tag, you need to enable runner to execute the Job without the tag in the runner edit interface Settings;

  • Because of ourgitlab-runnerUSES asshWhich will log in to the server we specified and execute us in.gitlab-ci.ymlDefined in thescriptThe Git repository command will retrieve the code from the Git repository, so we need to modify the host file on the server.
Vim/etc/hosts 192.168.7.134 git.macrozheng.comCopy the code
  • The next step is to commit the script to the Git repositoryProject-CI/CD-PipelinesThe task that is being performed is found in

  • Open the details page of Pipeline, you can find that the two tasks defined by us have been successfully executed;

  • On the Job details page, you can view the logs generated during the task execution.

  • If you want to manually execute Pipelines instead of committing triggers, you can click on the Pipelines pageRun PipelineButton can be;

  • After the success of the operation, can access the project by the following address: http://192.168.7.134:8088/swagger-ui/

conclusion

If you use Gitlab as a Git repository, it's great to use its CI/CD capabilities for automated deployment! Install a lightweight Gitlab-Runner and write a simple.gitlab-ci.yml script file. In fact, we have previously introduced a variety of automated deployment solutions, such as Jenkins, Gogs+Drone, Gitlab CI/CD, and we can find a common denominator, these solutions are not based on Linux commands. So to play with automated deployment, you have to play with Linux commands first!

The resources

Official document: docs.gitlab.com/ee/ci/

Project source address

Github.com/macrozheng/...

In this paper, making github.com/macrozheng/... Welcome to Star!

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.