Skip to content

Instantly share code, notes, and snippets.

@jalev
Created November 9, 2017 12:29
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 jalev/67f4a5bf3a5d0690b2a7939522627f7e to your computer and use it in GitHub Desktop.
Save jalev/67f4a5bf3a5d0690b2a7939522627f7e to your computer and use it in GitHub Desktop.
- 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