Skip to content

Instantly share code, notes, and snippets.

@byrnedo
Created April 7, 2016 07:08
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save byrnedo/9b2078c191360c681f85cebb2187d66f to your computer and use it in GitHub Desktop.
Save byrnedo/9b2078c191360c681f85cebb2187d66f to your computer and use it in GitHub Desktop.
Check if newer docker image available.
#!/bin/bash
# ensure running bash
if ! [ -n "$BASH_VERSION" ];then
echo "this is not bash, calling self with bash....";
SCRIPT=$(readlink -f "$0")
/bin/bash $SCRIPT
exit;
fi
REGISTRY="my.registry.com:5000"
REPOSITORY="awesome-project-of-awesomeness"
LATEST="`wget -qO- http://$REGISTRY/v1/repositories/$REPOSITORY/tags`"
LATEST=`echo $LATEST | sed "s/{//g" | sed "s/}//g" | sed "s/\"//g" | cut -d ' ' -f2`
RUNNING=`docker inspect "$REGISTRY/$REPOSITORY" | grep Id | sed "s/\"//g" | sed "s/,//g" | tr -s ' ' | cut -d ' ' -f3`
if [ "$RUNNING" == "$LATEST" ];then
echo "same, do nothing"
else
echo "update!"
echo "$RUNNING != $LATEST"
fi
@chetabahana
Copy link

chetabahana commented Jun 15, 2019

Cronjob to check both image and source:

  • auto update on source
  • auto build on image
  • auto recompose
#!/bin/sh
USER=<username on github>
REGISTRY=<username on docker hub>
UPSTREAM=<username on original source>
REPOSITORY=<the name of repository/project>
WORKDIR=/absolute/path/to/your/working/directory

eval `ssh-agent` && ssh-add -l
if grep -Fqe "Image is up to date" << EOF
`docker pull $REGISTRY/$REPOSITORY`
EOF
then
    cd $WORKDIR && rm -rf $REPOSITORY
    git clone git@github.com:$USER/$REPOSITORY.git
    cd $REPOSITORY && git checkout master
    git remote add upstream git://github.com/$UPSTREAM/$REPOSITORY.git
    git fetch --prune upstream
    if [ `git rev-list HEAD...upstream/master --count` -eq 0 ]
    then
        echo "both image and source are the same, do nothing"
    else
        echo "update exist on source, do update!"
        #Do update on source and get auto build image on docker hub
        git reset --hard upstream/master
        git push origin master --force
    fi
    cd $WORKDIR && rm -rf $REPOSITORY
else
    echo "newest image exist, do update!"
    #do recompose based on newest image
    docker-compose remove --volumes
    docker-compose up -d
fi
eval `ssh-agent -k`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment