Jenkins 自动化构建maven项目(一)

Jenkins中常见的构建项目方式

1. 流水线项目(Pipeline project)
2. maven项目(Maven project)
3. 自由风格的项目
4. ...

Pipeline 流水线项目(java 演示)

新建项目

选择右侧菜单, 选择新建任务(New Item), 输入任务名称(随意) simple-pipeline-java, 选择流水线构建类型,点击确定

编辑任务内容

General中 描述可自定义,例如 一个简单的流水线java项目, 然后选择流水线(tab菜单)
1642299725(1).png

准备工作

随意生成一个java代码,提交到自定义仓库,或github,gitee,gitlab等
我这边使用的是gitee。

插件管理中,新增gitee

编辑流水线代码pipeline的简单说明

  1. 拉取代码


    1642301840(1).png
pipeline {
  agent any
  stages {
    stage('Checkout From gitee') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitee-java', url: 'https://gitee.com/simonzz/demo1.git']]])
      }
    }
  }
}

我们在blue ocean中运行,等待执行成功。


1642302064(1).png
  • 截图中,我们可以看到 项目clone到/var/jenkins_home/worksspace/simple-pipleline-java ,可登录到容器查看,这里就不做演示了。
  1. Maven Build项目
docker exec -it jenkins1 /bin/sh

2.1. 下载maven

# 切到临时文件夹
cd /tmp
# 获取maven 
wget https://dlcdn.apache.org/maven/maven-3/3.8.4/binaries/apache-maven-3.8.4-bin.tar.gz
# 解压maven
tar xzvf apache-maven-3.8.4-bin.tar.gz
# 移动到 /opt文件下
mv apache-maven-3.8.4 /opt/
# 添加到PATH环境变量中(可在文件最后添加)
vi /etc/profile
export MAVEN_HOME=/opt/apache-maven-3.8.4
export PATH=$MAVEN_HOME/bin:$PATH
# 重新加载
source /etc/profile
# 验证是否安装成功
mvn -v
0903517ce1a1:~# mvn -v
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: /opt/apache-maven-3.8.4
Java version: 11.0.13, vendor: Eclipse Adoptium, runtime: /opt/java/openjdk
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "4.19.128-microsoft-standard", arch: "amd64", family: "unix"

2.2 使用maven 打包 (stages下追加)

    stage('Maven Build') {
        steps {
            sh '/opt/apache-maven-3.8.4/bin/mvn -v'

        }
    }

· 指定mvn路径


1642304859(1).png

docker 构建镜像

docker-in-docker

  • 定义仓库repository 镜像名称
  • 通过BUILD_NUMBER构建镜像
  • 将构建的镜像追加最新镜像标签 latest
    在stages 中追加
environment {
 REPOSITORY_IMAGE_NAME='docker容器仓库'
 CONTAINER_NAME='simple-jenkins-java'
}
stages {
stage('Build Docker Image') {
  steps {
    sh 'docker build -t $REPOSITORY_IMAGE_NAME:BUILD_$BUILD_NUMBER .'
    sh 'docker tag $REPOSITORY_IMAGE_NAME:BUILD_$BUILD_NUMBER $REPOSITORY_IMAGE_NAME:latest'
  }
}
}
  • 提交到仓库(这里使用到pipeline ssh插件)
environment {
  ...
  LOGIN_DOCKER='docker login -u **** -p ****'
}
stages {
  stage('Push to Repository') {
    steps {
      sh "$LOGIN_DOCKER"
      sh "docker push $REPOSITORY_IMAGE_NAME:BUILD_$BUILD_NUMBER"
      sh "docker push $REPOSITORY_IMAGE_NAME:latest"
    }
  }
}
1642327633(1).png
  • WARNING! Using --password via the CLI is insecure. Use --password-stdin
  • WARNING! Your password will be stored unencrypted in /root/.docker/config.json.

docker login | Docker Documentation

部署到服务器

    stage('Publish to Server') {
        steps {
            script {
                def remote = [:]
                remote.name = "ssh-test"
                remote.host = "172.17.0.5"
                remote.user = 'root'
                remote.password = 'xyj12345'
                remote.allowAnyHosts = true
                sshScript remote: remote, script: "$PUBLISH"
            }
        }
    }

编辑部署脚本

update.sh 代码, 文件存放在项目根目录下即可

APP_NAME='simple-jenkins-test'
# 停止和删除原来的docker
OLD="$(docker ps --all --quiet --filter=name="$APP_NAME")"
if [[ -n "$OLD" ]]; then
        docker stop $APP_NAME
        docker rm $APP_NAME
fi
docker login -u your-name -p your-password
docker run -itd -p 8086:8080 --name $APP_NAME zhangpengxyj/jenkins-test-demo1
docker logout

完整pipeline

