1

スクリプト化された Jenkinsfile 内からビルドが実行された原因を特定する方法を見つけようとしています。その理由は、cron ジョブで実行したい docker コンテナーにスクリプトがあるためです。したがって、cron ジョブがトリガーされたときにコンテナーを実行するだけで、変更をプッシュするときに、それをチェックアウトしたいのです。コードを作成し、コンテナーを再構築し、静的コード分析を実行し、テストを実行します。cron の実行では、そのすべてを行う必要はありません。

どうすれば原因をつかむことができますか?試してみcurrentBuild.getCauses()ましたが、得られます

groovy.lang.MissingMethodException: No signature of method: org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper.getCauses() is applicable for argument types: () values: []

私は試しprintln currentBuild.getRawBuild().getCauses()ましたが、得ました

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild

jenkinsfile でビルドの原因を特定するにはどうすればよいですか?

4

3 に答える 3

2

この Groovy コードは、トリガー プロジェクトの名前を取得します (Groovy サンドボックスで正常に実行されます)。

String getTriggeringProjectName() {
    if (currentBuild.upstreamBuilds) {
        return currentBuild.upstreamBuilds[0].projectName
    } else {
        return ""
    }
}
于 2020-10-13T22:49:55.337 に答える
1

トリガー ビルドのプロジェクト名 ( getUpstreamProject ) とビルド番号 ( getUpstreamBuild ):

currentBuild.rawBuild.getCauses().get(0).getUpstreamProject()
currentBuild.rawBuild.getCauses().get(0).getUpstreamBuild()

または、パーミッションの変更を必要としないと思われる別の方法: currentBuild.getUpstreamBuilds().get(0).getProjectName()

私が取り組んでいる何かで上流プロジェクトのブランチが必要でした:

currentBuild.getUpstreamBuilds().get(0).getRawBuild().getEnvVars().get("BRANCH_NAME", "")

これに関するドキュメントは次のとおりです: getEnvVars()

ちなみに、混乱した権限を有効にする必要があります。

于 2018-10-12T22:58:24.450 に答える