|
13 | 13 | set -x
|
14 | 14 | set -e
|
15 | 15 |
|
| 16 | +IMAGE="opengrok/docker" |
| 17 | + |
16 | 18 | if [[ -n $TRAVIS_TAG ]]; then
|
17 | 19 | VERSION="$TRAVIS_TAG"
|
18 | 20 | VERSION_SHORT=$( echo $VERSION | cut -d. -f1,2 )
|
|
33 | 35 |
|
34 | 36 | # Build the image.
|
35 | 37 | docker build \
|
36 |
| - -t opengrok/docker:$VERSION \ |
37 |
| - -t opengrok/docker:$VERSION_SHORT \ |
38 |
| - -t opengrok/docker:latest . |
| 38 | + -t $IMAGE:$VERSION \ |
| 39 | + -t $IMAGE:$VERSION_SHORT \ |
| 40 | + -t $IMAGE:latest . |
39 | 41 |
|
40 | 42 | #
|
41 | 43 | # Run the image in container. This is not strictly needed however
|
42 | 44 | # serves as additional test in automatic builds.
|
43 | 45 | #
|
44 |
| -docker run -d opengrok/docker |
| 46 | +docker run -d $IMAGE |
45 | 47 | docker ps -a
|
46 | 48 |
|
47 | 49 | # Travis can only work on master since it needs encrypted variables.
|
48 | 50 | if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then
|
49 |
| - echo "Not publishing docker image for pull requests" |
| 51 | + echo "Not pushing Docker image for pull requests" |
50 | 52 | exit 0
|
51 | 53 | fi
|
52 | 54 |
|
53 | 55 | # The push only works on the main repository.
|
54 | 56 | if [[ "${TRAVIS_REPO_SLUG}" != "oracle/opengrok" ]]; then
|
55 |
| - echo "Not publishing docker image for non main repository" |
| 57 | + echo "Not pushing Docker image for non main repository" |
56 | 58 | exit 0
|
57 | 59 | fi
|
58 | 60 |
|
59 |
| -# Allow Docker publish for release builds only. |
| 61 | +# Allow Docker push for release builds only. |
60 | 62 | if [[ -z $TRAVIS_TAG ]]; then
|
61 | 63 | echo "TRAVIS_TAG is empty"
|
62 | 64 | exit 0
|
|
74 | 76 |
|
75 | 77 | # Publish the image to Docker hub.
|
76 | 78 | if [ -n "$DOCKER_PASSWORD" -a -n "$DOCKER_USERNAME" -a -n "$VERSION" ]; then
|
77 |
| - echo "Logging into docker" |
| 79 | + echo "Logging into Docker Hub" |
78 | 80 | echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
|
79 | 81 |
|
80 | 82 | # All the tags need to be pushed individually:
|
81 | 83 | for tag in $VERSION $VERSION_SHORT latest; do
|
82 |
| - echo "Pushing docker image for tag $tag" |
83 |
| - docker push opengrok/docker:$tag |
| 84 | + echo "Pushing Docker image for tag $tag" |
| 85 | + docker push $IMAGE:$tag |
84 | 86 | done
|
85 | 87 | fi
|
| 88 | + |
| 89 | +# Update README file in Docker hub. |
| 90 | +push_readme() { |
| 91 | + declare -r image="${1}" |
| 92 | + declare -r token="${2}" |
| 93 | + declare -r input_file="${3}" |
| 94 | + |
| 95 | + if [[ ! -r $input_file ]]; then |
| 96 | + echo "file $input_file is not readable" |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | + |
| 100 | + local code=$(jq -n --arg msg "$(<${input_file})" \ |
| 101 | + '{"registry":"registry-1.docker.io","full_description": $msg }' | \ |
| 102 | + curl -s -o /dev/null -L -w "%{http_code}" \ |
| 103 | + https://cloud.docker.com/v2/repositories/"${image}"/ \ |
| 104 | + -d @- -X PATCH \ |
| 105 | + -H "Content-Type: application/json" \ |
| 106 | + -H "Authorization: JWT ${token}") |
| 107 | + |
| 108 | + if [[ "${code}" = "200" ]]; then |
| 109 | + echo "Successfully pushed README to Docker Hub" |
| 110 | + else |
| 111 | + printf "Unable to push README to Docker Hub, response code: %s\n" "${code}" |
| 112 | + exit 1 |
| 113 | + fi |
| 114 | +} |
| 115 | + |
| 116 | +TOKEN=$(curl -s -H "Content-Type: application/json" -X POST \ |
| 117 | + -d '{"username": "'${DOCKER_USERNAME}'", "password": "'${DOCKER_PASSWORD}'"}' \ |
| 118 | + https://hub.docker.com/v2/users/login/ | jq -r .token) |
| 119 | +if [[ -z $TOKEN ]]; then |
| 120 | + echo "Cannot get auth token to publish the README file" |
| 121 | + exit 1 |
| 122 | +fi |
| 123 | + |
| 124 | +push_readme "${IMAGE}" "${TOKEN}" "docker/README.md" |
0 commit comments