Jenkins backups // // This is a Jenkinsfile that backs up (parts of) Jenkins. // // So why not use the SCM-sync plugin? I've found it's just not configurable // enough, and if something goes wrong it's a lot more opaque than this technique // which uses standard shell and git commands. // // By default this approach covers users, credentials, and job and plugin configs, // but not full builds/workspaces. Obviously you want to send this stuff only to // a private repository! If you want to change the files that are included to // add or remove files from consideration with the backup, edit the shell script // section below. Make sure this job is pinned to your Jenkins master using your // preferred technique for that. // // The job that executes this Jenkinsfile should describe three parameters: // // * BACKUP_REPO: // a string representing the repo you want your backup's pushed to // // * BACKUP_BRANCH: // a string representing where you want backup's pushed to // // * GIT_CREDENTIAL_ID: // a string representing the Jenkins credential id for // i.e. your github deploy key SSH. See the /credentials // URL on your Jenkins server to add a new SSH key, and make // sure that the username is `git`. Add github deployment keys // at i.e. https://github.com/$user/$project/settings/keys. Make // sure your key allows push and isn't read-only. // pipeline { agent any; stages { stage("INIT"){ steps{ checkout([ $class: 'GitSCM', branches: [[name: '*/${BACKUP_BRANCH}']], doGenerateSubmoduleConfigurations: false, extensions: [[ $class: 'RelativeTargetDirectory', relativeTargetDir: 'jenkins-backup']], submoduleCfg: [], userRemoteConfigs: [ [ credentialsId: "$GIT_CREDENTIAL_ID", url: "$BACKUP_REPO"] ], ]); }} //steps // stage stage("BACKUP"){ steps{ sh """#!/bin/bash pushd jenkins-backup git config --local user.email "jenkins@example.com" git config --global user.name "Jenkins" find .|xargs git rm popd cp -v $JENKINS_HOME/*xml $WORKSPACE/jenkins-backup pushd $JENKINS_HOME/ for i in \$( find jobs|grep config.xml ); do mkdir -p $WORKSPACE/jenkins-backup/`dirname \$i` cp \$i $WORKSPACE/jenkins-backup/`dirname \$i` done popd cp -rfv $JENKINS_HOME/secret* $WORKSPACE/jenkins-backup/ cp -rfv $JENKINS_HOME/user* $WORKSPACE/jenkins-backup/ pushd jenkins-backup find .|xargs git add git commit -a -m"Jenkins Backup Job" """ }} //steps // stage stage("PUSH"){ steps{ sshagent(["$GIT_CREDENTIAL_ID"]) { sh "cd jenkins-backup && git push origin HEAD:$BACKUP_BRANCH" } // sshagent }} //steps // stage }} //stages // pipeline Setting up Neo4j with Vagrant and Ansible An Elixir Microservice Architecture Notes for SaltStack OpenVPN Setup with ansible and terraform on EC2 Links for Topics in Devops Datadog event publishing How to provision Docker with Ansible How to Dockerize Elixir for Raspberry Pi / ARM CI/CD Series Python release automation