Skip to content

Commit 3468d30

Browse files
[Feature Request]Add parameter for tagging ECR repo (#379)
* add job and command for tag ecr repo * cleanup * expand option description * add tests for tagging repo * Remove repo tag from some tests for extra validation * fix command * fix typo * Add some small fixes * improved docs and add test for empty tags * fix condition * Fix syntax for multiple repo tags * Update syntax on test for multiple tags * change test string * Add debug to tag_repo script * add account id to tag_repo command * changed variable name for account it * Remove debug --------- Co-authored-by: Mateo Arboleda <[email protected]>
1 parent 55c9e8b commit 3468d30

File tree

7 files changed

+102
-1
lines changed

7 files changed

+102
-1
lines changed

.circleci/test-deploy.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ jobs:
102102
workspace_root: << parameters.workspace_root >>
103103
repo: << parameters.repo >>
104104
create_repo: << parameters.create_repo >>
105+
repo_tag: Key=Env,Value=CITesting
105106
tag: << parameters.tag >>
106107
dockerfile: << parameters.dockerfile >>
107108
path: << parameters.path >>
@@ -118,6 +119,10 @@ jobs:
118119
path: << parameters.path >>
119120
platform: << parameters.platform >>
120121
push_image: false
122+
- aws-ecr/tag_repo:
123+
repo: << parameters.repo >>
124+
tag: '[{\"Key\": \"Validation\", \"Value\": \"CITesting\"}, {\"Key\": \"Validation2\", \"Value\": \"CITesting\"}]'
125+
region: << parameters.region >>
121126
- run:
122127
name: Tests for docker image
123128
command: |
@@ -173,6 +178,7 @@ workflows:
173178
workspace_root: workspace
174179
repo: aws-ecr-orb-${CIRCLE_SHA1:0:7}-multi-platform-without-push
175180
create_repo: true
181+
repo_tag: Key=Env,Value=CITesting
176182
context: [CPE-OIDC]
177183
tag: integration,myECRRepoTag
178184
dockerfile: sample/Dockerfile
@@ -198,6 +204,7 @@ workflows:
198204
workspace_root: workspace
199205
repo: aws-ecr-orb-${CIRCLE_SHA1:0:7}-default-profile
200206
create_repo: true
207+
repo_tag: Key=Env,Value=CITesting
201208
context: [CPE-OIDC]
202209
tag: integration,myECRRepoTag
203210
dockerfile: sample/Dockerfile
@@ -315,6 +322,7 @@ workflows:
315322
workspace_root: workspace
316323
repo: aws-ecr-orb-${CIRCLE_SHA1:0:7}-named-profile-<<matrix.use_credentials_helper>>
317324
create_repo: true
325+
repo_tag: Key=Env,Value=CITesting
318326
tag: integration,myECRRepoTag
319327
dockerfile: sample/Dockerfile
320328
path: workspace
@@ -380,6 +388,7 @@ workflows:
380388
workspace_root: workspace
381389
repo: aws-ecr-orb-${CIRCLE_SHA1:0:7}-skip_when_tags_exist-<<matrix.executor>>
382390
create_repo: true
391+
repo_tag: Key=Env,Value=CITesting
383392
tag: integration,myECRRepoTag
384393
dockerfile: Dockerfile
385394
path: ./sample

src/commands/build_and_push_image.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,13 @@ parameters:
227227
Defaults to qemu-v7.0.0-28, change only if you know what you are doing.
228228
See https://hub.docker.com/r/tonistiigi/binfmt for details.
229229
230+
repo_tag:
231+
type: string
232+
default: ""
233+
description: |
234+
A list of strings in json format, containing tags for repository.
235+
Shorthand Syntax: [{"Key": "FirstTag", "Value": "FirstValue"}]
236+
230237
steps:
231238
- when:
232239
condition: <<parameters.checkout>>
@@ -269,6 +276,7 @@ steps:
269276
repo_encryption_type: <<parameters.repo_encryption_type>>
270277
encryption_kms_key: <<parameters.repo_encryption_kms_key>>
271278
image_tag_mutability: <<parameters.repo_image_tag_mutability>>
279+
repo_tag: <<parameters.repo_tag>>
272280
- when:
273281
condition: <<parameters.set_repo_policy>>
274282
steps:

src/commands/create_repo.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ parameters:
5151
be immutable which will prevent them from being overwritten.
5252
default: "MUTABLE"
5353

54+
repo_tag:
55+
type: string
56+
description: >
57+
A list of strings in json format, containing tags for repository.
58+
Shorthand Syntax: [{"Key": "FirstTag", "Value": "FirstValue"}]
59+
default: ""
60+
5461
steps:
5562
- run:
5663
name: Create Repository
@@ -63,4 +70,5 @@ steps:
6370
AWS_ECR_ENUM_ENCRYPTION_TYPE: <<parameters.repo_encryption_type>>
6471
AWS_ECR_STR_ENCRYPTION_KMS_KEY: <<parameters.encryption_kms_key>>
6572
AWS_ECR_STR_IMAGE_TAG_MUTABILITY: <<parameters.image_tag_mutability>>
73+
AWS_ECR_STR_REPO_TAG: <<parameters.repo_tag>>
6674
command: <<include(scripts/create_repo.sh)>>

src/commands/tag_repo.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
description: >
2+
Add tag to an existing ECR repository
3+
4+
parameters:
5+
account_id:
6+
type: string
7+
default: ${AWS_ACCOUNT_ID}
8+
description: >
9+
The 12 digit AWS id associated with the ECR account.
10+
This field is required
11+
12+
repo:
13+
type: string
14+
description: Name of an Amazon ECR repository
15+
16+
tag:
17+
type: string
18+
description: >
19+
A list of strings in json format, containing tags for repository
20+
Shorthand Syntax: [{"Key": "FirstTag", "Value": "FirstValue"}]
21+
22+
profile_name:
23+
default: "default"
24+
description: AWS profile to use
25+
type: string
26+
27+
region:
28+
type: string
29+
default: ${AWS_DEFAULT_REGION}
30+
description: >
31+
AWS region of ECR repository. Defaults to environment variable ${AWS_DEFAULT_REGION}
32+
33+
steps:
34+
- run:
35+
name: Add tag <<parameters.tag>> to <<parameters.repo>>
36+
environment:
37+
AWS_ECR_STR_REPO: <<parameters.repo>>
38+
AWS_ECR_STR_REPO_TAG: <<parameters.tag>>
39+
AWS_ECR_STR_REGION: <<parameters.region>>
40+
AWS_ECR_STR_AWS_PROFILE: <<parameters.profile_name>>
41+
AWS_ECR_STR_ACCOUNT_ID: <<parameters.account_id>>
42+
command: <<include(scripts/tag_repo.sh)>>

src/jobs/build_and_push_image.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ parameters:
232232
Defaults to qemu-v7.0.0-28, change only if you know what you are doing.
233233
See https://hub.docker.com/r/tonistiigi/binfmt for details.
234234
235+
repo_tag:
236+
type: string
237+
default: ""
238+
description: |
239+
A list of strings in json format, containing tags for repository.
240+
Shorthand Syntax: [{"Key": "FirstTag", "Value": "FirstValue"}]
241+
235242
steps:
236243
- build_and_push_image:
237244
account_id: <<parameters.account_id>>
@@ -269,3 +276,4 @@ steps:
269276
use_credentials_helper: <<parameters.use_credentials_helper>>
270277
aws_domain: <<parameters.aws_domain>>
271278
binfmt_version: <<parameters.binfmt_version>>
279+
repo_tag: <<parameters.repo_tag>>

src/scripts/create_repo.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ AWS_ECR_EVAL_REPO="$(eval echo "${AWS_ECR_STR_REPO}")"
44
AWS_ECR_EVAL_PROFILE_NAME="$(eval echo "${AWS_ECR_STR_PROFILE_NAME}")"
55
AWS_ECR_EVAL_ENCRYPTION_KMS_KEY="$(eval echo "${AWS_ECR_STR_ENCRYPTION_KMS_KEY}")"
66
AWS_ECR_EVAL_IMAGE_TAG_MUTABILITY="$(eval echo "${AWS_ECR_STR_IMAGE_TAG_MUTABILITY}")"
7+
AWS_ECR_EVAL_REPO_TAG="$(eval echo "${AWS_ECR_STR_REPO_TAG}")"
78

89
if [ "$AWS_ECR_BOOL_PUBLIC_REGISTRY" == "1" ]; then
910
aws ecr-public describe-repositories --profile "${AWS_ECR_EVAL_PROFILE_NAME}" --region us-east-1 --repository-names "${AWS_ECR_EVAL_REPO}" >/dev/null 2>&1 ||
@@ -20,6 +21,10 @@ else
2021
ENCRYPTION_CONFIGURATION+=",kmsKey=${AWS_ECR_EVAL_ENCRYPTION_KMS_KEY}"
2122
fi
2223

24+
if [ -z "${AWS_ECR_EVAL_REPO_TAG}" ]; then
25+
AWS_ECR_EVAL_REPO_TAG="{\"Key\": \"Name\", \"Value\": \""${AWS_ECR_EVAL_REPO}\""}"
26+
fi
27+
2328
aws ecr describe-repositories \
2429
--profile "${AWS_ECR_EVAL_PROFILE_NAME}" \
2530
--region "${AWS_ECR_EVAL_REGION}" \
@@ -30,5 +35,6 @@ else
3035
--repository-name "${AWS_ECR_EVAL_REPO}" \
3136
--image-tag-mutability "${AWS_ECR_EVAL_IMAGE_TAG_MUTABILITY}" \
3237
--image-scanning-configuration "${IMAGE_SCANNING_CONFIGURATION}" \
33-
--encryption-configuration "${ENCRYPTION_CONFIGURATION}"
38+
--encryption-configuration "${ENCRYPTION_CONFIGURATION}" \
39+
--tags "${AWS_ECR_EVAL_REPO_TAG}"
3440
fi

src/scripts/tag_repo.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
AWS_ECR_EVAL_ACCOUNT_ID="$(eval echo "${AWS_ECR_STR_ACCOUNT_ID}")"
3+
AWS_ECR_EVAL_REGION="$(eval echo "${AWS_ECR_STR_REGION}")"
4+
AWS_ECR_EVAL_REPO="$(eval echo "${AWS_ECR_STR_REPO}")"
5+
AWS_ECR_EVAL_PROFILE_NAME="$(eval echo "${AWS_ECR_STR_PROFILE_NAME}")"
6+
AWS_ECR_EVAL_REPO_TAG="$(eval echo "${AWS_ECR_STR_REPO_TAG}")"
7+
8+
if [ "$AWS_ECR_BOOL_PUBLIC_REGISTRY" == "1" ]; then
9+
echo "repo_tag is not supported on public repos"
10+
exit 1
11+
fi
12+
13+
if [ -z "${AWS_ECR_STR_REPO_TAG}" ]; then
14+
AWS_ECR_EVAL_REPO_TAG="{\"Key\": \"Name\", \"Value\": \""${AWS_ECR_EVAL_REPO}\""}"
15+
fi
16+
aws ecr tag-resource \
17+
--profile "${AWS_ECR_EVAL_PROFILE_NAME}" \
18+
--region "${AWS_ECR_EVAL_REGION}" \
19+
--resource-arn "arn:aws:ecr:${AWS_ECR_EVAL_REGION}:${AWS_ECR_EVAL_ACCOUNT_ID}:repository/${AWS_ECR_EVAL_REPO}" \
20+
--tags "${AWS_ECR_EVAL_REPO_TAG}"

0 commit comments

Comments
 (0)