Git Jenkinsfile声明性管道-捕获所有签出scm输出
我正在写一个Jenkins文件,它的开头是这样的: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相关信息 这一步当然运行
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从哪里签出,不是吗?似乎您可以在文件的开头设置一个环境变量,然后在签出步骤中以及需要它的地方使用它。