简体   繁体   English

将“git 参数”从管道詹金斯作业 A 传递到作业 B

[英]Pass "git parameters" from pipeline jenkins job A to job B

I have a pipeline jenkins job A and a jenkins job B, which might or might not be a pipeline job.我有一个管道詹金斯工作 A 和一个詹金斯工作 B,这可能是也可能不是管道工作。 I have to:我必须:

  • Step 1).第1步)。 Trigger job B from job A and从作业 A 和触发作业 B
  • Step 2).第2步)。 Pass git parameters ( branch name and credentials ) from job A to job B, so that a project is checked out when job B is triggered into the workspace of job B.git 参数(分支名称和凭据)从作业 A 传递到作业 B,以便在作业 B 被触发到作业 B 的工作区时签出项目。

For now, i'm able to trigger job B from job A, I am not able to achieve step 2. Can someone help me with this ?现在,我可以从工作 A 触发工作 B,但我无法完成第 2 步。有人可以帮我吗?

There are a few ways to do it.有几种方法可以做到。

Jenkins file詹金斯文件

pass variables between pipeline if using a file.如果使用文件,则在管道之间传递变量。 You mentioned you are not using pipeline so I assume you are not working with JenkinsFile您提到您没有使用管道,所以我假设您没有使用 JenkinsFile

pipeline {
    // Define the desired paramaters
    parameters {
        string(name: 'var1', defaultValue: '')
        }

        // Define global variable
        stage("define global var") {
             steps {
               env.var1 = value1
              }
        }
     
        stage("test variables") {
            steps {
              echo "${env.var1}"
            }
        }
  }

Write the variables to file将变量写入文件

Write the variables to file and then read it from the disk.将变量写入文件,然后从磁盘读取。

Adding a set variable step添加设置变量步骤


// First set the variables

script
{
    env.var1_name = "var1_value"
    .....
    env.varX_name = "varX1_value"

    // Print the values of the variables
    echo " My vars[DEV:${env.var1_name }] 
}
  • Full script will look like完整的脚本看起来像
stage('define glbal variables') {
    script
    {
        env.var1_name = "var1_value"
        .....
        env.varX_name = "varX1_value"

        // Print the values of the variables
        echo " My vars[DEV:${env.var1_name }] 
    }
}

// Define Job which need teh value of the variable
stage('Job A') {
    echo "NEXT JOB DEV version = ${env.var1_name }"
}

stage('Job B') {
    echo "NEXT JOB DEV version = ${env.var1_name }"
}

Define a global variable定义一个全局变量

// Define a ---groovy--- global named [var1] which can be shared between steps
var1 = 'value1'

...

pipeline {
  stages {
    stage('A') {
      steps {
        echo "${var1}" 
        }
      }
   }
}

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

相关问题 如何在 git push 上将自定义参数传递给 jenkins 作业(多分支管道)? - How to pass custom parameters to jenkins job (multibranch pipeline) on git push? Jenkins:将 git commit hash 传递给下游作业 - Jenkins: pass git commit hash to a downstream job Jenkins:如何在参数化作业中传递Git凭证? - Jenkins: how to pass Git credentials in parameterized job? 在詹金斯管道作业中使用git存档而不是具有凭据的克隆 - Use git archive instead of clone with credentials in jenkins pipeline job 使用 Jenkins 管道将多个 git 存储库检出到同一个作业中 - Using a Jenkins pipeline to checkout multiple git repos into same job Jenkins工作中的动态GIT存储库 - Dynamic GIT repositories from Jenkins job 如何从詹金斯工作推到Git分支? - How to push to Git branch from Jenkins job? 如何在下游作业 B 中使用 Jenkins JOB A 的 git 分支名称作为参数来检出 git 分支? - How to use git branch name of Jenkins JOB A in lower stream job B as a parameter to checkout the git branch? GitVersion在Jenkins Multibranch Pipeline工作中 - GitVersion in a Jenkins Multibranch Pipeline job 使用Jenkins Pipeline作业将Jenkins作业构建状态发布到Gitlab提交 - Publish Jenkins Job build status to Gitlab commit with Jenkins Pipeline job
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM