diff --git a/examples/jenkins/Dockerfile b/examples/jenkins/Dockerfile new file mode 100644 index 00000000..de37b6f3 --- /dev/null +++ b/examples/jenkins/Dockerfile @@ -0,0 +1,2 @@ +FROM jenkins/jenkins:2.128-alpine +RUN /usr/local/bin/install-plugins.sh git promoted-builds git-client parameterized-trigger build-pipeline-plugin dashboard-view diff --git a/examples/jenkins/Vagrantfile b/examples/jenkins/Vagrantfile deleted file mode 100644 index d7f44e67..00000000 --- a/examples/jenkins/Vagrantfile +++ /dev/null @@ -1,12 +0,0 @@ -# -*- mode: ruby -*- -# vi: set ft=ruby : - -Vagrant.configure(2) do |config| - config.vm.box = "ubuntu/xenial64" - - config.vm.network :forwarded_port, guest: 8080, host: 8080 - config.vm.synced_folder "jobs/", "/mnt/jobs" - config.vm.synced_folder "views/", "/mnt/views" - - config.vm.provision :shell, path: 'provision.sh' -end diff --git a/examples/jenkins/provision.sh b/examples/jenkins/provision.sh deleted file mode 100755 index 8aa1ced9..00000000 --- a/examples/jenkins/provision.sh +++ /dev/null @@ -1,72 +0,0 @@ -#!/bin/bash -# With ideas from https://github.com/rgl/jenkins-vagrant/blob/master/provision.sh -set -eo pipefail - -jenkins_queue_length() { - curl --silent http://localhost:8080/queue/api/json | python -c "import json; import sys; print len(json.loads(sys.stdin.read())['items'])" -} - -wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add - -sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list' -sudo apt-get update -sudo apt-get -qy install default-jre curl git python -sudo apt-get -qy install jenkins # only install after all dependencies are already available - -echo "Disabling Jenkins security" -while [ ! -s /var/lib/jenkins/secrets/initialAdminPassword ]; do sleep 1; done -sudo service jenkins stop - -# disable security, see https://jenkins.io/doc/book/operating/security/#disabling-security -sleep 10 -sudo rm /var/lib/jenkins/config.xml - -# disable showing the wizard on the first access. -cp -p /var/lib/jenkins/jenkins.install.UpgradeWizard.state /var/lib/jenkins/jenkins.install.InstallUtil.lastExecVersion - -sudo service jenkins start - -echo "Waiting for Jenkins to come up" -until curl --output /dev/null --silent --head --fail http://localhost:8080; do sleep 1; done - -echo "Installing plugins" -# Need to fetch plugin list first, Jenkins fails with 'No update center data is retrieved yet from: https://updates.jenkins.io/update-center.json' if we don't do this manually -curl --silent -X POST http://localhost:8080/pluginManager/checkUpdatesServer > /dev/null - -for PLUGIN in git promoted-builds git-client parameterized-trigger build-pipeline-plugin dashboard-view; do - sudo java -jar /var/cache/jenkins/war/WEB-INF/jenkins-cli.jar -s http://localhost:8080/ install-plugin "${PLUGIN}" -done - -sudo service jenkins restart - -echo "Waiting for Jenkins to come up" -until curl --output /dev/null --silent --head --fail http://localhost:8080; do sleep 1; done - -# Create or Update -echo "Configuring pipeline" -pushd . -cd /mnt/jobs -for JOB_CONFIG in *; do - # shellcheck disable=SC2001 - JOB_NAME=$( echo "$JOB_CONFIG" | sed s/.xml$// ) - curl --silent -X POST --data-binary "@/mnt/jobs/$JOB_CONFIG" -H "Content-Type: application/xml" "http://localhost:8080/createItem?name=$JOB_NAME" > /dev/null - curl --silent -X POST --data-binary "@/mnt/jobs/$JOB_CONFIG" "http://localhost:8080/job/$JOB_NAME/config.xml" > /dev/null -done -cd /mnt/views -for VIEW_CONFIG in *; do - # shellcheck disable=SC2001 - VIEW_NAME=$( echo "$VIEW_CONFIG" | sed s/.xml$// ) - curl --silent -X POST --data-binary "@/mnt/views/$VIEW_CONFIG" -H "Content-Type: application/xml" "http://localhost:8080/createView?name=$VIEW_NAME" > /dev/null - curl --silent -X POST --data-binary "@/mnt/views/$VIEW_CONFIG" "http://localhost:8080/view/$VIEW_NAME/config.xml" > /dev/null -done -popd - -echo "Triggering builds" -for _ in 1 2 3 4 5; do - curl --silent -X POST http://localhost:8080/job/Test/build > /dev/null - - # Wait for build to run to enqueue next - sleep 2 - until [[ "$(jenkins_queue_length)" -eq 0 ]] ; do - sleep 1 - done -done diff --git a/examples/jenkins/run.sh b/examples/jenkins/run.sh index a75d6a49..439723f5 100755 --- a/examples/jenkins/run.sh +++ b/examples/jenkins/run.sh @@ -24,31 +24,123 @@ announce() { hint_at_logs() { # shellcheck disable=SC2181 - if [[ "$?" -ne 0 ]]; then + if [[ "$?" -ne 0 && -f "$TMP_LOG" ]]; then echo echo "Logs are in ${TMP_LOG}" fi } -goal_start() { - announce "Starting vagrant image" - echo -n " (give it a few minutes)" +container_exists() { + if [[ -z $(docker container ls -q -a --filter name=buildviz_jenkins_example) ]]; then + return 1 + else + return 0 + fi +} + +jenkins_queue_length() { + curl --silent http://localhost:8080/queue/api/json | python -c "import json; import sys; print len(json.loads(sys.stdin.read())['items'])" +} + +build_queue_empty() { + if [[ "$(jenkins_queue_length)" -eq 0 ]]; then + return 0; + else + return 1; + fi +} + +configure_pipeline() { + local job_config + local job_name + local view_config + local view_name + ( + cd "$SCRIPT_DIR"/jobs + for job_config in *; do + # shellcheck disable=SC2001 + job_name=$( echo "$job_config" | sed s/.xml$// ) + curl --fail --silent -X POST --data-binary "@$job_config" -H "Content-Type: application/xml" "${BASE_URL}/createItem?name=${job_name}" > /dev/null + curl --fail --silent -X POST --data-binary "@$job_config" "${BASE_URL}/job/${job_name}/config.xml" > /dev/null + done + ) ( - cd "$SCRIPT_DIR" - vagrant up > "$TMP_LOG" + cd "$SCRIPT_DIR"/views + for view_config in *; do + # shellcheck disable=SC2001 + view_name=$( echo "$view_config" | sed s/.xml$// ) + curl --fail --silent -X POST --data-binary "@$view_config" -H "Content-Type: application/xml" "${BASE_URL}/createView?name=${view_name}" > /dev/null + curl --fail --silent -X POST --data-binary "@$view_config" "${BASE_URL}/view/${view_name}/config.xml" > /dev/null + done ) +} + +trigger_builds() { + for _ in 1 2 3 4 5; do + curl --fail --silent -X POST "${BASE_URL}/job/Test/build" > /dev/null + + # Wait for build to run to enqueue next + sleep 2 + until build_queue_empty ; do + sleep 1 + done + done +} + +provision_jenkins() { + docker build "$SCRIPT_DIR" --tag buildviz_jenkins_example + docker container create -p 8080:8080 --name buildviz_jenkins_example buildviz_jenkins_example + docker container start buildviz_jenkins_example + echo "Disabling Jenkins security" + wait_for_server "$BASE_URL/favicon.ico" + sleep 10 + docker container exec buildviz_jenkins_example rm /var/jenkins_home/config.xml + docker container exec buildviz_jenkins_example cp -p /var/jenkins_home/jenkins.install.UpgradeWizard.state /var/jenkins_home/jenkins.install.InstallUtil.lastExecVersion + docker container restart buildviz_jenkins_example wait_for_server "$BASE_URL" + + echo "Configuring pipeline" + configure_pipeline + echo "Triggering builds" + trigger_builds +} + +goal_start() { + if ! container_exists; then + announce "Provisioning docker image" + echo + provision_jenkins + echo "done" + else + announce "Starting docker image" + docker container start buildviz_jenkins_example > "$TMP_LOG" + + wait_for_server "$BASE_URL" + echo " done" + rm "$TMP_LOG" + fi +} + +goal_stop() { + announce "Stopping docker image" + docker container stop buildviz_jenkins_example > "$TMP_LOG" echo " done" rm "$TMP_LOG" } -goal_stop() { - announce "Stopping vagrant image" - ( - cd "$SCRIPT_DIR" - vagrant halt > "$TMP_LOG" - ) +goal_destroy() { + announce "Destroying docker container" + docker container stop buildviz_jenkins_example > "$TMP_LOG" + docker container rm buildviz_jenkins_example >> "$TMP_LOG" + echo " done" + rm "$TMP_LOG" +} + +goal_purge() { + announce "Purging docker images" + docker rmi jenkins/jenkins:2.128-alpine >> "$TMP_LOG" + docker rmi buildviz_jenkins_example >> "$TMP_LOG" echo " done" rm "$TMP_LOG" } @@ -59,7 +151,7 @@ main() { if type -t "goal_$1" &>/dev/null; then "goal_$1" else - echo "usage: $0 (start|stop)" + echo "usage: $0 (start|stop|destroy|purge)" fi } diff --git a/examples/runJenkinsExample.sh b/examples/runJenkinsExample.sh index a4a05d3a..144b94b9 100755 --- a/examples/runJenkinsExample.sh +++ b/examples/runJenkinsExample.sh @@ -21,7 +21,7 @@ clean_up() { main() { ensure_port_available - echo "This example will download and install Jenkins in a VirtualBox and then sync its output to buildviz" + echo "This example will download and install Jenkins via Docker and then sync its output to buildviz" echo echo "Press any key to continue"