环境准备:
1、部署gitlab
1.1 部署gitlab
[root@k8s-node2 ~]# mkdir gitlab
[root@k8s-node2 ~]# cd gitlab docker run -d \
--name gitlab \
-p 8443:443 \
-p 9999:80 \
-p 9998:22 \
-v $PWD/config:/etc/gitlab \
-v $PWD/logs:/var/log/gitlab \
-v $PWD/data:/var/opt/gitlab \
-v /etc/localtime:/etc/localtime \
lizhenliang/gitlab-ce-zh:latest
访问地址:http://IP:9999
初次会先设置管理员密码 ,然后登陆,默认管理员用户名root,密码就是刚设置的。
1.2 创建项目,提交测试代码
进入后先创建项目,提交代码,以便后面测试。
项目名称java-demo,可见等级私有,创建
默认使用的是主机名
现在已经创建好仓库,现在往仓库提交代码
示例java demo包
tomcat-java-demo-master
先将自己仓库项目代码拉下来,因为没有内容,所以提示是空的
[root@k8s-node2 ~]# git clone http://192.168.146.131:9999/root/java-demo.git
Cloning into 'java-demo'...
Username for 'http://192.168.146.131:9999': root
Password for 'http://root@192.168.146.131:9999':
warning: You appear to have cloned an empty repository.
进入到java-demo目录,将代码拷贝到当前目录,提交到自己的gitlab仓库里边
[root@k8s-node2 ~]# cd java-demo/
[root@k8s-node2 java-demo]# cp ../tomcat-java-demo-master/* ./ -rf
[root@k8s-node2 java-demo]# ls
db Dockerfile LICENSE pom.xml README.md src
#先提交到暂存区
[root@k8s-node2 java-demo]# git add .
[root@k8s-node2 java-demo]# git commit -m 'all'
#提交到远程仓库master分支
[root@k8s-node2 java-demo]# git push origin master
刷新gltlab页面
2、部署Harbor镜像仓库
上篇已经安装部署
3、部署Jenkins
3.1 准备JDK和Maven环境
# tar zxvf jdk-8u45-linux-x64.tar.gz
# mv jdk1..0_45 /usr/local/jdk
# tar zxf apache-maven-3.5.-bin.tar.gz
# mv apache-maven-3.5. /usr/local/maven
docker run -d --name jenkins -p 80:8080 -p 50000:50000 -u root \
-v /opt/jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker \
-v /usr/local/apache-maven-3.5.0:/usr/local/maven \
-v /usr/local/jdk1.8.0_45:/usr/local/jdk \
-v /etc/localtime:/etc/localtime \
--name jenkins jenkins/jenkins:lts
进入到容器中,查看是否已有jdk和maven
[root@k8s-node3 ~]# docker exec -it jenkins bash
root@e06952269e6c:/# ls /usr/local/jdk/
COPYRIGHT LICENSE README.html THIRDPARTYLICENSEREADME-JAVAFX.txt THIRDPARTYLICENSEREADME.txt bin db include javafx-src.zip jre lib man release src.zip
root@e06952269e6c:/# ls -l /usr/local/ma
man/ maven/
root@e06952269e6c:/# ls -l /usr/local/maven/
total
-rw-r--r--. dialout Apr LICENSE
-rw-r--r--. dialout Apr NOTICE
-rw-r--r--. dialout Apr README.txt
drwxr-xr-x. root root Dec : bin
drwxr-xr-x. root root Dec : boot
drwxr-xr-x. dialout Apr conf
drwxr-xr-x. dialout Dec : lib
默认80端口
[root@k8s-node3 ~]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e06952269e6c jenkins/jenkins:lts "/sbin/tini -- /usr/…" seconds ago Up seconds 0.0.0.0:->/tcp, 0.0.0.0:->/tcp jenkins
jenkins的文件再这个目录,只要备份这个数据目录,可以再其它机器上重新运行命令再起一个
[root@k8s-node3 ~]# ls /opt/jenkins_home/
config.xml jenkins.install.InstallUtil.lastExecVersion jobs plugins updates
copy_reference_file.log jenkins.install.UpgradeWizard.state logs secret.key userContent
hudson.model.UpdateCenter.xml jenkins.model.JenkinsLocationConfiguration.xml nodeMonitors.xml secret.key.not-so-secret users
identity.key.enc jenkins.telemetry.Correlator.xml nodes secrets war
可以再logs里获取密码 docker logs jenkins
选择插件安装,先不装任何插件
选择无,点击安装
用户admin,密码自己设置一个
开始使用Jenkins
3.2 安装插件
系统管理-->插件管理-->Installed
搜索git/pipeline,点击安装。
如果安装慢,可以更新一下插件源地址
cd /opt/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
创建jobs,名字test
4、发布测试
4.1 添加参数化构建
This project is parameterized -> String Parameter
Name:Branch # 变量名,下面脚本中调用
Default Value:master # 默认分支
Description:发布的代码分支 # 描述
${BUILD_NUMBER} 这个变量是Jenkins自身记录的版本号,作为镜像的tag,保证其唯一性
4.2 Pipeline脚本
#!/usr/bin/env groovy def registry = "192.168.146.130" #指定仓库地址
def project = "welcome"
def app_name = "java-demo"
def image_name = "${registry}/${project}/${app_name}:${Branch}-${BUILD_NUMBER}"
def git_address = "http://192.168.146.131:9999/root/java-demo.git"
def docker_registry_auth = "bf0b3689-c1c7-4bc4-9189-e5a53246efe1"
def git_auth = "3fca4572-f8c3-4132-9a30-f5b948e41983" 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
java -version
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 lizhenliang
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 """
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变量值。
完成后 ,构建测试发布
可以看到每一步的操作跟日志
注:默认是*官方地址进行拉取操作的,我们可以修改为阿里云的地址
/usr/local/apache-maven-3.5.0/conf这个目录路径下编辑settings.xml
<mirror>
<id>central</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
Harbor镜像仓库里边可以看到发布的版本