Skip to content

Commit 8925909

Browse files
author
Vladimir Kotal
authored
refresh repository README on Docker hub after image push (#2868)
1 parent 5e0c6b2 commit 8925909

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

dev/before_install

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
2020
python3 \
2121
python3-venv \
2222
python3-pip \
23-
nodejs
23+
nodejs \
24+
jq
2425
if [[ $? != 0 ]]; then
2526
echo "cannot install extra packages"
2627
exit 1
@@ -37,7 +38,7 @@ if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
3738
elif [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
3839
brew update
3940

40-
brew install cvs libgit2
41+
brew install cvs libgit2 jq
4142
if [[ $? != 0 ]]; then
4243
echo "cannot install extra packages"
4344
exit 1

dev/docker.sh

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
set -x
1414
set -e
1515

16+
IMAGE="opengrok/docker"
17+
1618
if [[ -n $TRAVIS_TAG ]]; then
1719
VERSION="$TRAVIS_TAG"
1820
VERSION_SHORT=$( echo $VERSION | cut -d. -f1,2 )
@@ -33,30 +35,30 @@ fi
3335

3436
# Build the image.
3537
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 .
3941

4042
#
4143
# Run the image in container. This is not strictly needed however
4244
# serves as additional test in automatic builds.
4345
#
44-
docker run -d opengrok/docker
46+
docker run -d $IMAGE
4547
docker ps -a
4648

4749
# Travis can only work on master since it needs encrypted variables.
4850
if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then
49-
echo "Not publishing docker image for pull requests"
51+
echo "Not pushing Docker image for pull requests"
5052
exit 0
5153
fi
5254

5355
# The push only works on the main repository.
5456
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"
5658
exit 0
5759
fi
5860

59-
# Allow Docker publish for release builds only.
61+
# Allow Docker push for release builds only.
6062
if [[ -z $TRAVIS_TAG ]]; then
6163
echo "TRAVIS_TAG is empty"
6264
exit 0
@@ -74,12 +76,49 @@ fi
7476

7577
# Publish the image to Docker hub.
7678
if [ -n "$DOCKER_PASSWORD" -a -n "$DOCKER_USERNAME" -a -n "$VERSION" ]; then
77-
echo "Logging into docker"
79+
echo "Logging into Docker Hub"
7880
echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
7981

8082
# All the tags need to be pushed individually:
8183
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
8486
done
8587
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

Comments
 (0)