我有一個腳本化管道,我需要從存儲庫中簽出,而不是在本地查看。然而,當我將它新增至 git 時,作業立即失敗。這是工作腳本;
node {
INSTANCE_ID = ""
stage('Get Instance Id') {
INSTANCE_ID = sh (
script: 'aws ec2 describe-instances --region=$awsRegion --filters Name=tag:Name,Values=\"$instanceName\" --query \'Reservations[0].Instances[0].InstanceId\'',
returnStdout: true
).trim()
if (INSTANCE_ID == "") {
error 'No instance with the name ' + $instanceName + ' was found in the ' + $awsRegion + ' region.'
}
}
stage('Start EC2 Instance') {
sh ('aws ec2 start-instances --region=$awsRegion --instance-ids ' + INSTANCE_ID)
}
stage('Wait for instance to be running') {
INSTANCE_STATE = sh (
script: 'aws ec2 describe-instances --region=$awsRegion --instance-id ' + INSTANCE_ID + ' --query \'Reservations[0].Instances[0].State.Name\'',
returnStdout: true
).trim()
numberOfStatusChecksPerformed = 0
while (INSTANCE_STATE != '"running"') {
echo INSTANCE_STATE
sleep 20
numberOfStatusChecksPerformed = numberOfStatusChecksPerformed + 1
// Wait 5 minutes
if (numberOfStatusChecksPerformed > 15) {
error 'Instance state was not running, it status is: ' + INSTANCE_STATE
}
INSTANCE_STATE = sh (
script: 'aws ec2 describe-instances --region=$awsRegion --instance-id ' + INSTANCE_ID + ' --query \'Reservations[0].Instances[0].State.Name\'',
returnStdout: true
).trim()
}
}
}
我嘗試透過改變來轉換它;node {
到;
#!/usr/bin/env groovy
def INSTANCE_ID = ""
pipeline {
agent any
stages {
其餘部分保持不變。我收到以下錯誤;
java.io.FileNotFoundException
at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:167)
at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:159)
at jenkins.plugins.git.GitSCMFileSystem$3.invoke(GitSCMFileSystem.java:193)
at org.jenkinsci.plugins.gitclient.AbstractGitAPIImpl.withRepository(AbstractGitAPIImpl.java:29)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.withRepository(CliGitAPIImpl.java:72)
at jenkins.plugins.git.GitSCMFileSystem.invoke(GitSCMFileSystem.java:189)
at jenkins.plugins.git.GitSCMFile.content(GitSCMFile.java:159)
at jenkins.scm.api.SCMFile.contentAsString(SCMFile.java:338)
at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:110)
at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:67)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:303)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE
答案1
你曾經讓這個工作過嗎?在嘗試轉換 Jenkinsfile 後,如果沒有看到 Jenkinsfile 的其餘部分,就很難判斷。但是您貼上的錯誤輸出似乎除了聲明式語法問題之外還發生了其他問題。我可以根據腳本提供的簡化版本可能如下所示:
#!/usr/bin/env groovy
def INSTANCE_ID = ""
pipeline {
agent any
options {
// your options here
}
parameters {
// your parameters here
}
environment {
// KEY = "Value"
}
stages {
stage('Get Instance Id') {
steps {
script {
sh "./some_command.sh"
INSTANCE_ID = sh(returnStdout: true, script: 'aws ec2 describe-instances ... ').trim()
}
}
}
}
}
希望有幫助。
它可能是script {}
該sh(returnStdout: true, script: ...
零件的包裝紙。我認為這可能很重要。
答案2
您不需要轉換為聲明式管道;如果 Scripted 適合您,請堅持使用它。一般來說,將腳本化管道轉換為聲明式管道比相反要困難得多。如果您有一個正在運行的腳本化管道,則根本不要更改它,只需將正在運行的管道腳本放入 Git 存儲庫中的 Jenkinsfile 中,它就應該可以正常工作。