1. 测试环境情况:
Docker主机 10.24.101.99 JFrog Artifactory 主机 10.24.101.99:8081 (admin password) jenkinx 10.24.101.99:8080 github原始地址:https://github.com/jinriyang/swampup github fock后地址:https://github.com/jinanxiaolaohu/swampup
2. 调试通了之后简单的groovy脚本
因为test脚本有异常 删除test stage 部分的脚本
后面有一部分无意义的脚本 但是没来得及 仔细清理.
#!/usr/bin/env groovy node ('master') { //Clone example project from GitHub repository git url: 'https://github.com/jinanxiaolaohu/swampup.git', branch: 'master' def rtServer = Artifactory.server SERVER_ID def buildInfo = Artifactory.newBuildInfo() def tagName buildInfo.env.capture = true //Fetch all depensencies from Artifactory stage('Dependencies') { dir('automation/docker-app') { try { println "Gather Java and Tomcat" def downloadSpec = """{ "files": [ { "pattern": "zhaobsh_generic/jdk-8-linux-x64.tar.gz", "target": "jdk/jdk-8-linux-x64.tar.gz", "flat":"true" }, { "pattern": "zhaobsh_generic/apache-tomcat-8.tar.gz", "target": "tomcat/apache-tomcat-8.tar.gz", "flat":"true" } ] }""" rtServer.download (downloadSpec, buildInfo) if (fileExists('jdk/jdk-8-linux-x64.tar.gz') && fileExists('tomcat/apache-tomcat-8.tar.gz')) { println "Downloaded dependencies" } else { println "Missing Dependencies either jdk or tomcat - see listing below:" sh 'ls -d */*' throw new FileNotFoundException("Missing Dependencies") } } catch (Exception e) { println "Caught exception during resolution. Message ${e.message}" throw e } } } //Build docker image named "docker-framework" with Java 8 and Tomcat stage('Build') { dir ('automation/docker-app') { withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: CREDENTIALS, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { def rtDocker = Artifactory.docker server: rtServer tagName = "${ARTDOCKER_REGISTRY}/docker-app:${env.BUILD_NUMBER}" def gradleLatestPath = getLatestGradleWar().trim() def gradleWarDownload = """{ "files": [ { "pattern": "zhaobsh_generic/*.war", "target": "war/webservice.war", "flat": "true" } ] }""" sh 'rm -f war/*' rtServer.download(gradleWarDownload) docker.build(tagName) echo tagName sleep 2 rtDocker.push(tagName,REPO, buildInfo) sleep 2 rtServer.publishBuildInfo buildInfo } } } //Promote image from local staging repositoy to production repository stage ('Promote') { dir ('automation/docker-app') { def promotionConfig = [ 'buildName' : env.JOB_NAME, 'buildNumber' : env.BUILD_NUMBER, 'targetRepo' : PROMOTE_REPO, 'comment' : 'Framework test with latest version of application', 'sourceRepo' : SOURCE_REPO, 'status' : 'Released', 'includeDependencies': false, 'copy' : true ] echo promotionConfig as String rtServer.promote promotionConfig reTagLatest (SOURCE_REPO) reTagLatest (PROMOTE_REPO) } } } def getLatestGradleWar () { def response = '' def gradleLatestWarSrc = """items.find( { "repo":{"$eq":"zhaobsh_generic"}, "name":{"$match":"webservice-*.war"} } ).sort({"$desc" : ["created"]}).limit(1)""" withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: CREDENTIALS, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { def curlString = "curl -s -u " + env.USERNAME + ":" + env.PASSWORD + " " + SERVER_URL def gradleLatestStr = curlString + "/api/search/aql -X POST -H 'Content-Type: text/plain' -d '" + gradleLatestWarSrc + "' | python -c 'import sys, json; print json.load(sys.stdin)["results"][0]["path"]'" println "Curl String is " + gradleLatestStr response = sh (script: gradleLatestStr, returnStdout: true) } println "Curl response: " + response return response } def updateDockerFile () { def BUILD_NUMBER = env.BUILD_NUMBER sh 'sed -i "s/docker-app:latest/docker-app:$BUILD_NUMBER/" Dockerfile' } def reTagLatest (targetRepo) { def BUILD_NUMBER = env.BUILD_NUMBER sh 'sed -E "s/@/$BUILD_NUMBER/" retag.json > retag_out.json' switch (targetRepo) { case PROMOTE_REPO : sh 'sed -E "s/TARGETREPO/${PROMOTE_REPO}/" retag_out.json > retaga_out.json' break case SOURCE_REPO : sh 'sed -E "s/TARGETREPO/${SOURCE_REPO}/" retag_out.json > retaga_out.json' break } sh 'cat retaga_out.json' withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: CREDENTIALS, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { def curlString = "curl -u " + env.USERNAME + ":" + env.PASSWORD + " " + SERVER_URL def regTagStr = curlString + "/api/docker/$targetRepo/v2/promote -X POST -H 'Content-Type: application/json' -T retaga_out.json" println "Curl String is " + regTagStr sh regTagStr } } //test docker image by runnning container def testFramework (tag) { def result = true docker.image(tag).withRun('-p 8181:8181') {c -> sleep 10 def stdout = sh(script: 'curl "http://localhost:8181/swampup/"', returnStdout: true) if (stdout.contains("Welcome Docker Lifecycle Training")) { println "*** Passed Test: " + stdout } else { println "*** Failed Test: " + stdout result = false } } sh "docker rmi ${tag}" return result } def updateProperty (property) { withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: CREDENTIALS, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) { def curlString = "curl -u " + env.USERNAME + ":" + env.PASSWORD + " " + "-X PUT " + SERVER_URL def updatePropStr = curlString + "/api/storage/${SOURCE_REPO}/docker-framework/${env.BUILD_NUMBER}?properties=${property}" println "Curl String is " + updatePropStr sh updatePropStr } }
3. pipeline的参数情况
4. linux主机的修改.
关闭防火墙 关闭selinux 关闭swap 修改/etc/docker/daemon.json 增加jfrog仓库为insecure-registries 重启docker
下载jdk的tar包 解压缩并且 修改 /etc/profile 配置文件 增加JAVA_HOME 环境变量.
安装jenkins
安装jfrog artifactory
docker下安装sonar
5. jfrog artifactory的处理
新增仓库 主要是第三步中的REPO的名字必须得有. 仓库类型 docker
新增generic 仓库 上传文件, 与groovy脚本中的下载文件部分相匹配.
主要需要的文件如图示
仓库名/文件名
继续一个仓库名 文件名
6. jenkins 需要进行的处理.
根据配置文件中的 server_id 增加 artifactory的 server_id 并且在步骤三中 定义上.
在 系统管理-系统设置中 进行设置, 添加 serverid 如图示
下载部分可选插件
Artifactory部分的 增加相当的额内容.
即可
7. build with parameters 即可.