pipeline {
  environment {
      REPOSITORY_IMAGE_NAME='zhangpengxyj/jenkins-test-demo1'
      CONTAINER_NAME='simple-jenkins-java'
      LOGIN_DOCKER='cat ~/docker_passwd.txt | docker login -u zhangpengxyj --password-stdin'
      PUBLISH='update.sh'
  }
  agent any
  
  stages {
    stage('Checkout From gitee') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitee-java', url: 'https://gitee.com/simonzz/demo1.git']]])
      }
    }
    stage('Package to jar') {
        steps {
            sh '/usr/bin/apache-maven/bin/mvn -v'
            sh '/usr/bin/apache-maven/bin/mvn clean package -ff -q -Dmaven.test.skip=true'
        }
    }
    stage('Build Docker Image') {
        steps {
            sh 'docker build -t $REPOSITORY_IMAGE_NAME:BUILD_$BUILD_NUMBER .'
            sh 'docker tag $REPOSITORY_IMAGE_NAME:BUILD_$BUILD_NUMBER $REPOSITORY_IMAGE_NAME:latest'
        }
    }
    stage('Push to Repository') {
        steps {
            sh "$LOGIN_DOCKER"
            sh "docker push $REPOSITORY_IMAGE_NAME:BUILD_$BUILD_NUMBER"
            sh "docker push $REPOSITORY_IMAGE_NAME:latest"
        }
    }
    stage('Publish to Server') {
        steps {
            script {
                def remote = [:]
                remote.name = "ssh-test"
                remote.host = "172.17.0.5"
                remote.user = 'root'
                remote.password = 'xyj12345'
                remote.allowAnyHosts = true
                sshScript remote: remote, script: "$PUBLISH"
            }
        }
    }
  }
}

部署方法2 SSH 登录远程服务器部署

jenkins 免密码登录远程服务器

远程服务器中添加部署脚本

root@a49671bc99ee:~# pwd
/root
# 追加部署内容,可以将上述的update.sh复制到这里,权限为755
root@a49671bc99ee:~# vi update.sh 
·
APP_NAME='simple-jenkins-test'
# 停止和删除原来的docker
OLD="$(docker ps --all --quiet --filter=name="$APP_NAME")"
if [[ -n "$OLD" ]]; then
        docker stop $APP_NAME
        docker rm $APP_NAME
fi
docker login -u your-name -p your-password
docker run -itd -p 8086:8080 --name $APP_NAME -e "spring.profiles.active=test" zhangpengxyj/jenkins-test-demo1
docker logout
·
root@a49671bc99ee:~# ls -al
total 52
drwx------ 1 root root 4096 Jan 17 10:14 .
drwxr-xr-x 1 root root 4096 Jan 17 10:10 ..
-rw------- 1 root root 1789 Jan 17 10:14 .bash_history
-rw-r--r-- 1 root root 3106 Dec  5  2019 .bashrc
drwx------ 2 root root 4096 Jan 16 17:26 .cache
drwx------ 2 root root 4096 Jan 17 10:14 .docker
-rw-r--r-- 1 root root  161 Dec  5  2019 .profile
drwx------ 2 root root 4096 Jan 17 09:57 .ssh
-rw------- 1 root root 8626 Jan 17 10:10 .viminfo
-rwxr-xr-x 1 root root  302 Jan 17 10:14 update.sh

pipeline中追加部署内容

pipeline {
  ...
  stages {
    ...
    stage {
      steps {
        sh "ssh [email protected] ./update.sh"
      }
    }
  }
}

完整pipeline

pipeline {
  environment {
      REPOSITORY_IMAGE_NAME='zhangpengxyj/jenkins-test-demo1'
      CONTAINER_NAME='simple-jenkins-java'
      LOGIN_DOCKER='cat ~/docker_passwd.txt | docker login -u zhangpengxyj --password-stdin'
      PUBLISH='ssh [email protected] ./update.sh'
  }
  agent any
  
  stages {
    stage('Checkout From gitee') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitee-java', url: 'https://gitee.com/simonzz/demo1.git']]])
      }
    }
    stage('Package to jar') {
        steps {
            sh '/usr/bin/apache-maven/bin/mvn -v'
            sh '/usr/bin/apache-maven/bin/mvn clean package -ff -q -Dmaven.test.skip=true'
        }
    }
    stage('Build Docker Image') {
        steps {
            sh 'docker build -t $REPOSITORY_IMAGE_NAME:BUILD_$BUILD_NUMBER .'
            sh 'docker tag $REPOSITORY_IMAGE_NAME:BUILD_$BUILD_NUMBER $REPOSITORY_IMAGE_NAME:latest'
        }
    }
    stage('Push to Repository') {
        steps {
            sh "$LOGIN_DOCKER"
            sh "docker push $REPOSITORY_IMAGE_NAME:BUILD_$BUILD_NUMBER"
            sh "docker push $REPOSITORY_IMAGE_NAME:latest"
            sh "docker logout"
        }
    }
    stage('Publish to Server') {
        steps {
            sh "$PUBLISH"
        }
    }
  }
}

END. 有问题可以交流

你可能感兴趣的:(Jenkins 自动化构建maven项目(一))