为什么我的Jenkinsfile会出现workflowscript错误?

wtlkbnrh  于 2023-04-29  发布在  Jenkins
关注(0)|答案(1)|浏览(599)

我对Jenkins还是个新手。我正在尝试构建一个docker镜像,测试它并推送到注册表。
测试阶段涉及确保http端点能够返回状态代码200我的Jenkinsfile代码有问题,会抛出错误。我安装了jenkins pipeline linter来提供帮助,但它会在我清除一个错误时带来新的错误。我感谢一些帮助我的jenkinsfile代码来纠正我的错误。谢谢你。
请参见下面的代码:

pipeline {
    agent any
    options {
        buildDiscarder(logRotator(numToKeepStr: '5'))
    }
    environment {
        DOCKERHUB_CREDENTIALS = credentials('dockerhub')
    }

    stages {
        stage('Build image') {
            steps {
                script {
                    sh 'docker build -t (stlng/freeze-$env.BRANCH_NAME:$env.BUILD_NUMBER) .'
                }
            }
        }

        stage('Test image') {
            agent
            stages {
                steps {
                stage('Launch app') {
                    steps {
                        sh 'docker compose -f freeze.yml up -d'
                    }
                }

                stage('testing endpoint') {
                    steps {
                        httpRequest url:'http://localhost:8000',
                        validResponseCodes:'200'
                    }
                }

                stage('shutting down app') {
                    steps {
                        sh 'docker compose -f freeze.yml down'
                    }
                }
                }
            }
        }
        stage('dockerHub login') {
            steps {
                sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
            }
        }

        stage('Push image to registry') {
            steps {
                sh 'docker push (stlng/freeze-$env.BRANCH_NAME:$env.BUILD_NUMBER)'
            }
        }

        stage('dockerhub logout') {
            steps {
                sh 'docker logout'
            }
        }

 /*** workspace clean up*/
        post {
            always  {
                sh 'docker system prune'
                cleanWs()
            }
        }
        }
    }

下面是一些最后的错误。一旦我清理完一套新的就会出现。

Errors encountered validating Jenkinsfile:
WorkflowScript: 19: Not a valid stage section definition: "agent". Some extra configuration is required. @ line 19, column 9.
           stage('Test image') {
           ^

WorkflowScript: 22: Expected a stage @ line 22, column 17.
                   steps {
                   ^

WorkflowScript: 63: Expected a stage @ line 63, column 9.
           post {
           ^

WorkflowScript: 21: No stages specified @ line 21, column 13.
               stages {
               ^

我最初的Jenkinsfile没有测试阶段和清理阶段(请参见下文),它运行得很好。当我试图介绍这两个阶段时,错误就开始了。

pipeline {
  agent any
  options {
    buildDiscarder(logRotator(numToKeepStr: '5'))
  }
  environment {
    DOCKERHUB_CREDENTIALS = credentials('dockerhub')
  }
  stages {
    stage('Build image for app') {
      steps {
        sh 'docker build -t stlng/freeze-master:0.0.1 .'
      }
    }
    stage('Login to docker hub') {
      steps {
        sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
      }
    }
    stage('Push docker image to docker hub registry') {
      steps {
        sh 'docker push stlng/freeze-master:0.0.1'
      }
    }
  }
  post {
    always {
      sh 'docker logout'
    }
  }
  
}
envsm3lx

envsm3lx1#

看起来您有嵌套阶段的步骤,这是无效的。如果需要,可以在阶段中嵌套阶段。然而,步骤不能包含阶段。

stage('Test image') {
    agent
        stages {
            steps { // <----This is the problem
                stage('Launch app') {

相关问题