简体   繁体   English

如何从Jenkinsfile加载第二个Jenkins文件,但继续使用原始工作区 - multibranch管道

[英]How to load a second Jenkinsfile from a Jenkinsfile but continue to use original workspace - multibranch pipeline

I have a need to load in an external project from a base project at build time. 我需要在构建时从基础项目加载外部项目。 (I own both code bases.) The external project contains a Jenkinsfile that I need to execute from the base project's Jenkinsfile, along with other scripts that need to be ran. (我拥有两个代码库。)外部项目包含一个Jenkinsfile,我需要从基础项目的Jenkinsfile执行,以及需要运行的其他脚本。

However, loading the external Jenkinsfile after cloning in the project causes a new WORKSPACE to be used (@2,3,4 etc appended at the end). 但是,在项目中克隆后加载外部Jenkins文件会导致使用新的WORKSPACE(最后附加@ 2,3,4等)。 This new workspace does not have the external project's cloned in files from the original workspace. 此新工作区没有将外部项目克隆到原始工作区的文件中。

My hacky solution so far was to reset the WORKSPACE in the external Jenkinsfile to the original workspace. 到目前为止,我的hacky解决方案是将外部Jenkins文件中的WORKSPACE重置为原始工作区。

environment {
    WORKSPACE = path/to/original/workspace
}

However, this will not work for a multibranch job, because those workspaces have a randomly generated set of characters at the end. 但是,这对多分支作业不起作用,因为这些工作区最后会有一组随机生成的字符。

My thought was to pass in the workspace as a different environment variable name and set WORKSPACE in the second job to that. 我的想法是将工作空间作为不同的环境变量名称传递,并在第二个作业中将WORKSPACE设置为该值。 However, withEnv seems to work with regular strings but not variables. 但是,withEnv似乎使用常规字符串而不是变量。

Here is basically what I am doing so far, which does not work: 这基本上是我到目前为止所做的,但这不起作用:

base Jenkinsfile: 基础Jenkinsfile:

node {
 cleanWs()
 checkout scm
 checkout([$class: 'GitSCM', branches: [[name: 'branch-name']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'sub-dir']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'creds', url: 'url']]])
  withEnv (['BASE_WORKSPACE=$WORKSPACE']) {
    load 'sub-dir/Jenkinsfile'
 }
}

cloned in Jenkinsfile: 克隆在Jenkinsfile中:

pipeline {
 agent any
 environment {
    WORKSPACE = env.BASE_WORKSPACE
 }
 stages {
     stage('Stage 1'){
         steps{
             sh '''
             $WORKSPACE/sub-dir/bash-script.sh
             '''
        }
     }
  }
}

I have a few more hacky solutions in mind 我还有一些更多的hacky解决方案

  • Guessing at the base workspace from the external Jenkinsfile (subtract 1 from the end of the workspace name after the @ sign) 从外部Jenkins文件在基础工作区猜测(在@符号后从工作区名称的末尾减去1)

  • Writing the base workspace to a file in the base Jenkinsfile, and reading it in the second Jenkinsfile 将基础工作区写入基本Jenkinsfile中的文件,并在第二个Jenkins文件中读取它

  • Trying different syntax with withEnv 尝试使用withEnv的不同语法

But before I went even more into the weeds wanted to reach out to this awesome community to see if anyone had thoughts on this. 但是在我进入杂草之前,想要接触到这个令人敬畏的社区,看看是否有人对此有所了解。

Thanks! 谢谢!

However, withEnv seems to work with regular strings but not variables. 但是,withEnv似乎使用常规字符串而不是变量。

Use the following syntax instead to have the variable expanded: 使用以下语法来扩展变量:

 withEnv(["BASE_WORKSPACE=${env.WORKSPACE}"]) {
withEnv (['BASE_WORKSPACE=' + WORKSPACE]) {
     load 'sub-dir/Jenkinsfile'
 }

worked for me, while the quotes are bash the rest is still groovy in the Jenkinsfile. 为我工作,而引用是bash,其余的仍然是Jenkins文件中的常规。 Allowing you to concatenate the strings. 允许您连接字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM