# This should be changed to your system's jenkins root
JENKINS_ROOT="/var/lib/jenkins"
JENKINS_USERS="$JENKINS_ROOT/users"
JENKINS_JOBS="$JENKINS_ROOT/jobs"
JENKINS_CONFIG="$JENKINS_ROOT/config.xml"
# This variable is used so that we can verify that a user is not found
FAIL_TEST=0
exportUserFolder() {
if [ -d "$JENKINS_USERS/$1" ]; then
cp -R "$JENKINS_USERS/$1" ./
else
FAIL_TEST=`expr $FAIL_TEST + 1`
fi
}
exportUserPermissions() {
if [ $(grep -c "^.*.*:$1<\/permission>.*" $JENKINS_CONFIG) -ne 0 ]; then
grep "^.*.*:$1<\/permission>.*" $JENKINS_CONFIG > $1.perm
else
FAIL_TEST=`expr $FAIL_TEST + 1`
fi
}
exportUsers() {
# Pre-work cleanup
if [ -d "working" ]; then
rm -R -f working
fi
if [ -f "user.tar" ]; then
rm -R -f user.tar
fi
mkdir working
cd working
# Checks if any users have been specified
if [ ! -z $1 ]; then
# Iterates over the users if they have
for u in $*; do
exportUserFolder $u
exportUserPermissions $u
# if FAIL_TEST is 2, that means both of the above functions failed to find a user
if [ "$FAIL_TEST" -ge "2" ]; then
echo "ERROR: User $u is not found!"
exit 1
fi
FAIL_TEST=0
done
else
# Otherwise we export everything!
# Copies ALL user folders
for f in `ls "$JENKINS_USERS"`; do
exportUserFolder $f
done
# Extracts all the users for who we have global permissions
for u in `sed -n -e 's/^.*.*:\(.*\)<\/permission>.*/\1/p' $JENKINS_CONFIG | uniq | sort`; do
exportUserPermissions $u
done
fi
# create the user.tar
# We only do this once, at the very end, so that if this fails for any reason a user.tar is not created,
# so we don't risk having a bad artifact built from the job
tar cv --file user.tar *
# Pop up to the workspace
cd ..
# move user.tar out of working into the workspace so that it can by archvied by jenkins
mv working/user.tar ./user.tar
# post-work cleanup
rm -R -f working
}
exportUsers $USERS
The only parameter needed is USERS, which is a space-separated list of users to export. If it's blank, or omitted, it exports all the users to a file called user.tar, which you should set up jenkins to archive.
Here's the import script:
INPUT_FILE="user.tar"
# This should be changed to your system's jenkins root JENKINS_ROOT="/Users/Shared/Jenkins/Home"
JENKINS_USERS="$JENKINS_ROOT/users"
JENKINS_JOBS="$JENKINS_ROOT/jobs"
JENKINS_CONFIG="$JENKINS_ROOT/config.xml"
# This variable is used so that we can verify that a user is not found
FAIL_TEST=0
importUserPermissions() {
if [ $PERMISSIONS = "true" ]; then
if [ -f "$1.perm" ]; then
# delete existing permissions
sed -i -e "/^.*.*:$1<\/permission>.*/d" $JENKINS_CONFIG
# Insert new permissions
while read line; do
sed -i -e "/^.*<\/authorizationStrategy>.*/ i\\
$line" $JENKINS_CONFIG
done < "$1.perm"
else
FAIL_TEST=`expr $FAIL_TEST + 1`
fi
fi
}
importUserFolder() {
if [ -d "$JENKINS_USERS/$1" ] && [ "$REPLACE_CONFIG" = "false" ]; then
echo "User folder for $1 already exists."
echo "Enable REPLACE_CONFIG to overwrite existing user folders"
else
if [ -d "$1" ]; then
cp -R "./$1" "$JENKINS_USERS/"
else
FAIL_TEST=`expr $FAIL_TEST + 1`
fi
fi
}
importUsers() {
if [ -d "working" ]; then
rm -R -f working
fi
mkdir working
cd working
tar xf "../$INPUT_FILE"
# Checks if any users have been specified
if [ ! -z $1 ]; then
# Iterates over the users if they have
for u in $*; do
importUserFolder $u
importUserPermissions $u
# if FAIL_TEST is 2, that means both of the above functions failed to find a user
if [ "$FAIL_TEST" -ge "2" ]; then
echo "ERROR: User $u is not found!"
exit 1
fi
FAIL_TEST=0
done
else
# Otherwise we import everything!
# Copies ALL user folders
for f in `ls -l | awk '/^d.*/{print $(NF);}'`; do
importUserFolder $f
done
for f in `ls *.perm | sed 's/\(.*\)\..*/\1/'`; do
importUserPermissions $f
done
fi
# Pop up to the workspace
cd ..
# post-work cleanup
rm -R -f working
rm user.tar
}
importUsers $USERS
This one takes a few more parameters:
- A file paramater writing to user.tar
- USER, a string that specifies the user(s) to import, spaces separating multiple users. If omitted or empty, all the users in the archive will be imported.
- PERMISSIONS, a boolean indicating wether to import permissions
- REPLACE_CONFIG, a boolean indicating wether to overwrite existing user info
I zipped up my Jenkins jobs for easy use here
