1. 创建jenkins的流水线
2. 创建artifactory的镜像仓库
3. 创建generic的仓库上传需要的 war包还有tar包.
4. 修改pipeline的 脚本
主要修改的地方. 修改war包的原始路径, 将路径修改为步骤3中定义的路径.
修改后未成形的脚本
#!/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-framework') {
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-framework') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: CREDENTIALS, usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
def rtDocker = Artifactory.docker server: rtServer
tagName = "${ARTDOCKER_REGISTRY}/docker-framework:${env.BUILD_NUMBER}"
docker.build(tagName)
echo tagName
sleep
rtDocker.push(tagName,REPO, buildInfo)
sleep
rtServer.publishBuildInfo buildInfo
}
}
}
//Test docker image
stage('Test') {
dir('automation/docker-framework/framework-test') { def gradleLatestPath = getLatestGradleWar().trim()
def gradleWarDownload = """{
"files": [
{
"pattern": "zhaobsh_generic/webservice-1.1.2.war",
"target": "war/webservice.war",
"flat": "true"
}
]
}"""
echo "Test"
sh 'rm -f war/*'
sh "mkdir -p war"
rtServer.download(gradleWarDownload)
updateDockerFile()
def tagDockerFramework = "${ARTDOCKER_REGISTRY}/docker-framework-test:${env.BUILD_NUMBER}"
docker.build(tagDockerFramework)
if (testFramework(tagDockerFramework)) {
println "Setting property and promotion"
updateProperty ("functional-test=pass")
sh "docker rmi ${tagName}"
} else {
updateProperty ("functional-test=fail; failed-test=page-not-loaded")
currentBuild.result = 'UNSTABLE'
sh "docker rmi ${tagName}"
return
}
}
}
//Scan build's Artifacts in Xray
stage('Xray Scan') {
if (XRAY_SCAN == "YES") {
def xrayConfig = [
'buildName' : env.JOB_NAME,
'buildNumber' : env.BUILD_NUMBER,
'failBuild' : false
]
def xrayResults = rtServer.xrayScan xrayConfig
echo xrayResults as String
} else {
println "No Xray scan performed. To enable set XRAY_SCAN = YES"
}
sleep
} //Promote image from local staging repositoy to production repository
stage ('Promote') {
dir ('automation/docker-framework') {
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
]
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()"""
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-framework:latest/docker-framework:$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
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
}
}
5. 修改参数. 增加相关的 流水线中的参数.
6. 修改docker daemon.json的内容 修改json的文件形式
{
"registry-mirrors": ["http://a7d80d83.m.daocloud.io"],
"insecure-registries":["127.0.0.1:8081","10.24.101.99:8081","CentOS75:8081"]
}
7. fork 例子中的github地址,并且进行修改
https://github.com/jinriyang/swampup
fock 到自己的
修改dockerfile的内容
https://github.com/jinanxiaolaohu/swampup/blob/master/automation/docker-framework/Dockerfile 修改后的内容
FROM ubuntu:latest MAINTAINER zhaobsh RUN /bin/bash -c cd ~; cd /home ; mkdir -p exec
ADD jdk/jdk--linux-x64.tar.gz /home/exec
RUN /bin/bash -c cd /home/exec; mv /home/exec/jdk* /home/exec/jdk8
ENV JAVA_HOME=/home/exec/jdk8
RUN sed "/securerandom.source=/{s/file:\/dev\/random/file:\/dev\/urandom/}" /home/exec/jdk8/jre/lib/security/java.security -i ADD tomcat/apache-tomcat-.tar.gz /home/exec
RUN /bin/bash -c cd /home/exec; mv /home/exec/apache-tomcat-.* /home/exec/tomcat
ADD tomcat/server.xml /home/exec/tomcat/conf
ENV CATALINA_HOME=/home/exec/tomcat
ENV TEST_ENV= CMD /bin/bash -c cd /home/exec; /home/exec/tomcat/bin/catalina.sh run
8. 进行build with parameters 进行处理.
根据错误信息 进行修改 修改过的主要地方有:
1. 镜像仓库要准确.
2. github中的代码需要修改 commit
3. 修改groovy中的脚本代码分支. 一般使用master即可.
4. stage 是 Test 时 可以将 下载 testpass的参数去掉 因为没有进行 单元测试.
5. /etc/docker/daemon.json 一定要配置对, 写镜像的名称的时候 不能加http 不加上如果也没有修改 增加insecure-registries 的话 就会出现http和https的错乱了.
6. 但是发现test的部分 总是出问题. 提示错误如图示 暂时未解决.
这个是个bug jenkins 暂未解决..暂时不搞了