|
| 1 | +#!/usr/bin/env bash |
| 2 | +# set -o pipefail # exit if pipe command fails |
| 3 | +[ -z "$DEBUG" ] || set -x |
| 4 | +set -e |
| 5 | + |
| 6 | +## |
| 7 | + |
| 8 | +NAME="dozzle" |
| 9 | +DOCKER_TAG="jriguera/$NAME" |
| 10 | +RELEASE="dozzle" |
| 11 | +DESCRIPTION="Docker image to run Dozzle in a Raspberry Pi. Simple logging for Docker." |
| 12 | +GITHUB_REPO="jriguera/docker-rpi-dozzle" |
| 13 | + |
| 14 | +### |
| 15 | + |
| 16 | +DOCKER="docker" |
| 17 | +JQ="jq" |
| 18 | +CURL="curl -s" |
| 19 | +RE_VERSION_NUMBER='^[0-9]+([0-9\.]*[0-9]+)*$' |
| 20 | + |
| 21 | +### |
| 22 | + |
| 23 | +ARCH="" |
| 24 | +case "$(uname -m)" in |
| 25 | + armv7l) |
| 26 | + ARCH='arm32v6' |
| 27 | + ;; |
| 28 | + x86_64|amd64) |
| 29 | + ARCH='amd64' |
| 30 | + ;; |
| 31 | + *) |
| 32 | + echo "ERROR: unsupported architecture: $(uname -m)" |
| 33 | + exit 1 |
| 34 | + ;; |
| 35 | +esac |
| 36 | + |
| 37 | +VERSION="" |
| 38 | +case $# in |
| 39 | + 0) |
| 40 | + echo "*** Creating a new release. Automatically calculating version number" |
| 41 | + ;; |
| 42 | + 1) |
| 43 | + if [ $1 == "-h" ] || [ $1 == "--help" ] |
| 44 | + then |
| 45 | + echo "Usage: $0 [version-number]" |
| 46 | + echo " Creates a release, commits the changes to this repository using tags and uploads " |
| 47 | + echo " the release to Github Releases and the final Docker image to Docker Hub. " |
| 48 | + echo " It also adds comments based on previous git commits." |
| 49 | + exit 0 |
| 50 | + else |
| 51 | + VERSION=$1 |
| 52 | + if ! [[ $VERSION =~ $RE_VERSION_NUMBER ]] |
| 53 | + then |
| 54 | + echo "ERROR: Incorrect version number!" |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | + echo "*** Creating a new release. Using release version number $VERSION." |
| 58 | + fi |
| 59 | + ;; |
| 60 | + *) |
| 61 | + echo "ERROR: incorrect argument. See '$0 --help'" |
| 62 | + exit 1 |
| 63 | + ;; |
| 64 | +esac |
| 65 | + |
| 66 | +# Create a personal github token to use this script |
| 67 | +if [ -z "$GITHUB_TOKEN" ] |
| 68 | +then |
| 69 | + echo "Github TOKEN not defined!" |
| 70 | + echo "See https://help.github.com/articles/creating-an-access-token-for-command-line-use/" |
| 71 | + exit 1 |
| 72 | +fi |
| 73 | + |
| 74 | +# You need bosh installed and with you credentials |
| 75 | +if ! [ -x "$(command -v $DOCKER)" ] |
| 76 | +then |
| 77 | + echo "ERROR: $DOCKER command not found! Please install it and make it available in the PATH" |
| 78 | + exit 1 |
| 79 | +fi |
| 80 | + |
| 81 | +# You need jq installed |
| 82 | +if ! [ -x "$(command -v $JQ)" ] |
| 83 | +then |
| 84 | + echo "ERROR: $JQ command not found! Please install it and make it available in the PATH" |
| 85 | + exit 1 |
| 86 | +fi |
| 87 | + |
| 88 | +DOCKER_USER=$(docker info 2> /dev/null | sed -ne 's/Username: \(.*\)/\1/p') |
| 89 | +if [ -z "$DOCKER_USER" ] |
| 90 | +then |
| 91 | + echo "ERROR: Not logged in Docker Hub!" |
| 92 | + echo "Please perform 'docker login' with your credentials in order to push images there." |
| 93 | + exit 1 |
| 94 | +fi |
| 95 | + |
| 96 | +# Creating the release |
| 97 | +if [ -z "$VERSION" ] |
| 98 | +then |
| 99 | + VERSION=$(sed -ne 's/^ARG.* VERSION=\(.*\)/\1/p' docker/Dockerfile) |
| 100 | + MYVERSION=$(sed -ne 's/^ARG.* MYVERSION=\(.*\)/\1/p' docker/Dockerfile) |
| 101 | + [ -n "$MYVERSION" ] && VERSION="$VERSION-$MYVERSION" |
| 102 | + echo "* Creating final release version $VERSION (from Dockerfile) ..." |
| 103 | +else |
| 104 | + echo "* Creating final release version $VERSION (from input)..." |
| 105 | +fi |
| 106 | + |
| 107 | +# Get the last git commit made by this script |
| 108 | +LASTCOMMIT=$(git show-ref --tags -d | tail -n 1) |
| 109 | +if [ -z "$LASTCOMMIT" ] |
| 110 | +then |
| 111 | + echo "* Changes since the beginning: " |
| 112 | + CHANGELOG=$(git log --pretty="%h %aI %s (%an)" | sed 's/^/- /') |
| 113 | +else |
| 114 | + echo "* Changes since last version with commit $LASTCOMMIT: " |
| 115 | + CHANGELOG=$(git log --pretty="%h %aI %s (%an)" "$(echo $LASTCOMMIT | cut -d' ' -f 1)..@" | sed 's/^/- /') |
| 116 | +fi |
| 117 | +if [ -z "$CHANGELOG" ] |
| 118 | +then |
| 119 | + echo "ERROR: no commits since last release with commit $LASTCOMMIT!. Please " |
| 120 | + echo "commit your changes to create and publish a new release!" |
| 121 | + exit 1 |
| 122 | +fi |
| 123 | +echo "$CHANGELOG" |
| 124 | + |
| 125 | +pushd docker |
| 126 | + echo "* Building Docker image with tag $NAME:$VERSION ..." |
| 127 | + $DOCKER build \ |
| 128 | + --build-arg ARCH=${ARCH} \ |
| 129 | + --build-arg TZ=$(timedatectl | awk '/Time zone:/{ print $3 }') \ |
| 130 | + . -t $NAME |
| 131 | + $DOCKER tag $NAME $DOCKER_TAG |
| 132 | + |
| 133 | + # Uploading docker image |
| 134 | + echo "* Pusing Docker image to Docker Hub ..." |
| 135 | + $DOCKER push $DOCKER_TAG |
| 136 | + $DOCKER tag $NAME $DOCKER_TAG:$VERSION |
| 137 | + $DOCKER push $DOCKER_TAG |
| 138 | + |
| 139 | + $DOCKER save -o "/tmp/$NAME-$VERSION.tgz" $DOCKER_TAG:$VERSION |
| 140 | +popd |
| 141 | + |
| 142 | +# Create annotated tag |
| 143 | +echo "* Creating a git tag ... " |
| 144 | +git tag -a "v$VERSION" -m "$RELEASE v$VERSION" |
| 145 | +git push --tags |
| 146 | + |
| 147 | +# Create a release in Github |
| 148 | +echo "* Creating a new release in Github ... " |
| 149 | +DESC=$(cat <<EOF |
| 150 | +# $RELEASE version $VERSION |
| 151 | +
|
| 152 | +$DESCRIPTION |
| 153 | +
|
| 154 | +## Changes since last version |
| 155 | +
|
| 156 | +$CHANGELOG |
| 157 | +
|
| 158 | +## Using it |
| 159 | +
|
| 160 | +Given the docker image with name 'dozzle': |
| 161 | +
|
| 162 | + docker run --name logging -p 8080:8080 -v /var/run/docker.sock:/var/run/docker.sock -d jriguera/dozzle |
| 163 | +
|
| 164 | +EOF |
| 165 | +) |
| 166 | +printf -v data '{"tag_name": "v%s","target_commitish": "master","name": "v%s","body": %s,"draft": false, "prerelease": false}' "$VERSION" "$VERSION" "$(echo "$DESC" | $JQ -R -s '@text')" |
| 167 | +releaseid=$($CURL -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/json" -XPOST --data "$data" "https://api.github.com/repos/$GITHUB_REPO/releases" | $JQ '.id') |
| 168 | +# Upload the release |
| 169 | +echo "* Uploading image to Github releases section ... " |
| 170 | +echo -n " URL: " |
| 171 | +$CURL -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/octet-stream" --data-binary @"/tmp/$NAME-$VERSION.tgz" "https://uploads.github.com/repos/$GITHUB_REPO/releases/$releaseid/assets?name=$NAME-$VERSION.tgz" | $JQ -r '.browser_download_url' |
| 172 | + |
| 173 | +echo |
| 174 | +echo "*** Description https://github.com/$GITHUB_REPO/releases/tag/v$VERSION: " |
| 175 | +echo |
| 176 | +echo "$DESC" |
| 177 | + |
| 178 | +# Delete the release |
| 179 | +rm -f "/tmp/$NAME-$VERSION.tgz" |
| 180 | +git fetch --tags |
| 181 | + |
| 182 | +exit 0 |
| 183 | + |
0 commit comments