基于gitlab+jenkins+docker+harbor+pipeline,CI/CD

参考:

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博客


一、gitlab准备测试项目mytest-pipeline

下载地址:https://gitee.com/sunwanning/mytest-pipeline

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第1张图片

 准备Dockerfile 文件

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第2张图片

 Dockerfile 文件内容: 

FROM daocloud.io/library/java:8u40-jdk
COPY mytest-pipeline.jar /usr/local/
WORKDIR /usr/local
CMD java -jar mytest-pipeline.jar

  准备Jenkinsfile 文件

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第3张图片

 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

三、harbor新建仓库repo

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第4张图片

四、jenkins配置新任务

1. 新建流水线任务

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第5张图片

 2. 设置参数化构建过程 

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第6张图片

 3. 流水线配置

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第7张图片

 基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第8张图片

4. 生成 【拉取git仓库代码】 pipeline语法

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第9张图片

 基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第10张图片

 5. 生成 【通过maven构建项目】 pipeline语法

/var/jenkins_home/maven/bin/mvn clean package -DskipTests

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第11张图片

  6. 生成 【docker制作自定义镜像】 pipeline语法

mv ./target/*.jar ./docker/
docker build -t ${JOB_NAME}:${tag} ./docker/

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第12张图片

 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}

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第13张图片

8. 生成 【通过Public Over SSH通知目标服务器】 pipeline语法

deploy.sh $harborAddress $harborRepo $JOB_NAME $tag 8081 8080

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第14张图片

 基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第15张图片

        切记:将单引号改为双引号基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第16张图片

9. 将生成的pipeline语法放进Jenkinsfile文件中提交代码,构建pipeline项目

基于gitlab+jenkins+docker+harbor+pipeline,CI/CD_第17张图片

你可能感兴趣的:(jenkins,docker,ci)