updated jenkins.sh

pull/2/head
Hari Sekhon 4 years ago
parent 2c870aac4f
commit b69e925943

@ -20,32 +20,61 @@ set -euo pipefail
srcdir="$(dirname "$0")"
# shellcheck disable=SC1090
. "$srcdir/lib/utils.sh"
. "$srcdir/lib/git.sh"
#NUM_AGENTS=1
# shellcheck disable=SC2034,SC2154
usage_description="
Boots Jenkins CI in Docker, and builds the current repo
export JENKINS_HOST=localhost
- boots Jenkins container in Docker
- installs plugins
- prints admin credentials
- creates job pipeline from setup/jenkins-job.xml and Jenkinsfile
- enables job
- builds job
- prints Jenkins UI URL and on Mac opens it in browser
${0##*/} [up]
${0##*/} down
${0##*/} ui - prints the Teamcity Server URL and on Mac automatically opens browser
Idempotent, you can re-run this and continue from any stage
See Also:
jenkins_cli.sh - this script makes heavy use of it to handle API calls with authentication as part of the setup
"
# used by usage() in lib/utils.sh
# shellcheck disable=SC2034
usage_args="[up|down|ui]"
help_usage "$@"
export JENKINS_HOST="${DOCKER_HOST:-localhost}"
export JENKINS_PORT=8080
server="http://$JENKINS_HOST:$JENKINS_PORT"
#api="$server/go/api"
export JENKINS_URL="http://$JENKINS_HOST:$JENKINS_PORT"
cli="$srcdir/jenkins_cli.sh"
#repo="${PWD##*/}"
git_repo="$(git remote -v | grep github.com | sed 's/.*github.com/https:\/\/github.com/; s/ .*//')"
#git_repo="$(git remote -v | grep github.com | sed 's/.*github.com/https:\/\/github.com/; s/ .*//')"
git_repo="$(git_repo)"
repo="${git_repo##*/}"
job="$repo"
job_xml="setup/jenkins-job.xml"
Jenkinsfile=Jenkinsfile
config="$srcdir/setup/jenkins-docker-compose.yml"
export COMPOSE_FILE="$srcdir/setup/jenkins-docker-compose.yml"
plugins_txt="$srcdir/setup/jenkins-plugins.txt"
for filename in "$Jenkinsfile" "$job_xml"; do
if ! [ -f "$job_xml" ]; then
echo "Jenkins configuration '$filename' not found - did you run this from the root of a standard repo?"
exit 1
die "Jenkins configuration '$filename' not found - did you run this from the root of a standard repo?"
fi
done
@ -56,81 +85,96 @@ fi
action="${1:-up}"
shift || :
opts=""
if [ "$action" = up ]; then
opts="-d"
fi
print_creds(){
password="$("$srcdir/jenkins_password.sh" || :)"
echo "Booting Jenkins docker:"
docker-compose -f "$config" "$action" $opts "$@"
echo
if [ "$action" = down ]; then
if [ -n "$password" ]; then
cat <<EOF
Jenkins Login username: admin
Jenkins Login password: $password
EOF
fi
}
if [ "$action" = up ]; then
timestamp "Booting Jenkins docker container:"
docker-compose up -d "$@"
echo >&2
elif [ "$action" = ui ]; then
echo "Jenkins URL: $JENKINS_URL"
print_creds
if is_mac; then
open "$JENKINS_URL"
fi
exit 0
else
docker-compose "$action" "$@"
echo >&2
exit 0
fi
when_jenkins_up(){
when_url_content 90 "$server" '(?i:jenkins|hudson)'
when_url_content 90 "$JENKINS_URL" '(?i:jenkins|hudson)'
echo
}
when_jenkins_up
echo "Installing plugins"
timestamp "Installing plugins"
# would be slow to do this via jenkins-cli
# sed 's/#.*//; s/:.*//; /^[[:space:]]*$/d' setup/jenkins-plugins.txt | while read plugin; do jenkins_cli.sh install-plugin "$plugin"; done
docker-compose -f "$config" exec -T jenkins-server /usr/local/bin/install-plugins.sh < "$plugins_txt"
docker-compose exec -T jenkins-server /usr/local/bin/install-plugins.sh < "$plugins_txt"
echo
password="$("$srcdir/jenkins_password.sh" || :)"
if [ -n "$password" ]; then
cat <<EOF
Jenkins Login username: admin
Jenkins Login password: $password
EOF
fi
# if this fails then the the CLI commands will also fail because they use a similar mechanism to find the admin password from the container to authenticate with the Jenkins API
print_creds
if ! "$cli" list-plugins | grep -q .; then
echo "Restarting Jenkins to pick up plugins:"
timestamp "Restarting Jenkins to pick up plugins:"
#"$cli" restart
#when_ports_down 300 "$JENKINS_HOST" "$JENKINS_PORT"
docker-compose -f "$config" restart jenkins-server "$@"
docker-compose restart jenkins-server "$@"
when_jenkins_up
SECONDS=0
while [ "$SECONDS" -lt 300 ]; do
if "$cli" list-plugins | grep -q .; then
echo
break
fi
tstamp "waiting for Jenkins to finish initializing and list plugins"
sleep 1
done
fi
echo "Validating Jenkinsfile"
SECONDS=0
while [ "$SECONDS" -lt 300 ]; do
if "$cli" list-plugins | grep -q .; then
echo
break
fi
timestamp "waiting for Jenkins to finish initializing and list plugins"
sleep 1
done
# XXX: this fails if the plugins haven't been loaded properly
# XXX: you'll also get this error trying to run the job's pipeline:
# java.lang.NoSuchMethodError: No such DSL method 'pipeline' found among steps
timestamp "Validating Jenkinsfile"
"$cli" declarative-linter < "$Jenkinsfile"
echo
echo "Creating / Updating job - $job:"
timestamp "Creating / Updating job - $job:"
if "$cli" list-jobs | grep -q "^$job$"; then
echo "job already exists, updating..."
timestamp "job already exists, updating..."
"$cli" update-job "$job" < "$job_xml"
else
echo "job does not exist, creating..."
timestamp "job does not exist, creating..."
"$cli" create-job "$job" < "$job_xml"
fi
echo
echo "Enabling job - $job:"
timestamp "Enabling job - $job:"
"$cli" enable-job "$job"
echo
# -f waits for build
# -v prints build contents
echo "Building job - $job and tailing output:"
timestamp "Building job - $job and tailing output:"
"$cli" build -f -v "$job"
#echo "Tailing last build:"

Loading…
Cancel
Save