Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Commit f150c22

Browse files
committed
Added crane push job.
1 parent 804e663 commit f150c22

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
* Added new class `PredefinedImages` in `gcip.addons.container`. Allows access to container images, that are widley used.
2323
* Added new `class` which handels docker client config and renders it to a json string.
2424
* Added `gcip.addon.container.job.crane` to allow copying container images between registries.
25+
* Added `push` Job function to `gcip.addon.container.job.crane` to allow pushing local tarballs to remote registries.
2526

2627
### Changed
2728

gcip/addons/container/jobs/crane.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from gcip.core.job import Job
44
from gcip.core.image import Image
5+
from gcip.core.variables import PredefinedVariables
56
from gcip.addons.container.config import DockerClientConfig
67
from gcip.addons.container.images import PredefinedImages
78

@@ -47,3 +48,53 @@ def copy(
4748
job.prepend_scripts(*docker_client_config.get_shell_command())
4849

4950
return job
51+
52+
53+
def push(
54+
dst: str,
55+
image_path: Optional[str] = None,
56+
image_name: Optional[str] = None,
57+
*,
58+
docker_client_config: Optional[DockerClientConfig] = None,
59+
crane_image: Optional[Union[Image, str]] = None,
60+
) -> Job:
61+
"""
62+
Creates a job to push container image to remote container registry with `crane`.
63+
64+
The image to copy must be in a `tarball` format. It gets validated with crane
65+
and is pushed to `dst` destination registry.
66+
67+
Args:
68+
dst (str): Registry URL to copy container image to.
69+
image_path (Optional[str], optional): Path where to find the container image.
70+
If `None` it defaults internally to `PredefinedVariables.CI_PROJECT_DIR`. Defaults to None.
71+
image_name (Optional[str], optional): Container image name, searched for in `image_path` and gets `.tar` appended.
72+
If `None` it defaults internally to `PredefinedVariables.CI_PROJECT_NAME`. Defaults to None.
73+
docker_client_config (Optional[DockerClientConfig], optional): Creates the Docker configuration file base on objects settings,
74+
used by crane to authenticate against given registries. Defaults to None.
75+
crane_image (Optional[Union[Image, str]], optional): Container image which contains `crane` command.
76+
Defaults to PredefindedImages.CRANE.
77+
78+
Returns:
79+
Job: Returns a `gcip.Job`, with neccessary configuration to push a container image stored as a `tarball` to a remote registry.
80+
"""
81+
if not crane_image:
82+
crane_image = PredefinedImages.CRANE
83+
84+
if not image_path:
85+
image_path = PredefinedVariables.CI_PROJECT_DIR
86+
if not image_name:
87+
image_name = PredefinedVariables.CI_PROJECT_NAME
88+
job = Job(
89+
script=[
90+
f"crane validate --tarball {image_path}/{image_name}.tar",
91+
f"crane push {image_path}/{image_name}.tar {dst}",
92+
],
93+
namespace="push_container_image",
94+
)
95+
job.set_image(crane_image)
96+
97+
if docker_client_config:
98+
job.prepend_scripts(*docker_client_config.get_shell_command())
99+
100+
return job
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
stages:
2+
- push_container_image
3+
push-container-image-push-image:
4+
image:
5+
name: crane_image:v1.1.2
6+
stage: push_container_image
7+
script:
8+
- mkdir -p $HOME/.docker/
9+
- 'echo "{\"auths\": {\"index.docker.io\": {\"username\": \"$REGISTRY_USERNAME\",
10+
\"password\": \"$REGISTRY_PASSWORD\"}}, \"credHelpers\": {\"0132456789.dkr.eu-central-1.amazonaws.com\":
11+
\"ecr-login\"}}" > $HOME/.docker/config.json'
12+
- crane validate --tarball /path/to/project/crane.tar
13+
- crane push /path/to/project/crane.tar index.docker.io
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
stages:
2+
- push_container_image
3+
push-container-image-push-image:
4+
image:
5+
name: gcr.io/go-containerregistry/crane:latest
6+
stage: push_container_image
7+
script:
8+
- crane validate --tarball /path/to/project/gitlab-ci-project.tar
9+
- crane push /path/to/project/gitlab-ci-project.tar index.docker.io

tests/unit/test_addons_container_crane.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from gcip.addons.container.config import DockerClientConfig
66

77

8-
def test_simple_crane_copy_job():
8+
def test_simple_crane_copy_job(gitlab_ci_environment_variables):
99
pipeline = Pipeline()
1010

1111
pipeline.add_children(
@@ -23,7 +23,7 @@ def test_simple_crane_copy_job():
2323
conftest.check(pipeline.render())
2424

2525

26-
def test_advanced_crane_copy_job():
26+
def test_advanced_crane_copy_job(gitlab_ci_environment_variables):
2727
dcc = DockerClientConfig()
2828
dcc.add_auth(registry="index.docker.io")
2929
dcc.add_cred_helper("0132456789.dkr.eu-central-1.amazonaws.com", "ecr-login")
@@ -38,3 +38,26 @@ def test_advanced_crane_copy_job():
3838
name="with_authentication",
3939
)
4040
conftest.check(pipeline.render())
41+
42+
43+
def test_simple_crane_push_job(gitlab_ci_environment_variables):
44+
pipeline = Pipeline()
45+
pipeline.add_children(crane.push(dst="index.docker.io"), name="push_image")
46+
conftest.check(pipeline.render())
47+
48+
49+
def test_advanced_crane_push_job(gitlab_ci_environment_variables):
50+
dcc = DockerClientConfig()
51+
dcc.add_auth(registry="index.docker.io")
52+
dcc.add_cred_helper("0132456789.dkr.eu-central-1.amazonaws.com", cred_helper="ecr-login")
53+
pipeline = Pipeline()
54+
pipeline.add_children(
55+
crane.push(
56+
dst="index.docker.io",
57+
image_name="crane",
58+
docker_client_config=dcc,
59+
crane_image="crane_image:v1.1.2",
60+
),
61+
name="push_image"
62+
)
63+
conftest.check(pipeline.render())

0 commit comments

Comments
 (0)