Jenkins (二)

Jenkins (二)

使用pipeline script
简单编译
发布war工程到远程tomcat中

配置所需

下载 apache-maven-3.9.3.tar.gz
解压 apache-maven-3.9.3-bin.tar.gz
拷贝到 docker jenkins 镜像里

$ docker cp apache-maven-3.9.3 37259c708ca1:/home/

下载apache-tomcat-8.5.91.tar.gz

修改 ./apache-tomcat-8.5.91/conf/server.xml port

开启 ./apache-tomcat-8.5.91/conf/tomcat-user.xml 角色,用户

  <role rolename="tomcat" />
  <role rolename="admin"/>
  <role rolename="admin-gui"/>
  <role rolename="manager"/>
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="tomcat" password="tomcat" roles="tomcat,admin-gui,admin,manager,manager-gui,manager-script"/>

./apache-tomcat-8.5.91/webapps/manager/META-INF/context.xml


Jenkins 安装 插件

Deploy to container Plugin: 此插件允许后面设置 pipeline Syntax > Steps > Sample Step 包含 deploy

Git Pipeline for Blue Ocean

Manage Jenkins > Tools
配置 maven

Maven Name: maven393
MAVEN_HOME: /home/apache-maven-3.9.3

新建 New Item zero-pipeline and Pipeline

Pipeline > Definition > Pipeline script

pipeline {
    agent any

    stages {
        stage('Pull') {
            steps {
                echo 'Pull action'
            }
        }
        stage('Build') {
            steps {
                echo 'Build action'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploy action'
            }
        }
    }
}

zero-pipeline Build 后 Console Output

Started by user deployer
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/zero-pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Pull)
[Pipeline] echo
Pull action
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] echo
Build action
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] echo
Deploy action
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

发布github 工程到指定服务器上

生成一个临时spring-boot 工程 coupon-project ,将其推送到github上
生成 github access token

settings > Developer settings > Personal access token > Tokens (classic)
点击 New personal access token (classic)
- 全选 repo:
- 其它选项依据自身情况判定
点击 Generate token 生成结果 ghp_RoF1J57KoY**************cR6o

新建 New Item third-pipeline-x and Pipeline

Pipeline > Definition > Pipeline script

pipeline {
    agent any
    tools {
        maven 'maven393'
    }
    stages {
        stage('Pull') {
            steps {
                echo 'Pull...........................'
                // generate this automatically in pipeline syntax
                // normally http://${jenkins’IP}:${jenkins's PORT}/job/zero-pipeline/pipeline-syntax/
                checkout scmGit(branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: 'docker jenkins', url: 'https://github.com/fernado/coupon-project.git']])
            }
        }
        stage('Build') {
            steps {
                echo 'Build...........................'
                sh "mvn -Dmaven.test.skip=true clean package"
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploy...........................'
                echo 'pwd()............' + pwd()
                // generate this automatically in pipeline syntax
                // normally http://${jenkins’IP}:${jenkins's PORT}/job/zero-pipeline/pipeline-syntax/
                deploy adapters: [tomcat8(credentialsId: 'tomcat', path: '', url: 'http://192.168.56.51:8682')], contextPath: null, war: '**/*.war'
            }
        }
    }
    // post {
    //     // If Maven was able to run the tests, even if some of the test
    //     // failed, record the test results and archive the jar file.
    //     success {
    //         junit '**/target/surefire-reports/TEST-*.xml'
    //         archiveArtifacts 'target/*.jar'
    //     }
    // }
}

上面的steps checkout*** 是如何生成的?别急,看一下 pipeline-syntax 自动生成
http://192.168.56.51:8080/job/zero-pipeline/pipeline-syntax/
Jenkins (二)_第1张图片
Jenkins (二)_第2张图片

Jenkins (二)_第3张图片
Jenkins (二)_第4张图片
生成的结果放入 pipeline script 的 Stage(‘Pull’)中

Stage(‘Deploy’) 中的脚本用同样类似的方式生成。

编译,发布

third-pipeline-x Build 后 Console Output

[INFO] Packaging webapp
[INFO] Assembling webapp [coupon-service] in [/var/jenkins_home/workspace/third-pipeline-x/coupon-service/target/coupon]
[INFO] Processing war project
[INFO] Webapp assembled in [99 msecs]
[INFO] Building war: /var/jenkins_home/workspace/third-pipeline-x/coupon-service/target/coupon.war
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for coupon-project 0.0.1-SNAPSHOT:
[INFO] 
[INFO] coupon-project ..................................... SUCCESS [  0.197 s]
[INFO] common-redis ....................................... SUCCESS [  3.917 s]
[INFO] coupon-service ..................................... SUCCESS [  3.048 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.260 s
[INFO] Finished at: 2023-07-18T22:22:46+08:00
[INFO] ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
Deploy...........................
[Pipeline] pwd
[Pipeline] echo
pwd()............/var/jenkins_home/workspace/third-pipeline-x
[Pipeline] deploy
[DeployPublisher][INFO] Attempting to deploy 1 war file(s)
[DeployPublisher][INFO] Deploying /var/jenkins_home/workspace/third-pipeline-x/coupon-service/target/coupon.war to container Tomcat 8.x Remote with context null
  Redeploying [/var/jenkins_home/workspace/third-pipeline-x/coupon-service/target/coupon.war]
  Undeploying [/var/jenkins_home/workspace/third-pipeline-x/coupon-service/target/coupon.war]
  Deploying [/var/jenkins_home/workspace/third-pipeline-x/coupon-service/target/coupon.war]
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

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