Pulling a file from another job before building in Jenkins?
Asked Answered
E

4

15

Hope you can help me on this one. I have a job called "Template" that generates a template.xml file. I have several other jobs that use this template.xml file. However, before they build, i wanted that they could pull the latest template.xml from the "Template" job workspace.

Educt answered 2/9, 2011 at 14:20 Comment(0)
G
25

In your "template" job, under Post-Build Actions, choose to artifact your xml file using the archive option.

You can then use the "Copy Artifact Plugin" to copy it over to all other jobs.

Jenkins Job Setup for Artifact Generator Project: enter image description here

Jenkins Job Setup for Artifact User Project: enter image description here

Grady answered 2/9, 2011 at 14:28 Comment(2)
I ended up using wget to get the file from the workspace... is this a bad practice?Educt
See highlycaffeinated's comments below. To add to that, you are limiting yourself to using Linux slaves (may not be an issue, something to think about though). Also, archiving allows you to relate different template files with different builds of the other jobs (using fingerprinting).Grady
R
5

In your Template job, you can archive the template.xml file as an artifact, then it will be available to your other jobs at a URL similar to the following:

http://myserver/jenkins/job/myjob/lastSuccessfulBuild/artifact/template.xml
Renn answered 2/9, 2011 at 14:27 Comment(2)
I ended up using wget to get the file from the workspace... is this a bad practice?Educt
Two risks in pulling it from the workspace: 1) a person with appropriate permissions can click the "Wipe Out Workspace" link and remove the file; 2) a build may fail leaving either no file or an unusable file. Pulling the file from the archived artifacts will always get you the file from the last successful build.Renn
T
2

I used Copy Artifacts plugin with Jenkinsfile. Here an example:

In the job that is producing the artifact you should do something like:

pipeline {
 options {
  copyArtifactPermission ‘*’ //Here you can specify the job name also
}
  stages {
    stage(“Run") {
      ...
      archiveArtifacts artifacts: “my_artifact.yaml"
    }
  }
}

In the job that is consuming the artifact you can use something like:

stage("Consumer") {
  steps {
    script {
        copyArtifacts filter: “my_artifact.yaml", projectName: 'PRODUCER_JOB', 
          selector: lastSuccessful()

    }
  }
}
Telfer answered 26/2, 2020 at 12:4 Comment(2)
Is archiveArtifacts a part of Copy Artifacts plugin?Justicz
Nope. jenkins.io/doc/pipeline/steps/core/…Justicz
O
0

In my end I use a pretty strainghforward approach. I run jenkins in a usual layout (ubuntu 16.04 server) and I need call the checkstyle maven plugin using the same checkstyle.xml config file residing inside a dev-resources job:

enter image description here

Than I call mvn checkstyle:

enter image description here

Hope it can be useful for someone else.

Oar answered 6/1, 2018 at 0:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.