如何相对于子目录运行詹金斯管道?

我有一个 git 存储库,其中包含 2 个模块。一个是基于 SpringBoot 的后端模块,另一个是基于 VueJS 的前端模块。


app-root

  - backend

  - frontend

我有一个声明式的 Jenkinsfile 来使用相关的 Maven 命令构建我的后端模块。我想从后端目录中执行所有这些 Maven 命令。


一种选择是分别dir("backend") { ....}用于所有命令,这在我看来很难看。


是否有任何其他选项可以指示 Jenkins 从子目录中执行整个管道?


蝴蝶刀刀
浏览 106回答 2
2回答

九州编程

我最终进入了一个“准备项目”阶段,将子目录内容放入根目录。删除所有根内容(阶段“清理”)以绝对确保没有以前构建的剩余部分也可能是一个好主意。node {    def dockerImage    stage('clean') {      sh "rm -rf *"    }    stage('checkout') {        checkout scm    }    // need to have only 'backend' subdir content on the root level    stage('prepare project') {        // The -a option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.        // The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.        sh "cp -a ./backend/. ."        sh "rm -rf ./backend"        // List the final content        sh "ls -la"    }    stage('build docker image') {        dockerImage = docker.build("docker-image-name")    }    stage('publish docker image') {        docker.withRegistry('https://my-private-nexus.com', 'some-jenkins-credentials-id') {            dockerImage.push 'latest'        }    }}

缥缈止盈

您可以在后端和前端模块中拥有 jenkinsfiles,并在每个管道上指向它们,例如:在管道本身上,您只需cd到子模块并执行其命令:pipeline {   agent any   stages {        stage('build') {            steps {                 sh 'cd backend'                 sh 'mvn clean package'             }       }    //other stages   }}如果您不想cd使用子模块,则可以使用稀疏签出,但在这种情况下,您必须相应地更改 jenkinsfile 的路径,因为它将位于根文件夹中。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java