docker部署gitlab与jenkins
Posted y_zilong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker部署gitlab与jenkins相关的知识,希望对你有一定的参考价值。
操作系统 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、docker安装jenkins
docker pull jenkins:2.60.3
docker pull jenkinszh/jenkins-zh:2.267 #中国定制版
#创建jenkins挂载目录并授权
mkdir -p /data/jenkins_home/
chmod 777 /data/jenkins_home/
#创建并启动Jenkins容器
docker run -d \\
-p 8090:8080 \\
-p 8091:50000 \\
-v /data/jenkins_home:/var/jenkins_home \\
-v /etc/localtime:/etc/localtime \\
-v /usr/local/apache-maven-3.8.5:/usr/local/maven \\
--name jenkins jenkins:2.60.3
-d #后台运行镜像
-p 8090:8080 #将镜像的8080端口映射到服务器的8090端口
-p 8091:50000 #将镜像的50000端口映射到服务器的8091端口
-v /data/jenkins_home:/var/jenkins_home #将硬盘上的一个目录挂载到Jenkins的工作目录,方便后面镜像更新镜像后继续使用原来的工作目录
-v /etc/localtime:/etc/localtime #让容器使用和服务器同样的时间设置
-v /usr/local/apache-maven-3.8.5:/usr/local/maven #挂载本地maven,前面是服务器上的,后面是挂载到容器上的目录
--name jenkins #给容器起一个名字
#查看docker容器日志
docker logs jenkins
#配置镜像加速,进入 cd /data/jenkins_home 目录
cd /data/jenkins_home
vim hudson.model.UpdateCenter.xml
#改成清华源
<?xml version='1.1' encoding='UTF-8'?>
<sites>
<site>
<id>default</id>
<url>https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json</url>
</site>
#重启jenkins
docker restart jenkins
#访问jenkins页面,输入你的ip加上8090
#查看密码
cat /data/jenkins_home/secrets/initialAdminPassword
docker run -d \\
-p 8090:8080 \\
-p 8091:50000 \\
-v /data/jenkins_home:/var/jenkins_home \\
-v /etc/localtime:/etc/localtime \\
--name jenkins jenkins:2.60.3
以上是关于docker部署gitlab与jenkins的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud+Docker+Jenkins+GitLab+Maven实现自动化构建与部署实战
SpringCloud+Docker+Jenkins+GitLab+Maven实现自动化构建与部署实战