Jenkins

將 Jenkins 腳本轉換為聲明式管道

  • January 4, 2021

我有一個腳本化的管道,我需要從儲存庫中籤出,而不是在本地簽出。但是,當我將它添加到 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

你有沒有得到這個工作?在您嘗試轉換它之後,如果沒有看到 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: ...。我認為這可能很重要。

引用自:https://serverfault.com/questions/929214