Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add sync-readme-to-ecr-public.sh script
  • Loading branch information
cjerad committed Oct 5, 2022
commit 890f7b0f0282f5ec0512c259c8042bdb42362a1f
21 changes: 17 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ KODATA = \
cmd/webhook/kodata/HEAD \
cmd/webhook/kodata/refs
CODECOVERAGE_OUT = $(PROJECT_DIR)/coverprofile.out
GITHUB_REPO_FULL_NAME = "aws/aws-node-termination-handler"
ECR_PUBLIC_REGISTRY ?= "public.ecr.aws/aws-ec2"
ECR_PUBLIC_REPOSITORY_ROOT = "aws-node-termination-handler-2"
GITHUB_REPO_FULL_NAME = aws/aws-node-termination-handler
ECR_PUBLIC_REGION = us-east-1
ECR_PUBLIC_REGISTRY ?= public.ecr.aws/aws-ec2
ECR_PUBLIC_REPOSITORY_ROOT = aws-node-termination-handler-2

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
Expand Down Expand Up @@ -129,10 +130,14 @@ apply: $(KO) $(KODATA) ## Deploy the controller into the current kubernetes clus
delete: ## Delete controller from current kubernetes cluster.
helm uninstall dev --namespace ${CLUSTER_NAMESPACE}

.PHONY: ecr-login
ecr-login: ## Login to default AWS ECR repository.
@$(PROJECT_DIR)/scripts/docker-login-ecr.sh

##@ Release

.PHONY: build-and-push-images
build-and-push-images: $(KO) $(KODATA) ## Build controller and webhook images and push to ECR public repository.
build-and-push-images: $(KO) $(KODATA) ecr-public-login ## Build controller and webhook images and push to ECR public repository.
@PATH="$(BIN_DIR):$(PATH)" $(PROJECT_DIR)/scripts/build-and-push-images.sh -r "$(ECR_PUBLIC_REGISTRY)/$(ECR_PUBLIC_REPOSITORY_ROOT)"

.PHONY: create-release-prep-pr
Expand All @@ -143,6 +148,10 @@ create-release-prep-pr: $(GUM) ## Update version numbers in documents and open a
create-release-prep-pr-draft: $(GUM) ## Update version numbers in documents and open a draft PR.
@PATH="$(BIN_DIR):$(PATH)" $(PROJECT_DIR)/scripts/prepare-for-release.sh -d

.PHONY: ecr-public-login
ecr-public-login: ## Login to the AWS ECR public repository.
@$(PROJECT_DIR)/scripts/docker-login-ecr.sh -g "$(ECR_PUBLIC_REGION)" -r "$(ECR_PUBLIC_REGISTRY)/$(ECR_PUBLIC_REPOSITORY_ROOT)"

.PHONY: upload-resources-to-github
upload-resources-to-github: ## Upload contents of resources/ as part of the most recent published release.
@$(PROJECT_DIR)/scripts/upload-resources-to-github.sh
Expand All @@ -159,5 +168,9 @@ repo-full-name: ## Get the full name of the GitHub repository for Node Terminati
ekscharts-sync-release: $(GH)
@PATH="$(BIN_DIR):$(PATH)" $(PROJECT_DIR)/scripts/sync-to-aws-eks-charts.sh -n

.PHONY: sync-readme-to-ecr-public
sync-readme-to-ecr-public: ecr-public-login ## Upload the README.md to ECR public controller and webhook repositories.
@$(PROJECT_DIR)/scripts/sync-readme-to-ecr-public.sh -r "$(ECR_PUBLIC_REGISTRY)/$(ECR_PUBLIC_REPOSITORY_ROOT)"

.PHONY: version
version: latest-release-tag ## Get the most recent release version.
55 changes: 52 additions & 3 deletions scripts/docker-login-ecr.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,57 @@
#!/usr/bin/env bash

aws ecr get-login-password \
--region "${AWS_REGION:?AWS_REGION is undefined or empty}" | \
set -euo pipefail

aws_region="${AWS_REGION:-}"
ecr_repository="${KO_DOCKER_REPO:-}"

usage=$(cat << EOM
usage: $(basename $0) -h | [-g REGION] [-p] [-r REPOSITORY]

Login to an AWS ECR repository.

Options:
-h Display this help message then exit.
-g REGION AWS Region. Defaults to "${aws_region}".
-r REPOSITORY ECR Registry. Defaults to "${ecr_repository}".

EOM
)

while getopts "g:r:h" opt; do
case "${opt}" in
g ) aws_region="${OPTARG}"
;;
r ) ecr_repository="${OPTARG}"
;;
h ) echo "${usage}"
exit 0
;;
\?) echo "${usage}" >&2
exit 1
;;
esac
done

function assert_not_empty {
if [[ -z "${!1}" ]]; then
echo "error: missing argument ${1}" >&2
echo "${usage}" >&2
exit 1
fi
}

assert_not_empty aws_region
assert_not_empty ecr_repository

ecr_cmd="ecr"
if echo "${ecr_repository}" | grep '^public\.ecr\.aws' >/dev/null; then
ecr_cmd="ecr-public"
fi

aws ${ecr_cmd} get-login-password \
--region "${aws_region}" | \
docker login \
--username AWS \
--password-stdin \
"${KO_DOCKER_REPO:?KO_DOCKER_REPO is undefined or empty}"
"${ecr_repository}"
91 changes: 91 additions & 0 deletions scripts/sync-readme-to-ecr-public.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bash

set -euo pipefail

repo_root_path="$(cd "$(dirname "$0")"; cd ..; pwd -P)"

if ! command -v jq; then
echo "command not found: jq" >&2
exit 1
fi

region="${AWS_REGION:-us-east-1}"
repo_root="${KO_DOCKER_REPO}"
version="$(make -s -f "${makefile_path}" version)"

usage=$(cat << EOM
usage: $(basename $0) -h | [-g REGION] [-r REPOSITORY] [-v VERSION]
Upload the README.md to the AWS ECR controller and webhook repositories.
Options:
-h Display this help message then exit.
-g REGION AWS Region of ECR image repository. Defaults to "${region}".
-r REPOSITORY Image repository to push the built images. Defaults to "${repo_root}".
-v VERSION Node Termination Handler version. Defaults to "${version}".
EOM
)

while getopts "r:h" opt; do
case "${opt}" in
g ) region="${OPTARG}"
;;
r ) repo_root="${OPTARG}"
;;
v ) version="${OPTARG}"
;;
h ) echo "${usage}"
exit 0
;;
\?) echo "${usage}" >&2
exit 1
;;
esac
done

assert_not_empty() {
if [[ -z "${!1}" ]]; then
echo "error: missing argument ${1}" >&2
echo "${usage}" >&2
exit 1
fi
}

assert_not_empty region
assert_not_empty repo_root
assert_not_empty version

#################################################

if ! (git --no-pager diff --name-only HEAD^ HEAD | grep 'README.md' >/dev/null); then
echo -e "⚠️ README.md did not change in the last commit. Not taking any action."
exit 0
fi

#################################################

content=$(jq -n --arg msg "$(cat README.md)" '{"usageText": $msg}' | jq '.usageText' | sed 's/\\n/\
/g')
if [[ ${#content} -gt 10240 ]]; then
truncation_msg="...
**truncated due to char limits**...
A complete version of the README can be found [here](https://github.com/aws/aws-node-termination-handler/blob/${version}/README.md).\""
content="${content:0:$((10240-${#truncation_msg}))}"
content+="${truncation_msg}"
fi

for repo in "controller" "webhook"; do
if ! aws ecr-public put-repository-catalog-data \
--region "${region}" \
--repository-name="${repo_root}/${repo}" \
--catalog-data aboutText="${content}",usageText="See About section" >/dev/null 2>&1; then
echo -e "❌ Failed to upload README.md to ${repo_root}/${repo}" >&2
exit 1
fi

echo -e "✅ Uploaded README.md to 'About' section of ${repo_root}/${repo}"
done

echo -e "✅ Finished sync'ing README.md to ECR Public"