Skip to content

Commit 699f3da

Browse files
authored
feat!: add registry login parameter (#292)
* feat: add registry login parameter * docs: add usage example * refactor: add registry login step * fix: add default step
1 parent 683c5b2 commit 699f3da

File tree

4 files changed

+74
-49
lines changed

4 files changed

+74
-49
lines changed

src/commands/build_and_push_image.yml

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,33 +82,29 @@ parameters:
8282
false.
8383
type: boolean
8484

85-
docker_login:
86-
default: false
87-
description: |
88-
Enable dockerhub authentication. Defaults to false.
89-
type: boolean
90-
91-
dockerhub_username:
92-
default: DOCKERHUB_USERNAME
93-
description: >
94-
Dockerhub username to be configured. Set this to the name of the
95-
environment variable you will set to hold this value, i.e.
96-
DOCKERHUB_USERNAME.
97-
type: env_var_name
98-
99-
dockerhub_password:
100-
default: DOCKERHUB_PASSWORD
101-
description: >
102-
Dockerhub password to be configured. Set this to the name of the
103-
environment variable you will set to hold this value, i.e.
104-
DOCKERHUB_PASSWORD.
105-
type: env_var_name
106-
10785
dockerfile:
10886
default: Dockerfile
10987
description: Name of dockerfile to use. Defaults to Dockerfile.
11088
type: string
11189

90+
container_registry_login:
91+
description: >-
92+
Enable login to different image container registries such as
93+
DockerHub, Heroku or Github. Defaults to false.
94+
type: boolean
95+
default: false
96+
97+
registry_login:
98+
description: >-
99+
Custom container registry login step
100+
e.g docker -u $DOCKER_ID -p $DOCKER_PASSWORD
101+
type: steps
102+
default:
103+
- run: >-
104+
echo "Error - container_registry_login parameter is set to true without
105+
any registry_login steps."
106+
- run: exit 1
107+
112108
path:
113109
default: .
114110
description: >-
@@ -255,11 +251,9 @@ steps:
255251
public_registry: <<parameters.public_registry>>
256252
repo_policy_path: <<parameters.repo_policy_path>>
257253
- when:
258-
condition: <<parameters.docker_login>>
259-
steps:
260-
- run: >
261-
docker login -u $<<parameters.dockerhub_username>> -p
262-
$<<parameters.dockerhub_password>>
254+
condition: <<parameters.container_registry_login>>
255+
steps: <<parameters.registry_login>>
256+
263257
- build_image:
264258
registry_id: <<parameters.registry_id>>
265259
repo: <<parameters.repo>>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
description: >
2+
In some cases, images being built using the build_and_push_image job need to pull a custom
3+
Docker image from a container registry like DockerHub, Heroku or GitHub Container Registry.
4+
This requires users to log into these registries first before the images can be built.
5+
This is an example of a using the build_and_push_image job with a custom registry login step.
6+
NOTE: The container_registry_login parameter must be set to true. Registry usernames and passwords
7+
are stored as environment variables in CircleCI with this example.
8+
9+
usage:
10+
version: 2.1
11+
12+
orbs:
13+
aws-ecr: circleci/[email protected]
14+
# importing aws-cli orb is required for authentication
15+
aws-cli: circleci/[email protected]
16+
workflows:
17+
build-and-push-image-with-container-registry-login:
18+
jobs:
19+
- aws-ecr/build_and_push_image:
20+
# must set container registry login to true
21+
container_registry_login: true
22+
container_registry_login_step:
23+
# custom login step for heroku.
24+
- run: docker login -u ${HEROKU_USERNAME} -p ${HEROKU_API_KEY}
25+
# custom login step for GitHub Container Registry.
26+
- run: docker login -u ${GITHUB_USERNAME} -p ${GITHUB_TOKEN}
27+
# custom login step for DockerHub.
28+
- run: docker login -u ${DOCKERHUB_ID} -p ${DOCKERHUB_PASSWORD}
29+
auth:
30+
- aws-cli/setup:
31+
role_arn: arn:aws:iam::123456789012
32+
repo: my-sample-repo
33+
tag: sampleTag
34+
dockerfile: Dockerfile
35+
path: .
36+
region: us-west-2

src/examples/build_test_then_push_image.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ usage:
99

1010
orbs:
1111
aws-ecr: circleci/[email protected]
12-
# importing aws-cli orb is required
12+
# importing aws-cli orb is required for authentication
1313
aws-cli: circleci/[email protected]
1414
jobs:
1515
build-test-then-push-with-buildx:

src/jobs/build_and_push_image.yml

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,23 @@ parameters:
9393
Enable Docker layer caching if using remote Docker engine.
9494
Requires setup_remote_docker to be set to true. Defaults to false.
9595
96-
docker_login:
96+
container_registry_login:
97+
description: >-
98+
Enable login to different image container registries such as
99+
DockerHub, Heroku or Github. Defaults to false.
97100
type: boolean
98101
default: false
99-
description: >
100-
Enable dockerhub authentication. Defaults to false.
101-
102-
dockerhub_username:
103-
type: env_var_name
104-
default: DOCKERHUB_USERNAME
105-
description: >
106-
Dockerhub username to be configured. Set this to the name of
107-
the environment variable you will set to hold this
108-
value, i.e. DOCKERHUB_USERNAME.
109102

110-
dockerhub_password:
111-
type: env_var_name
112-
default: DOCKERHUB_PASSWORD
113-
description: >
114-
Dockerhub password to be configured. Set this to the name of
115-
the environment variable you will set to hold this
116-
value, i.e. DOCKERHUB_PASSWORD.
103+
registry_login:
104+
description: >-
105+
Custom container registry login step
106+
e.g docker -u $DOCKER_ID -p $DOCKER_PASSWORD
107+
type: steps
108+
default:
109+
- run: >-
110+
echo "Error - container_registry_login parameter is set to true without
111+
any registry_login steps."
112+
- run: exit 1
117113

118114
dockerfile:
119115
type: string
@@ -234,9 +230,8 @@ steps:
234230
remote_docker_version: <<parameters.remote_docker_version>>
235231
workspace_root: <<parameters.workspace_root>>
236232
create_repo: <<parameters.create_repo>>
237-
docker_login: <<parameters.docker_login>>
238-
dockerhub_username: <<parameters.dockerhub_username>>
239-
dockerhub_password: <<parameters.dockerhub_password>>
233+
container_registry_login: <<parameters.container_registry_login>>
234+
registry_login: <<parameters.registry_login>>
240235
public_registry_alias: <<parameters.public_registry_alias>>
241236
set_repo_policy: <<parameters.set_repo_policy>>
242237
repo_policy_path: <<parameters.repo_policy_path>>

0 commit comments

Comments
 (0)