Centos7安装docker:
Centos7安装docker_大板鲫的博客-CSDN博客
Centos7安装docker-compose:
Centos7安装docker-compose_大板鲫的博客-CSDN博客_centos7 docker-compose
Centos7安装Git:
Centos7安装Git_大板鲫的博客-CSDN博客
Centos7安装gitlab:
Centos7安装gitlab_大板鲫的博客-CSDN博客
Centos7安装jenkins:
Centos7安装配置jenkins_大板鲫的博客-CSDN博客_centos jenkins安装
Centos7安装Harbor:
Centos7安装Harbor_大板鲫的博客-CSDN博客
下载地址:https://gitee.com/sunwanning/mytest-pipeline
准备Dockerfile 文件
Dockerfile 文件内容:
FROM daocloud.io/library/java:8u40-jdk
COPY mytest-pipeline.jar /usr/local/
WORKDIR /usr/local
CMD java -jar mytest-pipeline.jar
准备Jenkinsfile 文件
Jenkinsfile 文件内容:
// 所有脚本命令包含在pipeline{}中
pipeline {
// 指定任务在哪个节点执行(Jenkins支持分布式)
agent any
// 配置全局环境,指定变量名=变量值信息
environment{
harborAddress = '192.168.31.15:80'
harborRepo = 'repo'
harborUser = 'admin'
harborPasswd = 'Harbor12345'
}
// 存放所有任务的合集
stages {
stage('拉取git仓库代码') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitlab', url: 'ssh://[email protected]:2224/gitlab-instance-5ddc8f31/mytest-pipeline.git']]])
}
}
stage('通过maven构建项目') {
steps {
sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
}
}
stage('通过docker制作自定义镜像') {
steps {
sh '''mv ./target/*.jar ./docker/
docker build -t ${JOB_NAME}:${tag} ./docker/'''
}
}
stage('将自定义镜像推送到Harbor') {
steps {
sh '''docker login -u ${harborUser} -p ${harborPasswd} ${harborAddress}
docker tag ${JOB_NAME}:${tag} ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}
docker push ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}
'''
}
}
stage('通过Public Over SSH通知目标服务器') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: 'test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "deploy.sh $harborAddress $harborRepo $JOB_NAME $tag 8081 8080", execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
创建 deploy.sh文件
cd ~
vi deploy.sh
deploy.sh文件内容:
#harbor地址
harbor_addr=$1
#仓库名称
harbor_repo=$2
#项目名称
project=$3
#版本标签
version=$4
#宿主机端口
host_port=$5
#映射端口
container_port=$6
#harbor镜像地址
imageName=$harbor_addr/$harbor_repo/$project:$version
#容器id
containerId=`docker ps -a | grep ${project} | awk '{print $1}'`
#如果容器正在运行,先停止,后删除
if [ "$containerId" != "" ] ; then
docker stop $containerId
docker rm $containerId
echo "Delete Container Success"
fi
#镜像tag版本
tag=`docker images | grep ${project} | awk '{print $2}'`
#如果此版本镜像存在则删除
if [[ "$tag" =~ "$version" ]] ; then
docker rmi -f $imageName
echo "Delete Image Success"
fi
#登录harbor
docker login -u admin -p Harbor12345 $harbor_addr
#拉取镜像
docker pull $imageName
#运行容器
docker run -d -p $host_port:$container_port --name $project $imageName
echo "Start Container Success"
移动deploy.sh文件至/usr/bin目中,使文件可以在任何地方运行
#增加执行权限
chmod 777 ./deploy.sh
mv deploy.sh /usr/bin
1. 新建流水线任务
2. 设置参数化构建过程
3. 流水线配置
4. 生成 【拉取git仓库代码】 pipeline语法
5. 生成 【通过maven构建项目】 pipeline语法
/var/jenkins_home/maven/bin/mvn clean package -DskipTests
6. 生成 【docker制作自定义镜像】 pipeline语法
mv ./target/*.jar ./docker/
docker build -t ${JOB_NAME}:${tag} ./docker/
7. 生成 【将自定义镜像推送到Harbor】 pipeline语法
docker login -u ${harborUser} -p ${harborPasswd} ${harborAddress}
docker tag ${JOB_NAME}:${tag} ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}
docker push ${harborAddress}/${harborRepo}/${JOB_NAME}:${tag}
8. 生成 【通过Public Over SSH通知目标服务器】 pipeline语法
deploy.sh $harborAddress $harborRepo $JOB_NAME $tag 8081 8080
9. 将生成的pipeline语法放进Jenkinsfile文件中提交代码,构建pipeline项目