Skip to content

Commit

Permalink
feat: automatically publish readme to docker hub (jaegertracing#6118)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
Fixes: jaegertracing#3842 

## Description of the changes
- Added a shell script that publishes README.md files along with docker
images to docker hub

## How was this change tested?
- Locally as well as on fork

## Checklist
- [x] I have read
https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md
- [x] I have signed all commits
- [ ] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - for `jaeger`: `make lint test`

---------

Signed-off-by: Meet Soni <[email protected]>
Signed-off-by: Yuri Shkuro <[email protected]>
Co-authored-by: Yuri Shkuro <[email protected]>
  • Loading branch information
inosmeet and yurishkuro authored Oct 26, 2024
1 parent f5575e2 commit 85d558d
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
8 changes: 8 additions & 0 deletions scripts/build-upload-a-docker-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ local_test_only='N'
platforms="linux/amd64"
namespace="jaegertracing"
overwrite='N'
upload_readme='N'

while getopts "bc:d:f:hlop:t:" opt; do
# shellcheck disable=SC2220 # we don't need a *) case
Expand Down Expand Up @@ -107,6 +108,7 @@ else
if [[ "$overwrite" == 'N' ]]; then
check_overwrite "${IMAGE_TAGS[@]}"
fi
upload_readme='Y'
else
echo 'skipping docker images upload, because not on tagged release or main branch'
PUSHTAG="type=image,push=false"
Expand All @@ -127,6 +129,12 @@ docker buildx build --output "${PUSHTAG}" ${target_arg} ${base_debug_img_arg} \
echo "::endgroup::"
echo "Finished building${upload_comment} ${component_name} =============="

if [[ "$upload_readme" == "Y" ]]; then
echo "::group:: docker upload ${dir_arg}/README.md"
bash scripts/upload-docker-readme.sh "${component_name}" "${dir_arg}"/README.md
echo "::endgroup::"
fi

echo "::group:: docker prune"
df -h /
docker buildx prune --all --force
Expand Down
78 changes: 78 additions & 0 deletions scripts/upload-docker-readme.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash
# Copyright (c) 2024 The Jaeger Authors.
# SPDX-License-Identifier: Apache-2.0

set -euxf -o pipefail

usage() {
echo "Usage: $0 <repository_name> <file_path>"
exit 1
}

if [ "$#" -ne 2 ]; then
echo "Error: Missing arguments."
usage
fi

repo="$1"
readme_path="$2"
abs_readme_path=$(realpath "$readme_path")
repository="jaegertracing/$repo"

DOCKERHUB_TOKEN=${DOCKERHUB_TOKEN:?'missing Docker Hub token'}
QUAY_TOKEN=${QUAY_TOKEN:?'missing Quay token'}

dockerhub_url="https://hub.docker.com/v2/repositories/$repository/"
quay_url="https://quay.io/api/v1/repository/${repository}"

if [ ! -f "$abs_readme_path" ]; then
echo "❗Warning: no README file found at path $abs_readme_path"
echo "It is recommended to have a dedicated README file for each Docker image"
exit 0
fi

readme_content=$(<"$abs_readme_path")

set +x

# Handling DockerHUB upload
# encode readme as properly escaped JSON
body=$(jq -n \
--arg full_desc "$readme_content" \
'{full_description: $full_desc}')

dockerhub_response=$(curl -s -w "%{http_code}" -X PATCH "$dockerhub_url" \
-H "Content-Type: application/json" \
-H "Authorization: JWT $DOCKERHUB_TOKEN" \
-d "$body")

http_code="${dockerhub_response: -3}"
response_body="${dockerhub_response:0:${#dockerhub_response}-3}"

if [ "$http_code" -eq 200 ]; then
echo "Successfully updated Docker Hub README for $repository"
else
echo "🛑 Failed to update Docker Hub README for $repository with status code $http_code"
echo "Full response: $response_body"
fi

# Handling Quay upload
# encode readme as properly escaped JSON
quay_body=$(jq -n \
--arg full_desc "$readme_content" \
'{description: $full_desc}')

quay_response=$(curl -s -w "%{http_code}" -X PUT "$quay_url" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $QUAY_TOKEN" \
-d "$quay_body")

quay_http_code="${quay_response: -3}"
quay_response_body="${quay_response:0:${#quay_response}-3}"

if [ "$quay_http_code" -eq 200 ]; then
echo "Successfully updated Quay.io README for $repository"
else
echo "Failed to update Quay.io README for $repository with status code $quay_http_code"
echo "Full response: $quay_response_body"
fi
42 changes: 42 additions & 0 deletions scripts/upload-docker-readme.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash

# Copyright (c) 2024 The Jaeger Authors.
# SPDX-License-Identifier: Apache-2.0

set -euo pipefail

# Mock variables
dockerhub_repository="jaegertracing/test-docker"
http_code=""
response_body=""

simulate_update() {
local status=$1
if [ "$status" -eq 200 ]; then
http_code=200
response_body="Successfully updated README."
else
http_code="$status"
response_body="Error: Unable to update README."
fi
}

# Test 1: Successful update
echo "Running test: Successful update"
simulate_update 200
if [ "$http_code" -eq 200 ]; then
echo "Successfully updated Docker Hub README for $dockerhub_repository"
else
echo "Failed to update Docker Hub README for $dockerhub_repository with status code $http_code"
echo "Full response: $response_body"
fi

# Test 2: Failed update
echo "Running test: Failed update"
simulate_update 403
if [ "$http_code" -eq 200 ]; then
echo "Successfully updated Docker Hub README for $dockerhub_repository"
else
echo "Failed to update Docker Hub README for $dockerhub_repository with status code $http_code"
echo "Full response: $response_body"
fi

0 comments on commit 85d558d

Please sign in to comment.