CD在早些團隊裡面,如果沒有使用自動化的時候,開發給包給到運維,運維将包拷貝到伺服器上面之後,通過一些腳本将包啟動。
CD
之前CI将版本檔案上傳到了gitlab,現在要将這個版本檔案下載下傳下來,因為這裡面存儲着要釋出的包的名稱,是以CD流水線有兩個步驟,一個是下載下傳制品,另外一個步驟就是釋出制品。
要從Gitlab裡面去下載下傳這個版本檔案
添加選項參數:針對于不同的環境,選擇要釋出的環境
釋出的主機
def projectId = 17
def filePath = "myapp/release-${params.releaseVersion}.yaml"
def branchName = "main"
pipeline {
agent any
stages {
stage('Hello') {
steps {
script{
response = GetRepoFile(projectId,"myapp%2frelease-1.1.1.yaml",branchName)
println(response)
}
}
}
}
}
// 封裝HTTP
def HttpReq(reqType, reqUrl,reqBody ){
def gitServer = "http://139.198.166.235:81/api/v4"
withCredentials([string(credentialsId: 'ecbcd399-da69-4802-8760-87a1c1ff58a1', variable: 'GITLABTOKEN')]) {
response = httpRequest acceptType: 'APPLICATION_JSON_UTF8',
consoleLogResponseBody: true,
contentType: 'APPLICATION_JSON_UTF8',
customHeaders: [[maskValue: false, name: 'PRIVATE-TOKEN', value: "${GITLABTOKEN}"]],
httpMode: "${reqType}",
url: "${gitServer}/${reqUrl}",
wrapAsMultipart: false,
requestBody: "${reqBody}"
}
return response
}
//擷取檔案内容
def GetRepoFile(projectId,filePath, branchName ){
//GET /projects/:id/repository/files/:file_path/raw
apiUrl = "/projects/${projectId}/repository/files/${filePath}/raw?ref=${branchName}"
response = HttpReq('GET', apiUrl, "")
return response.content
}
"myapp%2frelease-1.1.1.yaml" 這裡有個轉義符代表/,如果定義為變量就這樣寫:def filePath = "myapp%2frelease-1.1.1.yaml" 或者def filePath = "myapp%2frelease-${params.releaseVersion}.yaml"
可以看到上面擷取到了制品資訊,現在就是去下載下傳制品了
def projectId = 17
def filePath = "myapp%2frelease-${params.releaseVersion}.yaml"
def branchName = "main"
pipeline {
agent {
label 'build'
}
stages {
stage('GetArtifactUrl') {
steps {
script{
response = GetRepoFile(projectId,filePath,branchName)
//println(response)
yamlData = readYaml text: """${response}"""
env.artifactUrl = yamlData.artifact
println(env.artifactUrl)
}
}
}
stage('DownloadArtifact') {
steps {
script{
withCredentials([usernamePassword(credentialsId: 'ff93f4bf-9d8c-4fc2-bccd-3e614f10d643',
passwordVariable: 'passwd',
usernameVariable: 'user')]) {
sh """
wget --http-user=${user} --http-passwd=${passwd} ${env.artifactUrl}
"""
}
}
}
}
}
}
// 封裝HTTP
def HttpReq(reqType, reqUrl,reqBody ){
def gitServer = "http://139.198.166.235:81/api/v4"
withCredentials([string(credentialsId: 'ecbcd399-da69-4802-8760-87a1c1ff58a1', variable: 'GITLABTOKEN')]) {
response = httpRequest acceptType: 'APPLICATION_JSON_UTF8',
consoleLogResponseBody: true,
contentType: 'APPLICATION_JSON_UTF8',
customHeaders: [[maskValue: false, name: 'PRIVATE-TOKEN', value: "${GITLABTOKEN}"]],
httpMode: "${reqType}",
url: "${gitServer}/${reqUrl}",
wrapAsMultipart: false,
requestBody: "${reqBody}"
}
return response
}
//擷取檔案内容
def GetRepoFile(projectId,filePath, branchName ){
//GET /projects/:id/repository/files/:file_path/raw
apiUrl = "/projects/${projectId}/repository/files/${filePath}/raw?ref=${branchName}"
response = HttpReq('GET', apiUrl, "")
return response.content
}
[root@jenkins-agent workspace]# cd nexus/
[root@jenkins-agent nexus]# ls
nexus-api-test nexus-chajian-download nexus-chajian-upload nexus-deploy-mvn release-file@tmp test@tmp
nexus-api-test@tmp nexus-chajian-download@tmp nexus-chajian-upload@tmp release-file test
[root@jenkins-agent nexus]# cd test
[root@jenkins-agent test]# ls
acmp-myapp-service-1.1.1.jar
制品拿到之後就是釋出了
前端項目編譯完之後是一個靜态的資源檔案,是一個壓縮包,相對于拷貝到nginx伺服器的站點目錄下面,然後解壓就ok了。後端項目就有一個腳本專門啟動,後端項目需要寫服務啟動腳本。
post {
always{
script {
cleanWs()
}
}
}