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

Target ECR registry by Account ID and Region #20

Merged
merged 5 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ steps:
ecr-name: my-repo
```

We can target registries in other accounts and regions, provided the current IAM user/role has the ability to auth against said account/registry:

```yaml
steps:
- plugins:
- seek-oss/docker-ecr-publish#v1.5.0:
account_id: '12345678910'
region: eu-west-1
ecr-name: my-repo
```

## Configuration

- `args` (optional, array|string)
Expand Down Expand Up @@ -196,6 +207,14 @@ steps:

Name of the ECR repository.

- `account_id` (optional, string)

Account ID for ECR registry, defaults to output of `aws sts get-caller-identity` e.g. current account ID.

- `region` (optional, string)

Region the ECR registry is in, defaults to `$AWS_DEFAULT_REGION` and then `eu-west-1` if not set.

- `tags` (optional, array|string)

Tags to push on all builds.
Expand Down
33 changes: 31 additions & 2 deletions hooks/command
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ set -euo pipefail

get_ecr_url() {
local repository_name="${1}"
local registry_id="${2}"
local region="${3}"

aws ecr describe-repositories \
--region "${region}" \
--repository-names "${repository_name}" \
--registry-id "${registry_id}" \
--output text \
--query 'repositories[0].repositoryUri'
}
Expand Down Expand Up @@ -91,8 +95,33 @@ push_tags() {
done
}

ecr_login() {
local region="${1}"
local account_id="${2}"

aws ecr get-login-password \
Copy link
Member

Choose a reason for hiding this comment

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

👏

--region "${region}" \
| docker login \
--username AWS \
--password-stdin "${account_id}".dkr.ecr."${region}".amazonaws.com
}

echo '--- Logging in to ECR'
$(aws ecr get-login --no-include-email)

if [[ -n ${BUILDKITE_PLUGIN_DOCKER_ECR_PUBLISH_REGION:-} ]]; then
region="${BUILDKITE_PLUGIN_DOCKER_ECR_PUBLISH_REGION}"
else
region="${AWS_DEFAULT_REGION:-eu-west-1}"
fi


if [[ -n ${BUILDKITE_PLUGIN_DOCKER_ECR_PUBLISH_ACCOUNT_ID:-} ]]; then
account_id="${BUILDKITE_PLUGIN_DOCKER_ECR_PUBLISH_ACCOUNT_ID}"
else
account_id="$(aws sts get-caller-identity --output text | cut -f1)"
fi

ecr_login "$region" "$account_id"

echo '--- Reading plugin parameters'

Expand All @@ -105,7 +134,7 @@ if [[ -z ${BUILDKITE_PLUGIN_DOCKER_ECR_PUBLISH_ECR_NAME:-} ]]; then
exit 1
fi

image="$(get_ecr_url "${BUILDKITE_PLUGIN_DOCKER_ECR_PUBLISH_ECR_NAME}")"
image="$(get_ecr_url "${BUILDKITE_PLUGIN_DOCKER_ECR_PUBLISH_ECR_NAME}" "${account_id}" "${region}")"

build_args=()
caches_from=()
Expand Down
4 changes: 4 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ configuration:
type: string
ecr-name:
type: string
account-id:
type: string
region:
type: string
tags:
type: [array, string]
target:
Expand Down