gitlab的CICD

Posted y_zilong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gitlab的CICD相关的知识,希望对你有一定的参考价值。

操作系统 centos7.9

1、docker安装gitlab

[root@gitlab ~]$hostname 
gitlab.yzl.cn
[root@gitlab ~]$mkdir -p /data/gitlab/config 
[root@gitlab ~]$mkdir -p /data/gitlab/logs
[root@gitlab ~]$mkdir -p /data/gitlab/data 
[root@gitlab ~]$docker run --detach \\
  --hostname gitlab.yzl.cn \\
  --publish 443:443 --publish 80:80 --publish 222:22 \\
  --name gitlab \\
  --restart always \\
  --volume /data/gitlab/config:/etc/gitlab \\
  --volume /data/gitlab/logs:/var/log/gitlab \\
  --volume /data/gitlab/data:/var/opt/gitlab \\
  --shm-size 256m \\
  gitlab/gitlab-ce:14.6.6-ce.0

docker exec -it gitlab /bin/bash
vi /etc/gitlab/gitlab.rb   #编辑站点地址
external_url 'http://10.0.0.6' 
gitlab-ctl reconfigure   #配置

docker restart gitlab

#服务控制
docker start gitlab
docker stop gitlab
docker rm gitlab

2、docker安装gitlab-runner

  docker run -d --name gitlab-runner --restart always \\
     -v /data/gitlab-runner/config:/etc/gitlab-runner \\
     -v /var/run/docker.sock:/var/run/docker.sock \\
     gitlab/gitlab-runner:latest
     
   docker run -d --name gitlab-runner --restart always \\
     -v /data/gitlab-runner/config:/etc/gitlab-runner \\
     -v /var/run/docker.sock:/var/run/docker.sock \\
     gitlab/gitlab-runner:v14.0.0
[root@gitlab ~]$docker exec -it gitlab-runner /bin/bash
root@92d6b38efa6e:/# gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=29 revision=3b6f852e version=14.0.0
Running in system-mode.                            
                                                   
Enter the GitLab instance URL (for example, https://gitlab.com/):
http://10.0.0.7/
Enter the registration token:
fsytcND1SuuBo3cfiYY5
Enter a description for the runner:
[92d6b38efa6e]: test   
Enter tags for the runner (comma-separated):
test
Registering runner... succeeded                     runner=fsytcND1
Enter an executor: ssh, virtualbox, docker-ssh+machine, docker, docker-ssh, shell, kubernetes, custom, parallels, docker+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 

#免交互注册:
gitlab-runner register \\
  --non-interactive \\
  --executor "shell" \\
  --url "http://10.0.0.7" \\
  --registration-token "fsytcND1SuuBo3cfiYY5" \\
  --description "test1" \\
  --tag-list "build,deploy" \\
  --run-untagged="true" \\
  --locked="false"
  
[root@gitlab ~]$docker exec -it gitlab-runner /bin/bash
root@92d6b38efa6e:/# gitlab-runner register \\
>   --non-interactive \\
>   --executor "shell" \\
>   --url "http://10.0.0.7" \\
>   --registration-token "fsytcND1SuuBo3cfiYY5" \\
>   --description "test1" \\
>   --tag-list "build,deploy" \\
>   --run-untagged="true" \\
>   --locked="false"
Runtime platform                                    arch=amd64 os=linux pid=59 revision=3b6f852e version=14.0.0
Running in system-mode.                            
                                                   
Registering runner... succeeded                     runner=fsytcND1
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 

3、maven搭建

#需要支持jdk环境,先安装jdk

[root@git src]# cat install_jdk.sh 
#!/bin/bash
DIR=`pwd`
JDK_FILE="jdk-8u291-linux-x64.tar.gz"
JDK_DIR="/usr/local"

color () 
    RES_COL=60
    MOVE_TO_COL="echo -en \\\\033[$RES_COLG"
    SETCOLOR_SUCCESS="echo -en \\\\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\\\033[1;32m"
    SETCOLOR_WARNING="echo -en \\\\033[1;33m"
    SETCOLOR_NORMAL="echo -en \\E[0m"
    echo -n "$2" && $MOVE_TO_COL
    echo -n "["
    if [ $1 = "success" -o $1 = "0" ] ;then
	    $SETCOLOR_SUCCESS
	    echo -n $" OK "
    elif [ $1 = "failure" -o $1 = "1" ] ; then
	    $SETCOLOR_FAILURE
	    echo -n $"FAILED"
    else
	    $SETCOLOR_WARNING
	    echo -n $"WARNING"
    fi
    $SETCOLOR_NORMAL
    echo -n "]"
    echo


install_jdk () 
if ! [ -f "$DIR/$JDK_FILE" ] ;then
	color 1 "$JDK_FILE 文件不存在"
	exit;
elif [ -d $JDK_DIR/jdk ] ;then
	color 1 "JDK 已经安装"
        exit;
else  
	[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk1.8.* jdk

cat > /etc/profile.d/jdk.sh << EOF
export JAVA_HOME=$JDK_DIR/jdk
export JRE_HOME=\\$JAVA_HOME/jre
export CLASSPATH=\\$JAVA_HOME/lib/:\\$JRE_HOME/lib/
export PATH=\\$PATH:\\$JAVA_HOME/bin
EOF
source /etc/profile.d/jdk.sh
java -version && color 0 "JDK 安装完成" ||  color 1 "JDK 安装失败" ; exit;



install_jdk

[root@git src]# source /etc/profile.d/jdk.sh 
[root@git src]# java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)

#安装maven

[root@git src]# wget https://dlcdn.apache.org/maven/maven-3/3.8.5/binaries/apache-maven-3.8.5-bin.tar.gz
[root@git src]# tar -xvf apache-maven-3.8.5-bin.tar.gz -C /usr/local/
[root@git src]# cd /usr/local/apache-maven-3.8.5/

#配置系统环境变量
[root@git apache-maven-3.8.5]# pwd
/usr/local/apache-maven-3.8.5
[root@git apache-maven-3.8.5]# vim /etc/profile.d/maven.sh
[root@git apache-maven-3.8.5]# cat /etc/profile.d/maven.sh 

export JAVA_HOME=/usr/local/jdk
export MAVEN_HOME=/usr/local/apache-maven-3.8.5
export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$JRE_HOME/bin:$JAVA_HOME:$PATH

[root@git apache-maven-3.8.5]# source /etc/profile.d/maven.sh 
[root@git apache-maven-3.8.5]# mvn -version
Apache Maven 3.8.5 (3599d3414f046de2324203b78ddcf9b5e4388aa0)
Maven home: /usr/local/apache-maven-3.8.5
Java version: 1.8.0_291, vendor: Oracle Corporation, runtime: /usr/local/jdk1.8.0_291/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.59.1.el7.x86_64", arch: "amd64", family: "unix"

#配置maven本地仓库路径和阿里云镜像下载地址
1、创建本地仓库目录maven_repository
这里创建在$MAVEN_HOME/maven_repository

[root@git apache-maven-3.8.5]# pwd
/usr/local/apache-maven-3.8.5
[root@git apache-maven-3.8.5]# mkdir maven_repository

2、修改maven目录下conf目录的settings.xml

[root@git conf]# vim settings.xml

<!--配置本地仓库地址(自己创建的本地仓库位置)-->
<localRepository>/usr/local/apache-maven-3.8.5/maven_repository</localRepository>

<!--配置镜像仓库(阿里云仓库)-->
<mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>

<!--JDK版本-->
<profile>
        <id>jdk-1.8</id>
        <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
        </activation>
        <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
</profile>

4、.gitlab-ci.yml编写

stages:
 - build
 - test
 - deploy
 build:
  stage: build
  tags:
     - build
     script:
     - echo "代码编译"
  test:
      stage: test
      tags:
      - test
      script:
      - echo "测试代码"
   deploy:
      stage: deploy
      tags:
      - deploy
      script:
      -echo "项目部署"
      -scp index.html root@10.0.0.8:/usr/share/nginx/html/

以上是关于gitlab的CICD的主要内容,如果未能解决你的问题,请参考以下文章

Gitlab-CICD最简单明了的入门教程

Gitlab-CICD最简单明了的入门教程

原创笔记CICD系列之一:安装gitlab

Docker+Harbor+Jenkins+Gitlab自动化CICD构建

Kubernetes之基于gitlab的CICD自动化

CICD GitLab的搭建与使用