3

I am looking for a way to create a copy of a pipeline project in Jenkins. If i select a normal project, i see an option "Copy Project" in the sidebar, but that is not there in pipeline projects. Is it at all possible to copy pipelines?

NeoTheThird
  • 360
  • 3
  • 16
  • 1
    Please check https://issues.jenkins-ci.org/plugins/servlet/mobile#issue/JENKINS-32756 – slashpai Dec 18 '17 at 13:47
  • Interesting, thanks @slashpai. – NeoTheThird Dec 18 '17 at 14:28
  • Possible duplicate of [How do I clone a job in jenkins?](https://stackoverflow.com/questions/23838259/how-do-i-clone-a-job-in-jenkins) – mkobit Dec 18 '17 at 15:08
  • 1
    @mkobit This question here is specific about pipelines which behave a little different. Since when searching specifically for copying pipeline jobs there are no useful results, i would like to keep this one open, especially since ther's already a high-quality answer. – NeoTheThird Dec 19 '17 at 00:16

2 Answers2

4

This is now possible using the Jenkins UI. When you click "New Item" in the main Jenkins windows, all the way at the bottom you can specify which item you wish to copy. Specify the name of the pipeline there. Jenkins would copy over all the config for you.

Jehandad
  • 414
  • 4
  • 13
1

I do not know of a way to do that in the UI, I am using the Jenkins CLI to do that. I wrote a wrapper for the command line which looks like this:

#!/bin/sh
#file: jenkins_cli.sh
cd $1
if [ -z ${JENKINS_CREDENTIALS+x} -o -z ${JENKINS_SERVER+x} ]
  then
    JENKINS_SERVER=$(<jenkins_url.txt)
    JENKINS_CREDENTIALS=$(<credentials_api.txt)
fi
java -jar jenkins-cli.jar -s $JENKINS_SERVER -auth $JENKINS_CREDENTIALS ${@:2} | dos2unix

since I have subfolders for every Jenkins master I have and those subfolders contain the jenkins_url.txt and credentials_api.txt. I then invoke commands like this: ./jenkins_cli <jenkinsxyz> help.

To save jobs of one Jenkins Master, I created this script:

#!/bin/sh
# save all job configurations locally
#
# parameters:
#   directory with credentials_api.txt, jenkins_url.txt and jenkins-cli.jar of 
#   the jenkins server that will be backuped
cd $1
CLI="./../jenkins_cli.sh"

echo "create backup folder.."
mkdir -p backup

echo "save job list.."
. $CLI . list-jobs | unix2dos.exe > jobs.txt

echo "save job configuration.."
LOOPS=$(wc -l < jobs.txt)
for l in $(seq $LOOPS); do
    JOBNAME=$(tail -n+$l jobs.txt | head -n1)
    . $CLI . get-job "$JOBNAME" > ./backup/$JOBNAME.xml
    echo "saved job nr. $l: $JOBNAME"
done

not the nicest, but it works :) and I am using Git for Windows, thats why I am piping everything to unix2dos.

tim
  • 407
  • 6
  • 16
  • forgot to mention: if I want to create the saved job on a new master I just call `./jenkins_cli create-job < job.xml` – tim Dec 18 '17 at 15:26