Skip to content

Commit

Permalink
Switch Jenkins example to docker. Way faster, less noise when provisi…
Browse files Browse the repository at this point in the history
…oning
  • Loading branch information
cburgmer committed Jun 22, 2018
1 parent 1b290bc commit 502c8aa
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 98 deletions.
2 changes: 2 additions & 0 deletions examples/jenkins/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
12 changes: 0 additions & 12 deletions examples/jenkins/Vagrantfile

This file was deleted.

72 changes: 0 additions & 72 deletions examples/jenkins/provision.sh

This file was deleted.

118 changes: 105 additions & 13 deletions examples/jenkins/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion examples/runJenkinsExample.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 502c8aa

Please sign in to comment.