将 Jenkins 脚本转换为声明式管道

将 Jenkins 脚本转换为声明式管道

我有一个脚本管道,需要从仓库中签出,而不是本地签出。但是,当我将其添加到 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 后,如果不查看它的其余部分,很难判断。但你粘贴的错误输出似乎除了声明性语法问题之外还有其他问题。我可以提供基于脚本的简化版本,如下所示:

#!/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

您不需要转换为声明式管道;只要坚持使用脚本化管道(如果它适合您)。一般来说,将脚本化管道转换为声明式管道比反过来要困难得多。如果您有一个可以运行的脚本化管道,请不要对其进行任何更改,只需将可以运行的管道脚本放入 Git 存储库中的 Jenkinsfile 中,它就可以正常工作。

相关内容