如何从自定义Jenkins管道调用存储库的Jenkinsfile阶段?

时间:2019-04-08 19:07:32

标签: jenkins jenkins-pipeline

我正在尝试创建一个自定义的Jenkins管道,它将多个Jenkins作业生成的工件绑定在一起。这些作业中的每一个都在不同的存储库上运行(基于它们自己的Jenkinsfiles)。这些Jenkinsfiles阶段执行诸如创建存档,编译一些代码等工作。

可能有人说我可以使用单个作业的工件,然后将它们绑定在一起,但是问题是,这个高级管道会在特定的git tag上签出这些存储库,生成一些文件签名,编译发行版应用程序版本等

为了给您一个简短的想法,这是我的一个Jenkinsfiles的一部分,让我们从componentA存储库中说:

pipeline {
  agent any
  stages {
    stage('Cleanup library') {
      steps {
        sh 'rm -rf .git .gitignore .gitmodules Jenkinsfile .some_useless_files'
      }
    }
    stage('Pack library') {
      steps {
        sh 'tar -czf my_arch.tar.gz ./*'
      }
    }
    stage('Save artifacts') {
      steps {
        archiveArtifacts 'my_arch.tar.gz'
      }
    }
  }
}

这是主要管道:

node {
    dir('compA') {
        git url: 'git@some.path.com:my_lib/componentA.git'
        sh('git checkout $compA_rv')
        load 'Jenkinsfile'
    }
    // some other stuff
    //...
}

输出:

+ git checkout compA_rv
Note: checking out 'compA_rv'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 5a840d0... Some commit
[Pipeline] load
[Pipeline] { (Jenkinsfile)
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/apk-release@2
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Cleanup library)
[Pipeline] sh
+ rm -rf .git .gitignore .gitmodules Jenkinsfile .some_useless_files
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Pack library)
[Pipeline] sh
+ tar -czf my_arch.tar.gz ./my_arch.tar.gz
tar: ./my_arch.tar.gz: File shrank by 448 bytes; padding with zeros
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Save artifacts)
Stage "Save artifacts" skipped due to earlier failure(s)
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // load
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

似乎该管道加载了Jenkinsfile并在另一个目录Running on Jenkins in /var/jenkins_home/workspace/apk-release@2中执行它,因此它不能一无所有地创建存档。

由于我不熟悉Jenkins管道,您对如何解决该问题有任何建议吗?

最诚挚的问候

1 个答案:

答案 0 :(得分:0)

我认为您可以完全切换到管道并使用自定义工作区

pipeline {
  agent {
    node {
      label 'my-defined-label'
      customWorkspace '/some/other/path'
    }
  }
  stages {
    stage('Example Build') {
      steps {
        sh 'mvn -B clean verify'
      }
    }
  }
}