当前位置: 代码迷 >> 综合 >> Jenkins CD VM部署实践 02 根据版本文件下载制品
  详细解决方案

Jenkins CD VM部署实践 02 根据版本文件下载制品

热度:108   发布时间:2023-09-30 12:22:24.0

CD在早些团队里面,如果没有使用自动化的时候,开发给包给到运维,运维将包拷贝到服务器上面之后,通过一些脚本将包启动。

CD

之前CI将版本文件上传到了gitlab,现在要将这个版本文件下载下来,因为这里面存储着要发布的包的名称,所以CD流水线有两个步骤,一个是下载制品,另外一个步骤就是发布制品。

要从Gitlab里面去下载这个版本文件

添加选项参数:针对于不同的环境,选择要发布的环境

Jenkins CD VM部署实践 02 根据版本文件下载制品

发布的主机

Jenkins CD VM部署实践 02 根据版本文件下载制品

Jenkins CD VM部署实践 02 根据版本文件下载制品

def projectId = 17
def filePath = "myapp/release-${params.releaseVersion}.yaml"
def branchName = "main"pipeline {agent anystages {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/rawapiUrl = "/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"

Jenkins CD VM部署实践 02 根据版本文件下载制品 Jenkins CD VM部署实践 02 根据版本文件下载制品

  可以看到上面获取到了制品信息,现在就是去下载制品了

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.artifactprintln(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/rawapiUrl = "/projects/${projectId}/repository/files/${filePath}/raw?ref=${branchName}"response = HttpReq('GET', apiUrl, "")return response.content}

Jenkins CD VM部署实践 02 根据版本文件下载制品

Jenkins CD VM部署实践 02 根据版本文件下载制品

[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了。后端项目就有一个脚本专门启动,后端项目需要写服务启动脚本。

扩展: 如何清除工作目录? 安装Workspace Cleanup插件。在Pipeline 的Post中的always添加CleanWs() 

用来在build开始前或build完成后清理workspace。

Jenkins CD VM部署实践 02 根据版本文件下载制品

      post {always{script {cleanWs()}}}
  相关解决方案