Jenkinsfile Declarative Pipeline - 捕获所有checkout scm输出

时间:2017-06-12 15:55:59

标签: git jenkins jenkins-pipeline

我正在写一个Jenkins文件,其开头是这样的:

pipeline {
    agent {
        label 'ec2-slave'
    }
    stages {
        stage('Checkout') {
            steps {
                checkout scm
            }
        }
...

我想捕获checkout scm调用的输出,因为我需要在后续步骤中使用与git相关的信息。

这一步当然运行得非常好,产生如下输出:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url xxxxxxxxx # timeout=10
Fetching upstream changes from xxxxxxxxx
 > git --version # timeout=10
using GIT_SSH to set credentials jenkins ssh key
 > git fetch --tags --progress xxxxxxxxx +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/master
Seen 2 remote branches
 > git tag -l # timeout=10
Checking out Revision 10214b72d4c1f2c69444cc79a1af7ecc7f033349 (origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 10214b72d4c1f2c69444cc79a1af7ecc7f033349
 > git rev-list 10214b72d4c1f2c69444cc79a1af7ecc7f033349 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] checkout
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url xxxxxxxxx # timeout=10
Fetching upstream changes from xxxxxxxxx
 > git --version # timeout=10
using GIT_SSH to set credentials jenkins ssh key
 > git fetch --tags --progress xxxxxxxxx +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/master
Seen 2 remote branches
 > git tag -l # timeout=10
Checking out Revision 10214b72d4c1f2c69444cc79a1af7ecc7f033349 (origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 10214b72d4c1f2c69444cc79a1af7ecc7f033349

是否有捕获和/或引用先前管道步骤的预期方法?

3 个答案:

答案 0 :(得分:1)

您可以使用脚本步骤,使用操作系统上安装的git执行checkout并捕获输出:

script {
  GIT_CLONE = sh (
    script: 'git clone xxxxxxxxx',
    returnStdout: true
  ).trim()
  echo "git clone output: ${GIT_CLONE}"
}

答案 1 :(得分:1)

我最终做的只是在我需要这些git变量的阶段添加一个额外的脚本步骤。

       steps {
            script {
                GIT_COMMIT = sh (
                    script: 'git rev-parse HEAD',
                    returnStdout: true
                ).trim()

                GIT_URL = sh (
                    script: 'git config --get remote.origin.url',
                    returnStdout: true
                ).trim()
            }
        }  

答案 2 :(得分:1)

任意管道脚本可在声明管道中使用

pipeline {
    agent any
stages {
    stage ('download') {
        steps {
            script {
                def scmInfo = checkout scm
                println "GIT_COMMIT " + scmInfo.GIT_COMMIT
            }
        }
    }
}