Skip to content

Commit

Permalink
Ensure a log output heading is always shown.
Browse files Browse the repository at this point in the history
Otherwise the log output (and timing information) of this plugin is
grouped into whatever previous heading existed; for elastic-stack that's
the S3 secrets heading.
  • Loading branch information
pda committed Nov 24, 2020
1 parent 0b50565 commit a1ca593
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions hooks/environment
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ function retry() {
# 'aws ecr get-login-password' was not available until v1.7.10 which
# was only released earlier that same month.
function login_using_aws_ecr_get_login() {
echo "~~~ Authenticating with AWS ECR :ecr: :docker:"

# bash 3.x compatible equivalent of mapfile;
# https://github.com/koalaman/shellcheck/wiki/SC2207
registry_ids=()
Expand Down Expand Up @@ -107,10 +109,9 @@ function login_using_aws_ecr_get_login() {
fi

if [[ ${#registry_ids[@]} -gt 0 ]] ; then
echo "~~~ Authenticating with AWS ECR to ${registry_ids[*]}"
# amend the ~~~ log heading with ^^^ to add the AWS account IDs
echo "^^^ Authenticating with AWS ECR for ${registry_ids[*]} :ecr: :docker:"
login_args+=("--registry-ids" "${registry_ids[@]}")
else
echo "~~~ Authenticating with AWS ECR"
fi

# shellcheck disable=SC2068
Expand All @@ -123,6 +124,7 @@ function login_using_aws_ecr_get_login() {
}

function login_using_aws_ecr_get_login_password() {
echo "~~~ Authenticating with AWS ECR :ecr: :docker:"
local region="${BUILDKITE_PLUGIN_ECR_REGISTRY_REGION:-${BUILDKITE_PLUGIN_ECR_REGION:-${AWS_DEFAULT_REGION:-}}}"
if [[ -z $region ]]; then
region="us-east-1"
Expand All @@ -140,6 +142,8 @@ function login_using_aws_ecr_get_login_password() {
echo >&2 "AWS account ID required via plugin config or 'aws sts get-caller-identity'"
exit 1
fi
# amend the ~~~ log heading with ^^^ to add the AWS account IDs
echo "^^^ Authenticating with AWS ECR in $region for ${account_ids[*]} :ecr: :docker:"
local password; password="$(aws --region "$region" ecr get-login-password)"
for account_id in "${account_ids[@]}"; do
docker login --username AWS --password-stdin "$account_id.dkr.ecr.$region.amazonaws.com" <<< "$password"
Expand Down
5 changes: 5 additions & 0 deletions tests/run.bats
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ load '/usr/local/lib/bats/load.bash'
run "$PWD/hooks/environment"

assert_success
assert_output --partial "~~~ Authenticating with AWS ECR :ecr: :docker:"
assert_output --partial "^^^ Authenticating with AWS ECR in ap-southeast-2 for 321321321321 :ecr: :docker:"
assert_output --partial "logging in to docker"
[[ $(cat /tmp/password-stdin) == "hunter2" ]]

Expand Down Expand Up @@ -197,6 +199,7 @@ load '/usr/local/lib/bats/load.bash'
run "$PWD/hooks/environment"

assert_success
assert_output --partial "~~~ Authenticating with AWS ECR :ecr: :docker:"
assert_output --partial "logging in to docker"

unstub aws
Expand Down Expand Up @@ -236,6 +239,8 @@ load '/usr/local/lib/bats/load.bash'
run "$PWD/hooks/environment"

assert_success
assert_output --partial "~~~ Authenticating with AWS ECR :ecr: :docker:"
assert_output --partial "^^^ Authenticating with AWS ECR for 1111 2222 :ecr: :docker:"
assert_output --partial "logging in to docker"

unstub aws
Expand Down

0 comments on commit a1ca593

Please sign in to comment.