Skip to content

Instantly share code, notes, and snippets.

@vutny
Created February 17, 2023 10:26
Show Gist options
  • Save vutny/95419acd44ebffd2ed852a1f2560ee4d to your computer and use it in GitHub Desktop.
Save vutny/95419acd44ebffd2ed852a1f2560ee4d to your computer and use it in GitHub Desktop.
Install Amazon ECR Docker Credential Helper on GNU/Linux x86_64/amd64
#!/usr/bin/env bash
# install-docker-credential-ecr-login.sh
#
# Install and configure AWS CLI v2 and the Amazon ECR Docker Credential Helper
# Exit immediately on error
set -o errexit
# All variables should be explicitly declared
set -o nounset
# Exit on error in a loop
set -o pipefail
# Debug
#set -o xtrace
APT_UPDATED=
AWS_CLI_URL='https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip'
AWS_ECR_CRED_HLPR_URL='https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com/0.4.0/linux-amd64/docker-credential-ecr-login'
DOCKER_CONFIG="$HOME/.docker/config.json"
TMPDIR=
__check_command_exists() {
command -v "$1" > /dev/null 2>&1
}
__exit_cleanup() {
EXIT_CODE=$?
# Cleanup temp dir if it was ever created
if [[ -n $TMPDIR ]]; then
rm --recursive --interactive=never "$TMPDIR"
fi
exit $EXIT_CODE
}
__make_temp_dir() {
TMPDIR=$(mktemp --directory)
# Bail out if the temp directory wasn't created successfully
if ! [[ -d $TMPDIR ]]; then
>&2 echo "Failed to create temp directory"
exit 1
fi
}
__apt_install_missing() {
if ! __check_command_exists "$1"; then
if [[ -z $APT_UPDATED ]]; then
$SUDO apt update
APT_UPDATED=yes
fi
$SUDO sh -c "DEBIAN_FRONTEND=noninteractive \
apt install -y -o DPkg::Options::=--force-confold $1"
fi
}
install_dependencies() {
__apt_install_missing unzip
__apt_install_missing wget
}
install_awscli() {
__check_command_exists aws && return 0
install_dependencies
__make_temp_dir
AWS_CLI_ARCHIVE="${TMPDIR}/${AWS_CLI_URL##*/}"
wget -q -O "$AWS_CLI_ARCHIVE" "$AWS_CLI_URL"
unzip -q -d "$TMPDIR" "$AWS_CLI_ARCHIVE"
$SUDO "${TMPDIR}/aws/install" >/dev/null
echo
echo 'AWS CLI installed successfully.'
}
configure_awscli() {
if ! [[ -f $HOME/.aws/credentials ]]; then
echo
aws configure
fi
}
install_credentials_helper() {
AWS_ECR_CRED_HLPR_BIN="/usr/local/bin/${AWS_ECR_CRED_HLPR_URL##*/}"
test -x "$AWS_ECR_CRED_HLPR_BIN" && return 0
__apt_install_missing wget
$SUDO wget -q -O "$AWS_ECR_CRED_HLPR_BIN" "$AWS_ECR_CRED_HLPR_URL"
$SUDO chmod +x "$AWS_ECR_CRED_HLPR_BIN"
}
configure_credentials_helper() {
__apt_install_missing jq
if ! [[ -f $DOCKER_CONFIG ]]; then
# shellcheck disable=SC2174
mkdir -m 700 -p "${DOCKER_CONFIG%/*}"
echo '{}' > "$DOCKER_CONFIG"
fi
CONFIG_JSON=$(jq -M --arg credsStore ecr-login \
'. + {credsStore: $credsStore}' < "$DOCKER_CONFIG")
echo "$CONFIG_JSON" >| "$DOCKER_CONFIG"
}
# Make sure to do cleanup even if the script exits abnormally
trap 'exit 1' HUP INT PIPE QUIT TERM
trap '__exit_cleanup' EXIT
# Use sudo when necessary
SUDO=sudo
if [[ $(id -u) == '0' ]] && ! __check_command_exists "$SUDO"; then
SUDO=''
fi
install_awscli
configure_awscli
install_credentials_helper
configure_credentials_helper
aws ecr describe-repositories > /dev/null
echo
echo "AWS ECR credentials helper for Docker is installed and configured."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment