Download artifact in a Jenkins build pipeline - jenkins

I'm currently learning to use the Jenkins build pipeline. I have one pipeline where I archive the artifact of the build like this:
stage("Build") { gitlabCommitStatus(name: "Build") {
/*Build my program and zip it*/
archiveArtifacts artifacts: 'Debug.7z', onlyIfSuccessful: true
}}
Now I want to use this artifact in another pipeline, but I can't find a command to download a archived artifact into my new pipeline. Note that I do not want to use the artifact in another stage, but in a completely different pipeline of a different build project.

You need to have Copy Artifact plugin installed for this to work. In the job where you want to copy the artifact, use the following code:
pipeline {
stages {
stage ('Copy Build Artifact') {
steps {
echo 'Copying artifact from projectA'
copyArtifacts(projectName: 'projectA', filter:'Debug.7z', optional: true);
// OR
// copyArtifacts(projectName: 'projectA', filter:'Debug.7z', selector: specific('5'), optional: true);
}
}
}
}
where:
selector: the selector to select the build to copy from. If not specified, latest stable build is used
optional: do not fail the step even if no appropriate build is found

Related

Jenkins scripted pipeline - multiple execution with passing artifacts between stages

I was searching and could not find proper information on how to resolve the issue I have with copying the artifacts to jobs that are being executed multiple times in parallel.
I have defined scripted pipeline which executes predefined jobs in stages some which are run in parallel as follows:
the main pipeline job is located in this structure: /jenkins/workspace/<main_job>
these jobs are also preparing artifacts and I later copy them to different stage/jobs in same pipeline with the build id of the executed job.
node() {
stage("Creating Build") {
def stages = [:]
failFast: true
stages["Core"] = {
copyArtifacts(projectName: <job to copy from>, flatten: true, target: '../' + coreBuildJob)
buildCore = build job: coreBuildJob
}
}
stages["Content"] = {
copyArtifacts(projectName: <job to copy from>, flatten: true, target: '../' + contentBuildJob)
buildContent = build job: contentBuildJob
}
parallel(stages)
}
I am using the CopyArtifact Plugin to copy the artifacts that were created but it appears that:
it copies the file to main job folder in the instance.
because of the different workspace/project location I was needed to define the 'target' location to properly copy the artifacts to required job that I execute in the script prior the jobs execution.
e.g for coreBuildJob in the Core stage:
`copyArtifacts(projectName: <job to copy from>, flatten: true, target: '../' + <job_for_execution>)`
This does helps me to resolve the issue with copying the required artifacts by these jobs but I end up with another issue in this case:
When I want this scripted pipeline job to be executed multiple times with different parameters.
The issue is that when the pipeline is executed for the 2nd time and the job that is run in one of the stages runs the 2nd time it creates the following path on the local machine:
`/jenkins/workspace/test_jobs/<job_for_execution>#2`
That means that what I have in my script is not correct, because it copies the files to:
`/jenkins/workspace/test_jobs/<job_for_execution>`
it does not copy the artifacts to proper location and they are not accessible from the executed job.
I thought of having the copyArtifacts part to be executed during the 'build job' command(as you can define in Jenkins UI with passing BUILD_ID as variable to copy artifacts like that) but I cant find any details regarding this to achieve the same behavior with the script.
How can this issue be resolved?
You can use stash/unstash.
After running your build, you can stash:
stash name:'data', includes: './*'
where data is the name (an identifier) and includes can be a directory, subdirectory or single file.
Then, in the stages you want to have the output of your build, use unstash:
unstash 'data'
After doing unstash, the files will be also in respective folder and you can run your other steps.
Refer to https://www.jenkins.io/doc/pipeline/examples/ for more information.

Jenkins - Execute Pipeline from Specific folder

I have a main pipeline in jenkins that will checkout the code , compile, test and build, then push image to docker. This is the high level of CI pipeline that I have. Say job name "MainJobA"
I need to create a new job , just for JavaDoc generation. For that i have created a new script in Git Repo and configured the same in Pipeline job.
Now i need to execute this sub job of javadoc generation and publishing the html reports from the workspace of "MainJobA" . I need to run the SubJobA's pipeline stages from
/home/jenkins/workspace/MainJobA
How can i achieve this?
There is build step exist in jenkins declarative pipelines.
Use it like:
pipeline {
agent any
stages {
stage ("build") {
steps {
build 'Pipeline_B'
}
}
}
}

Jenkins build via SSH, artifacts on host but not in workspace?

