How to trigger stages on file - jenkins

I am writing a Jenkins pipeline. I am looking for trick to trigger pipeline stages when one JSON file exists in path.

Related

Jenkins Job Migration to Jenkinsfile

I am searching for a way to convert existing jenkins pipeline jobs with parameters to Jenkinsfile that include the parameter within it.
Maybe xml to Jenkinsfile or something but I am yet to discover a way (found job to DSL or something like that)
I am looking to get job to scripted groovy parameter

How to run a job from a Jenkins Pipeline on the same executor (declarative syntax)

I want to use the Jenkins "PRQA" plugin, which seems not to have the option to use it from a pipeline. The plugin would run static code analysis and publish the results.
In my case, it requires some preparations that are already done in a pipelinejob. Because of that, I want to include the job into that pipeline, but on the same executor with the data prepared by the pipeline as some kind of inlined job-step.
I have tried to create a job for the PRQA-Plugin-Step and execute this with the build step from the pipeline. But this tries to start the job on a new executor (and stalls because I have only one executor).
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Prepare'
}
}
stage('SCA') {
steps {
//Run this without using a new executor with the Environment that exists now
build 'PRQA_Job'
}
}
}
}
What is the correct way to run the job on the same executor with the current working directory.
With specified build 'PRQA_Job' it's not possible to run second job on the same executor (1 job = 1 executor), since main job just waiting for a triggered job to be finished. But you can run another job on the same agent with more than 1 executor to reach workspace from main job.
For a test porpose specify agent name in both jobs: agent 'agent_name_here'
If you want to use plugin functionality for a plugin, which has no native pipeline support, you could try using "step: General Build step" feature for Jenkins Pipelines. You can use the Pipeline Syntax wizzard linked in the Job configuration windows to generate the needed Pipeline description.
If the plugin does not show up in the "step: General Build step" part of Jenkins you can use a separate Job. To copy all the needed files/Data into this second Job you will require to use Archive Artifact/Copy Artifact functionality of Jenkins to save files from your Pipeline build.
For more information on how to sue Archive Artifact/Copy Artifact see https://plugins.jenkins.io/copyartifact/ and
https://www.jenkins.io/doc/pipeline/tour/tests-and-artifacts/

Loading a JSON file from another job's archive into a Jenkins pipeline

I'd like to store a JSON file as the output of one job, and read that JSON in and parse it for use in a pipeline in a different job. I'm having trouble getting the JSON from the first job into my workspace so I can read it.
This mentions reading in a JSON, but not how to get it into the workspace = Pass Jenkins Pipeline parameters from a Jenkins job?
I see some suggestions that involve adding build steps (URL SCM plugin), but adding build steps doesn't seem available in my pipeline job
You should have a look at archiveArtifacts and copyArtifacts. You would archive the JSON file in one job and then copy it from in the other.
Edit:
In a pipeline you would do something like:
copyArtifacts(projectName: 'sourceproject')
or
copyArtifacts(projectName: 'downstream', selector: lastSuccessful())
You can look it up here: https://wiki.jenkins.io/display/JENKINS/Copy+Artifact+Plugin

Upload files to userContent of jenkins server

I have a multi-branch pipeline job from which I would like to upload a file to the Jenkins userContent location using a Groovy script. I tried the job-dsl-plugin to use userContent method, but it throws the error shown below:
java.lang.NoSuchMethodError: No such DSL method 'userContent' found among steps
Reference: https://github.com/jenkinsci/job-dsl-plugin/wiki/Job-DSL-Commands
Do I need to configure anything in order to upload a file to userContent? Is there any other way to upload a file to the userContent location?
You can not simply mix Pipeline DSL and Job DSL. See Use Job DSL in Pipeline scripts for instructions on using the Job DSL build step as a Pipeline step.
node {
jobDsl scriptText: 'userContent("test.txt", new ByteArrayInputStream("test".bytes))'
}

jenkins pipeline unable to read files

I have a simple Jenkinsfile where I want to load some data from the workspace. I am using the pipeline plugin to leverage the Jenkinsfile inside of the repository. The build is farmed off to a matching Jenkins agent. When I try to use "readFile" I get the following message:
java.io.FileNotFoundException: /path/to/jenkins/workspace/XXXXX/project/data.json (No such file or directory)
I also get the same message when trying to load a Groovy file from the workspace.
My Jenkinsfile looks like:
node('master') {
stage "Start"
echo "Starting"
stage "Load File"
def myJson = readFile "data.json"
}
Any ideas why I can't read these files?
Thanks,
Tim
When Jenkins processes a Jenkinsfile it does not automatically pull down the entire source repository. You need to execute "checkout scm" to pull down the contents of the repository. If you fail to do so no other files will be available to the pipeline script.

Resources