Skip to content

Instantly share code, notes, and snippets.

@managedkaos
Last active January 1, 2023 00:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save managedkaos/20c9387b639f5fabdf6a3fd99dc9db97 to your computer and use it in GitHub Desktop.
Save managedkaos/20c9387b639f5fabdf6a3fd99dc9db97 to your computer and use it in GitHub Desktop.
A `bash` function for running the Jenkins CI LTS image as a docker process.
function jenkinsondocker() {
if [ $(ps -elf | grep docker | wc -l) -gt 0 ];
then
if [ "$1" == "clean" ] || [ "$2" = "clean" ]; then
echo "# $(date) Removing any previously running Jenkins instances..."
echo Stopping $(docker stop jenkins 2>&1) | sed 's/Error response from daemon://'
echo Removing $(docker rm jenkins 2>&1) | sed 's/Error response from daemon://'
fi
docker pull jenkins/jenkins:lts
docker run --detach --publish 49000:8080 --name jenkins jenkins/jenkins:lts || return
# The container must be running before Jenkins can be configured
if [ "$1" == "configure" ] || [ "$2" = "configure" ]; then
# configure jenkins
echo "# $(date) Configuring Jenkins..."
docker exec --user root jenkins bash -c "apt update && apt install wget && which wget"
## skip the installation wizard at startup
echo "# $(date) Skipping the installation wizard on first boot..."
docker exec --user root jenkins bash -c "echo 'JAVA_ARGS=\"-Djenkins.install.runSetupWizard=false\"' >> /etc/default/jenkins"
## download the list of plugins
echo "# $(date) Downloading the list of plugins..."
docker exec --user root jenkins wget https://raw.githubusercontent.com/jenkinsci/jenkins/master/core/src/main/resources/jenkins/install/platform-plugins.json
## get the suggested plugins
echo "# $(date) Using the keyword 'suggest' to find the suggested plugins in the list..."
docker exec --user root jenkins bash -c 'grep suggest platform-plugins.json | cut -d\" -f 4 | tee suggested-plugins.txt'
## download the plugin installation tool
echo "# $(date) Downloading the plugin installation tool"
docker exec --user root jenkins bash -c 'wget https://github.com/jenkinsci/plugin-installation-manager-tool/releases/download/2.12.3/jenkins-plugin-manager-2.12.3.jar'
## run the plugin installation tool
echo "# $(date) Running the plugin installation tool..."
docker exec --user root jenkins bash -c '/usr/bin/env java -jar ./jenkins-plugin-manager-2.12.3.jar \
--verbose \
--plugin-download-directory=/var/jenkins_home/plugins \
--plugin-file=./suggested-plugins.txt | tee /var/log/plugin-installation.log'
# The container must be restarted to pick up the new configuration
echo "# $(date) Restarting Jenkins container..."
docker restart jenkins
fi
echo -n "# $(date) Waiting for Jenkins process to start ."
until [ $(curl -o /dev/null --silent --head --write-out '%{http_code}\n' http://127.0.0.1:49000) -eq 403 ]; do
echo -n '.';
sleep 1;
done
ADMIN_PASS=$(docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword)
echo
echo
echo
echo "Jenkins URL:"
echo " http://localhost:49000"
echo
echo "Admin password:"
echo " ${ADMIN_PASS}"
echo
echo "To run commands in the container as root:"
echo " docker exec --user root -it jenkins bash"
else
echo "Can't find any docker processes. Is Docker really running? :("
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment