【问题标题】:Jenkins "unable to resolve class Declarative" error when building Python scriptJenkins在构建Python脚本时“无法解析类声明”错误
【发布时间】:2021-12-23 20:01:06
【问题描述】:

我开始学习使用 Jenkins,并希望让它自动运行我的 Python 脚本。我按照他们的教程创建了一个名为 Pipeline Test 的新项目。

我还添加了我想要测试的 Python 脚本的 GitHub 存储库 (https://github.com/mateasmario/spam-bot)。

如您所见,我在该存储库中创建了一个Jenkinsfile。因为我的脚本名为spam-bot.py,所以我希望我的 Jenkinsfile 每次在 Jenkins 中单击“立即构建”时运行该脚本。这是我的 Jenkinsfile 的内容:

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent { docker { image 'python:3.10.1-alpine' } }
    stages {
        stage('build') {
            steps {
                sh 'python spam-bot.py'
            }
        }
    }
}

问题是,每当我点击“立即构建”时,我的构建都会失败,并且控制台会输出以下错误:

Started by user Mario Mateas
Obtained Jenkinsfile from git https://github.com/mateasmario/spam-bot.git
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 1: unable to resolve class Declarative 
 @ line 1, column 26.
   Jenkinsfile (Declarative Pipeline)
                            ^

1 error

    at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
    at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:958)
    at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:605)
    at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:554)
    at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
    at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
    at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
    at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
    at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:571)
    at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:523)
    at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:334)
    at hudson.model.ResourceController.execute(ResourceController.java:99)
    at hudson.model.Executor.run(Executor.java:432)
Finished: FAILURE

我在互联网上查找了这个错误,但没有找到任何有用的信息,这就是我决定在这里询问的原因。 我也没有配置任何 Docker 容器。我需要配置一个吗?我查看了 Jenkins 的 Docker 文档,但没有看到任何有关将 Python 映像(如 Jenkinsfile 开头提到的那个)添加到容器的有用信息。

【问题讨论】:

  • 这与 Git 无关,但看起来像是 Jenkins 或 Groovy 的错误构建。

标签: docker jenkins continuous-deployment


【解决方案1】:

您的Jenkinsfile 在第一行包含无效语法,这就是引发错误的原因。假设您打算将第一行作为注释,您可以将管道代码修改为:

// Jenkinsfile (Declarative Pipeline)
pipeline {
  ...
}

您的管道代码将具有有效的语法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    相关资源
    最近更新 更多