1#!/bin/bash 2 3# 4# Build and optionally push new image to Docker hub. 5# 6# When pushing, this script uses the following secure variables: 7# - DOCKER_USERNAME 8# - DOCKER_PASSWORD 9# 10# These are set via https://github.com/oracle/opengrok/settings/secrets 11# 12 13set -e 14 15# Update README file in Docker hub. 16push_readme() { 17 declare -r image="${1}" 18 declare -r token="${2}" 19 declare -r input_file="${3}" 20 21 if [[ ! -r $input_file ]]; then 22 echo "file $input_file is not readable" 23 exit 1 24 fi 25 26 local code=$(curl -s -o /dev/null -L -w "%{http_code}" \ 27 -X PATCH --data-urlencode \ 28 full_description@${input_file} \ 29 -H "Authorization: JWT ${token}" \ 30 ${API_URL}/repositories/"${image}"/) 31 32 if [[ "${code}" = "200" ]]; then 33 echo "Successfully pushed README to Docker Hub" 34 else 35 printf "Unable to push README to Docker Hub, response code: %s\n" "${code}" 36 exit 1 37 fi 38} 39 40echo "Running linter" 41docker run --rm -i hadolint/hadolint:2.6.0 < Dockerfile || exit 1 42 43API_URL="https://hub.docker.com/v2" 44IMAGE="opengrok/docker" 45 46if [[ -n $OPENGROK_REF && $OPENGROK_REF == refs/tags/* ]]; then 47 OPENGROK_TAG=${OPENGROK_REF#"refs/tags/"} 48fi 49 50if [[ -n $OPENGROK_TAG ]]; then 51 VERSION="$OPENGROK_TAG" 52 VERSION_SHORT=$( echo $VERSION | cut -d. -f1,2 ) 53 54 if [[ -z $VERSION ]]; then 55 echo "empty VERSION" 56 exit 1 57 fi 58 59 if [[ -z $VERSION_SHORT ]]; then 60 echo "empty VERSION_SHORT" 61 exit 1 62 fi 63 64 echo "Version: $VERSION" 65 echo "Short version: $VERSION_SHORT" 66 67 TAGS="$VERSION $VERSION_SHORT latest" 68 69 echo "Building docker image for release ($TAGS)" 70 docker build \ 71 -t $IMAGE:$VERSION \ 72 -t $IMAGE:$VERSION_SHORT \ 73 -t $IMAGE:latest . 74else 75 TAGS="master" 76 77 echo "Building docker image for master" 78 docker build -t $IMAGE:master . 79fi 80 81# 82# Run the image in a container. This is not strictly needed however 83# serves as additional test in automatic builds. 84# 85echo "Running the image in container" 86docker run -d $IMAGE 87docker ps -a 88 89# This can only work on home repository since it needs encrypted variables. 90if [[ -n "$OPENGROK_PULL_REQUEST" ]]; then 91 echo "Not pushing Docker image for pull requests" 92 exit 0 93fi 94 95# The push only works on the main repository. 96if [[ "$OPENGROK_REPO_SLUG" != "oracle/opengrok" ]]; then 97 echo "Not pushing Docker image for non main repository" 98 exit 0 99fi 100 101if [[ -z $DOCKER_USERNAME ]]; then 102 echo "DOCKER_USERNAME is empty, exiting" 103 exit 0 104fi 105 106if [[ -z $DOCKER_PASSWORD ]]; then 107 echo "DOCKER_PASSWORD is empty, exiting" 108 exit 0 109fi 110 111# Publish the image to Docker hub. 112if [ -n "$DOCKER_PASSWORD" -a -n "$DOCKER_USERNAME" -a -n "$TAGS" ]; then 113 echo "Logging into Docker Hub" 114 echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin 115 116 # All the tags need to be pushed individually: 117 for tag in $TAGS; do 118 echo "Pushing Docker image for tag $tag" 119 docker push $IMAGE:$tag 120 done 121fi 122 123# Update README and badge only for release builds. 124if [[ -n $OPENGROK_TAG ]]; then 125 TOKEN=$(curl -s -H "Content-Type: application/json" -X POST \ 126 -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PASSWORD}'"}' \ 127 ${API_URL}/users/login/ | jq -r .token) 128 if [[ -z $TOKEN ]]; then 129 echo "Cannot get auth token to publish the README file" 130 exit 1 131 fi 132 133 push_readme "${IMAGE}" "${TOKEN}" "docker/README.md" 134fi 135