#!/bin/bash | |
# Setup | |
# | |
# - Create a new Jenkins Job | |
# - Mark "None" for Source Control Management | |
# - Select the "Build Periodically" build trigger | |
# - configure to run as frequently as you like | |
# - Add a new "Execute Shell" build step | |
# - Paste the contents of this file as the command | |
# - Save | |
# | |
# NOTE: before this job will work, you'll need to manually navigate to the $JENKINS_HOME directory | |
# and do the initial set up of the git repository. | |
# Make sure the appropriate remote is added and the default remote/branch set up. | |
# | |
# Jenkins Configuraitons Directory | |
cd $JENKINS_HOME | |
# Add general configurations, job configurations, and user content | |
git add -- *.xml jobs/*/*.xml userContent/* | |
# only add user configurations if they exist | |
if [ -d users ]; then | |
user_configs=`ls users/*/config.xml` | |
if [ -n "$user_configs" ]; then | |
git add $user_configs | |
fi | |
fi | |
# mark as deleted anything that's been, well, deleted | |
to_remove=`git status | grep "deleted" | awk '{print $3}'` | |
if [ -n "$to_remove" ]; then | |
git rm --ignore-unmatch $to_remove | |
fi | |
git commit -m "Automated Jenkins commit" | |
git push -q -u origin master |
This comment has been minimized.
This comment has been minimized.
Thanks, it saved me a lot of time too. |
This comment has been minimized.
This comment has been minimized.
Thank you! |
This comment has been minimized.
This comment has been minimized.
I wonder if the line 34 awk should actually read '{print $2}' instead of '{print $3}'?
|
This comment has been minimized.
This comment has been minimized.
Thanks a lot !! |
This comment has been minimized.
This comment has been minimized.
Thank you! I want to post a "You Da Real MVP" meme but I don't think that's allowed on GH. |
This comment has been minimized.
This comment has been minimized.
Just a note: I replaced it for |
This comment has been minimized.
This comment has been minimized.
this is great, thank you! what do you add from the
I added all the secret files, and now I'm getting this:
|
This comment has been minimized.
This comment has been minimized.
Modified "to_remove" expression to be more robust: |
This comment has been minimized.
This comment has been minimized.
Adding user configurations gave me some errors, and can be simplified:
|
This comment has been minimized.
This comment has been minimized.
A simpler way of doing deletion resilient to whitespace in file names: |
This comment has been minimized.
This comment has been minimized.
The initial script works fine for me, but leaves out some more complex (deeper nested) jobs. |
This comment has been minimized.
This comment has been minimized.
I used the following to cope with nested jobs folders |
This comment has been minimized.
This comment has been minimized.
This can be simplified:
to:
|
This comment has been minimized.
This comment has been minimized.
And this can be simplified
to:
|
This comment has been minimized.
This comment has been minimized.
Final edition: #!/bin/sh -e
cd "$JENKINS_HOME"
# Add general configurations, secrets, job configurations, nodes, user content, users and plugins info:
ls -1d *.xml secrets/ jobs/*/*.xml nodes/*/*.xml userContent/* users/*/config.xml \
plugins/*/META-INF/MANIFEST.MF 2>/dev/null | xargs -r -d '\n' git add --
# Track deleted files:
LANG=C git status | awk '$1 == "deleted:" { print $2; }' | xargs -r git rm --ignore-unmatch
LANG=C git status | grep -q '^nothing to commit' || {
git commit -m "Automated Jenkins commit at $(date '+%Y-%m-%d %H:%M')"
git push -q -u origin master
} |
This comment has been minimized.
This comment has been minimized.
Published here: https://github.com/ilyaevseev/jenkins2git/ |
This comment has been minimized.
This comment has been minimized.
I'm noticing that when you restore jenkins, it's important to make sure the files are all owned by the jenkins user with the jenkins group or you might see the below error in the GUI:
|
This comment has been minimized.
This comment has been minimized.
transistorgit's comment was mangled by the Markdown renderer, but I think this was the intention:
|
This comment has been minimized.
This comment has been minimized.
This script has one issue though, folders and build history are not saved. |
This comment has been minimized.
Saved me the time of writing my own backup script. Thanks👍