Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git Jenkinsfile声明性管道-捕获所有签出scm输出_Git_Jenkins_Jenkins Pipeline - Fatal编程技术网

Git Jenkinsfile声明性管道-捕获所有签出scm输出

Git Jenkinsfile声明性管道-捕获所有签出scm输出,git,jenkins,jenkins-pipeline,Git,Jenkins,Jenkins Pipeline,我正在写一个Jenkins文件,它的开头是这样的: pipeline { agent { label 'ec2-slave' } stages { stage('Checkout') { steps { checkout scm } } ... 我想捕获checkout scm调用的输出,因为我需要在后续步骤中使用git相关信息 这一步当然运行

我正在写一个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

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

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

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

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

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

我最后做的只是在我需要这些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()
            }
        }  

我最后做的只是在我需要这些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()
            }
        }  

任意管道脚本在声明性管道中工作

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

任意管道脚本在声明性管道中工作

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

为了它的价值,我将这个Jenkins文件从自由式语法转换而来。例如,我可以通过保存对变量的
sh
调用来“引用”前面的步骤。我不知道如何使用
签出scm
完成同样的事情……我确信这是一个简单的解决方案,但我仍然在学习Groovy和JenkinsFiles的很多知识。我还在
脚本{}
块中尝试了这一点,它产生了奇怪的输出:
签出scm
echo“${scm.dump()”
这会生成一个带有一堆
null
值的映射,这些值对我没有多大帮助。为了它的价值,我将这个Jenkins文件从自由式语法转换为自由式语法。例如,我可以通过保存对变量的
sh
调用来“引用”前面的步骤。我不知道如何使用
签出scm
完成同样的事情……我确信这是一个简单的解决方案,但我仍然在学习Groovy和JenkinsFiles的很多知识。我还在
脚本{}
块中尝试了这一点,它产生了奇怪的输出:
签出scm
echo“${scm.dump()”
这将生成一个包含大量
null
值的映射,这对我来说没有多大用处。这可能是我唯一的选择,因为我不得不在Jenkins文件的其他部分使用类似的脚本块,尽管我希望在本例中有其他方法可以这样做。这可能是我唯一的选择,由于我不得不在Jenkins文件的其他部分使用类似的脚本块,尽管我希望在本例中有其他方法可以这样做。对于URL,您必须告诉Jenkins从哪里签出,不是吗?似乎您可以在Jenkins文件的开头设置一个环境变量,然后在签出步骤中使用该变量,以及您需要它的地方。对于URL,您必须告诉Jenkins从哪里签出,不是吗?似乎您可以在文件的开头设置一个环境变量,然后在签出步骤中以及需要它的地方使用它。