Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add script to automatically publish readme to docker hub #6118

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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} =============="

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: enclose in echo group as below, to collapse the logs

fi
echo "::endgroup::"

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"
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
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: the dedicated README file for Docker image $repo was not 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 \
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
--arg full_desc "$readme_content" \
'{full_description: $full_desc}')
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved

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
Loading