Skip to content

Instantly share code, notes, and snippets.

@johnbuhay
Last active March 12, 2017 21:40
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 johnbuhay/cc877a013742429f034afc3257c1a988 to your computer and use it in GitHub Desktop.
Save johnbuhay/cc877a013742429f034afc3257c1a988 to your computer and use it in GitHub Desktop.
Jenkins swarm slave docker entrypoint script
#!/usr/bin/env bash
set -e
# checks and defaults
SWARM_JAR="$JENKINS_HOME"/swarm-client-"$SWARM_VERSION".jar
[ -z "$SWARM_MASTER" ] && echo "SWARM_MASTER was not specified, enabling autodiscovery"
[ -z "$SWARM_NAME" ] && SWARM_NAME="$HOSTNAME"
[ -z "$SWARM_DESCRIPTION" ] && SWARM_DESCRIPTION="Swarm version: $SWARM_VERSION - Jenkins slave for docker jobs"
[ -z "$SWARM_CREDENTIALS_FILE" ] && SWARM_CREDENTIALS_FILE="/etc/jenkins-master/credentials"
[ -f "$SWARM_CREDENTIALS_FILE" ] && SWARM_USERNAME=$(head -n1 $SWARM_CREDENTIALS_FILE) \
SWARM_PASSWORD=$(tail -n1 $SWARM_CREDENTIALS_FILE)
DOCKER_CONFIG_FILE="${HOME}/.docker/config.json"
DOCKER_CONFIG_DIRECTORY=$(dirname "$DOCKER_CONFIG_FILE")
DOCKER_REPOSITORY=${DOCKER_REPOSITORY:-'https://index.docker.io/v1/'}
DOCKER_REPOSITORY_USERNAME=${DOCKER_REPOSITORY_USERNAME}
DOCKER_REPOSITORY_PASSWORD=${DOCKER_REPOSITORY_PASSWORD}
[ -d "$DOCKER_CONFIG_DIRECTORY" ] || mkdir "$DOCKER_CONFIG_DIRECTORY"
if [ ! -f "$DOCKER_CONFIG_FILE" ] && [ ! -z "$DOCKER_REPOSITORY_USERNAME" ] && [ ! -z "$DOCKER_REPOSITORY_PASSWORD" ]; then
# if the config file does not exist and
# the username and password variables are defined
# create the config file for a single docker repository
# for multiple docker repositories, supply a file at this location
cat > ${DOCKER_CONFIG_FILE} <<- EOF
{
"auths": {
"${DOCKER_REPOSITORY}": {
"auth": "$(echo -n ${DOCKER_REPOSITORY_USERNAME}:${DOCKER_REPOSITORY_PASSWORD} | base64 -w 0)"
}
}
}
EOF
fi
# entrypoint
exec java -jar "$SWARM_JAR" \
-fsroot "$JENKINS_HOME" \
-noRetryAfterConnected \
-showHostName \
$([ -n "$SWARM_MASTER" ] && echo "-master ${SWARM_MASTER}" )\
-name "$SWARM_NAME" \
-description "${SWARM_DESCRIPTION}" \
-executors "${SWARM_EXECUTORS:-1}" \
-mode "${SWARM_MODE:-exclusive}" \
-labels "${SWARM_LABELS:-docker}" \
-retry "${SWARM_RETRY:-10}" \
-username "${SWARM_USERNAME:-admin}" \
-password "${SWARM_PASSWORD:-passwerd}" \
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment