Created
November 9, 2017 12:29
-
-
Save jalev/67f4a5bf3a5d0690b2a7939522627f7e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- job: | |
name: docker | |
description: Creates Docker containers in the 'dockerfiles' git repo, similiar to the way that Docker does it. | |
node: docker | |
logrotate: | |
numToKeep: 30 | |
parameters: | |
- string: | |
name: REGISTRY | |
default: <SOME THING> | |
description: The name of the registry that you want to push this image to | |
scm: | |
- git: | |
url: <SOME URL> | |
credentials-id: <SOME GIT CREDENTIALS> | |
skip-tag: true | |
clean: | |
before: false | |
after: false | |
wipe-workspace: true | |
properties: | |
- github: | |
url: <HTTP OF YOUR URL> | |
builders: | |
- shell: | | |
#!/bin/bash | |
HEAD=$(git rev-parse --verify HEAD) | |
git fetch -q "$GIT_URL" "refs/heads/master" | |
UPSTREAM=$(git rev-parse --verify FETCH_HEAD) | |
# Detects changes in the tree and goes to build ONLY those docker containers | |
diff=$(git diff --numstat "$UPSTREAM...$HEAD" -- ./ | awk '{print $3}') | |
repos=($(echo $diff | awk -F '/' '{print $1}')) | |
if [ "${#repos[@]}" -eq 0 ]; then | |
echo "Skipping, as there are no changes in any repo" | |
exit 0 | |
fi | |
echo "Changes found. Iterating..." | |
for repo in "${repos[@]}"; | |
do | |
echo "Building lists." | |
install_changed=($(git diff --numstat "$UPSTREAM...$HEAD" -- ./${repo}/install | awk '{print $3}')) | |
dockerfile_changed=($(git diff --numstat "$UPSTREAM...$HEAD" -- ./${repo}/ | awk '{print $3}' | grep "Dockerfile" | awk -F '/' '{print $2}')) | |
# change into the directory we want to build | |
cd $repo | |
# If something in the installation directory has changed, then rebuild everything. | |
if [ "${#install_changed}" -gt 0 ]; | |
then | |
echo "Installation directory changed. Rebuilding all containers for ${repo}" | |
versions=$(find * -maxdepth 0 -type d ! -name install -print) | |
for version in "${versions[@]}"; | |
do | |
echo "Building ${REGISTRY}/${repo}:${version}" | |
./build.sh "${version}" "${REGISTRY}" | |
if [ $? -eq 0 ]; | |
then | |
docker push "${REGISTRY}/${repo}:${version}" | |
docker rmi "${REGISTRY}/${repo}:${version}" | |
else | |
echo "Could not push to registry." | |
exit 1 | |
fi | |
done | |
# If the dockerfile is the one to have changed, then... | |
elif [ "${#dockerfile_changed[@]}" -gt 0 ] ; | |
then | |
echo "Building docker containers." | |
for version in "${dockerfile_changed[@]}"; | |
do | |
echo "Building ${REGISTRY}/${repo}:${version}" | |
./build.sh "${version}" "${REGISTRY}" | |
if [ $? -eq 0 ]; | |
then | |
docker push "${REGISTRY}/${repo}:${version}" | |
docker rmi "${REGISTRY}/${repo}:${version}" | |
else | |
echo "Could not push to registry." | |
exit 1 | |
fi | |
done | |
else | |
echo "Couldn't find anything to build. Skipping to next." | |
fi | |
done | |
triggers: | |
- github | |
wrappers: | |
- ssh-agent-credentials: | |
user: '<SOME GIT CREDENTIALS>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment