centos7.9环境基于docker和pipeline构建jenkins的ci平台
Posted reblue520
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7.9环境基于docker和pipeline构建jenkins的ci平台相关的知识,希望对你有一定的参考价值。
1、部署gitlab
1.1 部署gitlab
docker run -d \\
--name gitlab \\
-p 8443:443 \\
-p 9999:80 \\
-p 9998:22 \\
-v /data/gitlab/config:/etc/gitlab \\
-v /data/gitlab/logs:/var/log/gitlab \\
-v /data/gitlab/data:/var/opt/gitlab \\
-v /etc/localtime:/etc/localtime \\
--restart=always \\
lizhenliang/gitlab-ce-zh:latest
访问地址:http://IP:9999
初次会先设置管理员密码 ,然后登陆,默认管理员用户名root,密码就是刚设置的。
root:admin888
1.2 创建项目,提交测试代码
进入后先创建项目,提交代码,以便后面测试
yum install -y git
mkdir git_demoair
cd git_demoair
# 将代码解压,并且推送到git仓库中
unzip tomcat-java-demo-master.zip
cd tomcat-java-demo-master
git init
git config --global user.email "admin@163.com"
git config --global user.name "admin"
git remote add origin http://10.11.0.232:9999/root/java-demo.git
git add .
git commit -m "init commit"
git push -u origin master
# git删除目录命令(保留目录:git rm -r --cached src db Dockerfile LICENSE pom.xml README.md)
git rm -r xx目录
git commit -m "delete xx目录"
git push origin master
2、部署Harbor镜像仓库
2.1 安装docker与docker-compose
# wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
# yum install docker-ce -y
# yum install docker-compose
# systemctl start docker
# systemctl enable docker
2.2 解压离线包部署
# tar zxvf harbor-offline-installer-v1.9.1.tgz
# cd harbor
# vi harbor.yml
hostname: 10.11.0.150
port: 8888
# ./prepare
# ./install.sh
2.3 在Jenkins主机配置Docker可信任
由于habor未配置https,还需要在docker配置可信任。
# cat /etc/docker/daemon.json
"registry-mirrors": ["http://qj4ewogx.mirror.aliyuncs.com"],
"insecure-registries": ["10.11.0.150"]
# systemctl restart docker
3、部署Jenkins
3.1 准备JDK和Maven环境
# tar xf jdk-8u261-linux-x64.tar.gz
# mv jdk1.8.0_261 /usr/local/jdk
# tar zxf apache-maven-3.6.1-bin.tar.gz
# mv apache-maven-3.6.1 /usr/local/maven
docker run -d --name jenkins -p 80:8080 -p 50000:50000 -u root \\
-v /data/jenkins_home:/var/jenkins_home \\
-v /var/run/docker.sock:/var/run/docker.sock \\
-v /usr/bin/docker:/usr/bin/docker \\
-v /usr/local/maven:/usr/local/maven \\
-v /usr/local/jdk:/usr/local/jdk \\
-v /etc/localtime:/etc/localtime \\
--name jenkins jenkins/jenkins:lts
密码: admin/admin888
maven 的设置
maven设置网络代理 和 源地址
ccproxy
true
http
10.11.0.150
808
127.0.0.1
基于 centos:7 镜像制作自定义mycentos:v2
# 交互式运行centos:7
# docker run -it --name=mycentos
# 在运行的mycentos容器中修改网络
# vi /etc/profile
export http_proxy=http://10.11.0.150:808
export https_proxy="https://10.11.0.150:808"
export ftp_proxy=http://10.11.0.150:808
export socks_proxy="socks://10.11.0.150:808/"
export http_proxy=http://10.11.0.150:808
export https_proxy=https://10.11.0.150:808
# echo "proxy=http://10.11.0.150:808" >> /etc/yum.conf
# source /etc/profile
# 替换yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 安装常用工具
yum install -y iproute net-tools telnet wget curl tree iftop htop iotop lrzsz
# 提交镜像为 mycentos:v1
# docker commit mycentos mycentos:v1
编译安装tomcat
下载地址:https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.43/bin/apache-tomcat-8.5.43.tar.gz
编写Dockerfile
FROM mycentos:v2
LABEL maintainer www.chinasoft.com
ENV VERSION=8.5.43
RUN source /etc/profile && \\
yum install java-1.8.0-openjdk wget curl unzip iproute net-tools -y && \\
yum clean all && \\
rm -rf /var/cache/yum/*
COPY apache-tomcat-$VERSION.tar.gz /
RUN source /etc/profile && \\
tar zxf apache-tomcat-$VERSION.tar.gz && \\
mv apache-tomcat-$VERSION /usr/local/tomcat && \\
rm -rf apache-tomcat-$VERSION.tar.gz /usr/local/tomcat/webapps/* && \\
mkdir /usr/local/tomcat/webapps/test && \\
echo "ok" > /usr/local/tomcat/webapps/test/status.html && \\
sed -i '1a JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"' /usr/local/tomcat/bin/catalina.sh && \\
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/tomcat/bin
WORKDIR /usr/local/tomcat
EXPOSE 8080
CMD ["catalina.sh", "run"]
java包
FROM java:8-jdk-alpine
LABEL maintainer www.chinasoft.com
ENV JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8 -Duser.timezone=GMT+08"
RUN apk add -U tzdata && \\
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./target/eureka-service.jar ./
EXPOSE 8888
CMD java-jar $JAVA_OPTS /eureka-service.jar
3.3 安装插件
如果安装慢,可以更新一下插件源地址
cd /data/jenkins_home/updates
sed -i 's/http:\\/\\/updates.jenkins-ci.org\\/download/https:\\/\\/mirrors.tuna.tsinghua.edu.cn\\/jenkins/g' default.json
sed -i 's/http:\\/\\/www.google.com/https:\\/\\/www.baidu.com/g' default.json
系统管理-->插件管理-->Installed
搜索git/pipeline,点击安装。
4、发布测试
4.1 添加参数化构建
This project is parameterized -> String Parameter
Name:Branch # 变量名,下面脚本中调用
Default Value:master # 默认分支
Description:发布的代码分支 # 描述
4.2 Pipeline脚本
def registry = "10.11.0.232"
def project = "welcome"
def app_name = "demo"
def image_name = "$registry/$project/$app_name:$Branch-$BUILD_NUMBER"
def git_address = "http://10.11.0.232:9999/root/java-demo.git"
def docker_registry_auth = "fb0e0bfe-7875-40a0-bc08-8898c6644cca"
# jenkins凭证id
def git_auth = "e7c91602-823f-484c-ad6f-d1497bf2d36c"
pipeline
agent any
stages
stage('拉取代码')
steps
checkout([$class: 'GitSCM', branches: [[name: '$Branch']], userRemoteConfigs: [[credentialsId: "$git_auth", url: "$git_address"]]])
stage('代码编译')
steps
sh """
JAVA_HOME=/usr/local/jdk
PATH=$JAVA_HOME/bin:/usr/local/maven/bin:$PATH
mvn clean package -Dmaven.test.skip=true
"""
stage('构建镜像')
steps
withCredentials([usernamePassword(credentialsId: "$docker_registry_auth", passwordVariable: 'password', usernameVariable: 'username')])
sh """
echo '
FROM $registry/library/tomcat:v1
LABEL maitainer chinasoft
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
' > Dockerfile
docker build -t $image_name .
docker login -u $username -p '$password' $registry
docker push $image_name
"""
stage('部署到Docker')
steps
sh """
REPOSITORY=$image_name
docker rm -f tomcat-java-demo |true
docker container run -d --name tomcat-java-demo -p 88:8080 $image_name
"""
4.3 添加凭据
1、添加拉取git代码凭据,并获取id替换到上面git_auth变量值。
2、添加拉取harbor镜像凭据,并获取id替换到上面docker_registry_auth变量值。
harbor的jenkins凭据
4.4 通过jenkins进行代码的发布
1、可以看到通过jenkins和pipeline能顺利编译并运行起项目
2、模拟代码修改并提交gitlab,并且重新执行jenkins编译,可以看到成功修改
[root@node01 git_demoair]# vim src/main/resources/templates/index.ftl
[root@node01 git_demoair]# git add .
[root@node01 git_demoair]# git commit -m "modify index.ftl"
以上是关于centos7.9环境基于docker和pipeline构建jenkins的ci平台的主要内容,如果未能解决你的问题,请参考以下文章
centos7.9环境基于docker和pipeline构建jenkins的ci平台
基于ansible在远程centos服务器docker环境安装docker-gogs