I've started with a new company and a very convoluted build methodology. The best way I've found to build projects is to command Jenkins to SSH into the build server and execute a string of commands there.
Building by executing shell commands using SSH works well, but the build artifacts don't show up in the Jenkins workspace. Therefore, I can't seem to archive the artifacts directly to Jenkins.
Is there a way to correct this or work around it? Can Jenkins be set up to archive files from outside the workspace?
Assuming that you've added your build node as a node (slave), here's a stash / unstash example
Stash on the build node
stage ('Stash file on node 1') {
node ('some_build_node') {
dir ('/tmp') {
stash name: 'TestTransfer', includes: 'foo.jar'
}
}
}
Unstash on the master
stage ('Unstash file on node 2') {
node ('master') {
// If you don't use a 'dir' block, it'll unstash in the workspace
// which is handy if you then want to archive the artifacts
dir ('/tmp') {
unstash 'TestTransfer'
}
}
}

View all Jenkins Artifacts

I am archiving artifacts at the end of each build. I have https://wiki.jenkins-ci.org/display/JENKINS/Archived+Artifact+Url+Viewer+PlugIn installed.
Thought the plugin says artifacts can be viewed here
"/archivedArtifacts/artifact/job_name/build_number/relative location of zip or jarfile within artifact folder/location of file within archive Ex:
http://jenkins_url/archivedArtifacts/artifact/Build%20-%20Dev/10526/junit-logs.zip/junit.log"
I am unable to see artifacts using this URL
http://localhost:8080/archivedArtifacts/artifact/TestFirstJob/32/target/MavenTest-0.0.1-SNAPSHOT.jar
I do not have any "artifact" folder. I do have archive folder in the inside each build number.
I can see artifacts of the last build on the respective job homepage.
Any help to view artifacts using this URL is appreciated. Also, Is there a way I can view all of the artifacts build so far at one place.
Archive is how to save files outside workspace. We can clean our workspace, run other builds and the file archived is still safe.
For do that is simple, only with one command we can artifact our files.
archiveArtifacts artifacts: 'file.extension'
For example:
pipeline {
agent any
stages {
stage('Download') {
steps {
sh 'echo "artifact file" > generatedFile.txt'
}
}
}
post {
always {
archiveArtifacts artifacts: 'generatedFile.txt', onlyIfSuccessful: true
}
}
}
Once we build the pipeline and it is successfull, we can find our artifact in either of the following locations:
$JENKINS_HOME/yourjobname/branches/branchname/builds/yourbuildernumer/archive
$JENKINS_HOME/yourjobname/builds/yourbuildernumer/archive

Auto generate build pipeline for gradle build using Jenkinsfile

I am trying to create a build pipeline based upon the Gradle tasks. I have viewed JenkinsFile configuration Pipeline-as-code-demo but I am unable to create a pipeline for gradle tasks. Please suggest me a possible way so that I can use the Jenkinsfile to automatically show the build pipeline just by reading the configurations from the Jenkinsfile.
Thankyou
In case your project uses Gradle Wrapper you can use the following snippet in your Jenkinsfile:
stage('Gradle Build') {
if (isUnix()) {
sh './gradlew clean build'
} else {
bat 'gradlew.bat clean build'
}
}
If you checkout to subdirectory sub-dir you might want to use
stage('Gradle Build') {
if (isUnix()) {
dir('sub-dir') {sh './gradlew clean build'}
} else {
dir('sub-dir') {bat 'gradlew.bat clean build'}
}
}
In case you're using Artifactory to resolve your build dependencies or to deploy your build artifacts, it is recommended to use the Pipeline DSL for Gradle build with Artifactory.
Here's an example taken from the Jenkins Pipeline Examples page:
node {
// Get Artifactory server instance, defined in the Artifactory Plugin administration page.
def server = Artifactory.server "SERVER_ID"
// Create an Artifactory Gradle instance.
def rtGradle = Artifactory.newGradleBuild()
stage 'Clone sources'
git url: 'https://github.com/jfrogdev/project-examples.git'
stage 'Artifactory configuration'
// Tool name from Jenkins configuration
rtGradle.tool = "Gradle-2.4"
// Set Artifactory repositories for dependencies resolution and artifacts deployment.
rtGradle.deployer repo:'ext-release-local', server: server
rtGradle.resolver repo:'remote-repos', server: server
stage 'Gradle build'
def buildInfo = rtGradle.run rootDir: "gradle-examples/4/gradle-example-ci-server/", buildFile: 'build.gradle', tasks: 'clean artifactoryPublish'
stage 'Publish build info'
server.publishBuildInfo buildInfo
}
Otherwise, you can simply run the gradle command with the sh or bat Pipeline steps.
In jenkins you can creates a jenkins pipeline using a script which is written in Jenkinsfile.
We write a script using 'stages' and 'node' as building block, these building blocks allow you to specify instructions that should be executed as part of jenkins pipeline.
To execute gradle build using JenkinsFile first check for Operating system and call appropriate shell that can execute that gradle task, as below:
Jenkinsfile
stage 'build_Project'
node{
if(isUnix()){
sh 'gradle build --info'
}
else{
bat 'gradle build --info'
}
}
Above code snippet create a stage with name build_project and execute gradle build script of the current project.

Resources