在哪里放置try / catch,使其可以按预期工作,特别是在存在并行分支的情况下工作?(此外,还有蓝海插件)
在有关Jenkinsfile的官方文档中,该主题没有任何明确内容,但是确实存在示例:
示例1:尝试在阶段块内
Jenkinsfile (Scripted Pipeline)
node {
stage('Example') { //It's inside the stage block
try {
sh 'exit 1'
}
catch (exc) {
echo 'Something failed, I should sound the klaxons!'
throw
}
}
}
Run Code Online (Sandbox Code Playgroud)
示例2:尝试在节点块内部
Jenkinsfile (Scripted Pipeline)
stage('Build') {
/* .. snip .. */
}
stage('Test') {
parallel linux: {
node('linux') {
checkout scm
try {
unstash 'app'
sh 'make check'
}
finally {
junit '**/target/*.xml'
}
}
},
windows: {
node('windows') {
/* .. …Run Code Online (Sandbox Code Playgroud) pipeline {
agent any
stages {
stage("foo") {
steps {
script {
env.RELEASE_SCOPE = input message: 'User input required', ok: 'Release!',
parameters: [choice(name: 'RELEASE_SCOPE', choices: 'patch\nminor\nmajor',
description: 'What is the release scope?')]
}
echo "${env.RELEASE_SCOPE}"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,选择是硬编码的(补丁\nminor\nmajor)——我的要求是在下拉列表中动态给出选择值。我通过调用 api - Artifacts list (.zip) 文件名从 artifactory 中获取值在上面的示例中,它在我们进行构建时请求输入,但我想做一个“带参数的构建”
请对此提出建议/帮助。
当我的 Jenkins 实例启动时,我会自动将 GitHub 组织添加到它,但是当它添加每个作业时,它会执行它。这会导致执行许多作业,从而长时间(数小时)堵塞构建队列。有没有办法通过 Jenkinsfile 导入每个项目,但不自动运行构建?
当我启动 Jenkins 实例时,我运行以下代码:
import jenkins.branch.OrganizationFolder
import org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait
import org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator
import org.jenkinsci.plugins.github_branch_source.OriginPullRequestDiscoveryTrait
import org.jenkinsci.plugins.github_branch_source.GitHubConfiguration
import static jenkins.model.Jenkins.instance as jenkins
static Boolean organisationFolderExists(String name) {
def folders = jenkins.items
if (folders.isEmpty()) {
return false
}
def organisation = folders.find { folder -> folder.name == name }
return organisation
}
String lookupApiUri(def apiEndpoint) {
GitHubConfiguration gitHubConfig = GitHubConfiguration.get()
gitHubConfig.getEndpoints().findResult { it.name == apiEndpoint ? it.apiUri : null }
}
void createOrganisationFolder(def config) {
def folder = …Run Code Online (Sandbox Code Playgroud) $ groovy --version
Groovy Version: 2.4.15 JVM: 1.8.0_171 Vendor: Oracle Corporation OS: Mac OS X
Run Code Online (Sandbox Code Playgroud)
我想在Groovy中执行一些AWS CLI命令,当然最终在Jenkins文件中运行Jenkins.
但是对于原型设计,我想在我的Mac上编码并将其作为一个普通的Groovy脚本执行.所以我有这个,例如.
#!/usr/bin/env groovy
def getEBSVolumes(awsRegion) {
def regions
if (awsRegion == "all") {
regions = sh(returnStdout: true, script: """#!/usr/bin/env bash
aws ec2 describe-regions --output text|awk '{print \$3}'
"""
)
}
else {
regions = awsRegion
}
echo "Regions: regions"
}
getEBSVolumes("all")
Run Code Online (Sandbox Code Playgroud)
当我执行它时,我得到了
$ ./x.groovy
Caught: groovy.lang.MissingMethodException: No signature of method: x.sh() is applicable for argument types: (java.util.LinkedHashMap) values: [[returnStdout:true, script:#!/usr/bin/env bash
aws ec2 …Run Code Online (Sandbox Code Playgroud) 我正在尝试获取管理员用户列表的列表以及 Jenkins 中的用户及其权限级别。
任何人都可以帮我提供任何可用的脚本吗?
java.lang.NoSuchMethodError:在步骤[归档,bat,build,catchError,checkout,deleteDir,dir,dockerFingerprintFrom,dockerFingerprintRun,echo,emailext,emailextrecipients,envVarsForTool,错误,fileExists,getContext,git中没有找到这样的DSL方法'publishOverSsh',输入,isUnix,libraryResource,负载,邮件,里程碑,节点,并行,属性,pwd,readFile,readTrusted,resolveScm,重试,脚本,sh,sleep,阶段,存储,步骤,svn,超时,时间戳,工具,未归档,取消隐藏,waitUntil,withContext,withCredentials,withDockerContainer,withDockerRegistry,withDockerServer,
考虑下面的例子
node {
stage('Build') {
echo "do buildy things"
}
stage('Deploy') {
hipchatSend(
color: "PURPLE",
message: "Holding for deployment authorization: ${env.JOB_NAME}, job ${env.BUILD_NUMBER}. Authorize or cancel at ${env.BUILD_URL}",
)
input('Push to prod?') //Block here until okayed.
echo "Deployment authorized by ${some.hypothetical.env.var}"
echo "do deploy things"
}
}
Run Code Online (Sandbox Code Playgroud)
响应输入时,单击按钮的用户名将存储在构建日志中。
这个用户名是否可以在我可以在另一个 hipChatSend 中使用的变量中使用?
我正在使用管道 ( Jenkinsfile),我需要更改节点版本。我添加了 Nvm Wrapper Plugin 但我不知道如何正确使用它Jenkinsfile
我应该添加nvm('...') {}内部steps吗?或者它应该在node步骤中的某个顶级位置?目前我什至没有node步骤 - 一切都是使用sh
我有以下内容Jenkinsfile:
#!groovy
def projectPath = "${projectPath}"
def specPath = "${specPath}"
int numberOfRetries = "${NUM_OF_RETRIES}".toInteger()
def failure = true
def retryAmount = 0
def start = System.currentTimeMillis()
def getSpecName() {
specPath.split("/")[-1].split(".")[0]
}
def getProjectPath() {
projectPath.split("/")[-1]
}
def rmDocker() {
def remove = sh script: "docker rm -f cypress_${getSpecName()}", returnStatus: true
}
stage("Cypress Setup") {
node("Cypress") {
rmDocker()
}
}
stage("Cypress Run") {
node("Cypress") {
currentBuild.setDisplayName("${projectPath} - ${getSpecName()}")
while (failure && retryAmount < numberOfRetries) {
sh "docker pull dockreg.bluestembrands.com/cypresswithtests:latest" …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Jenkins 中打印一个变量。但是我收到一条错误消息,说“替换不当”。我正在使用 Jenkinsfile 来实现这一点。这就是我正在做的。
static def printbn() {
sh '''
#!/usr/bin/env bash
echo \"${env.BUILD_NUMBER}\"
'''
}
pipeline {
agent any
stages {
stage('Print Build Number') {
steps {
printbn()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误
/var/lib/jenkins/workspace/groovymethod@tmp/durable-7d9ef0b0/script.sh: line 4: ${steps.env.BUILD_NUMBER}: bad substitution
Run Code Online (Sandbox Code Playgroud)
注意:我使用的是 Jenkins 版本 Jenkins ver. 2.163
jenkins ×9
groovy ×5
automation ×1
github ×1
jenkins-2 ×1
jenkins-api ×1
jenkins-cli ×1
node.js ×1