Skip to content
This repository was archived by the owner on Jun 16, 2024. It is now read-only.

Commit 5e798fe

Browse files
committed
Try more ways to login to the gitlab registry.
This can make the `DOCKER_AUTH_CONFIG` mostly unecessary if the job token is sufficient.
1 parent 2b1c6a8 commit 5e798fe

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# Using Podman to power your Gitlab CI pipeline
22

33
1. [Installation and Setup](#installation-and-setup)
4-
1. [Installing the gitlab-runner](#installing-the-gitlab-runner)
5-
2. [Setting up a Runner Instance](#setting-up-a-runner-instance)
4+
1. [Set up rootless Podman for the gitlab-runner user](#set-up-rootless-podman-for-the-gitlab-runner-user)
5+
2. [Installing the gitlab-runner](#installing-the-gitlab-runner)
6+
3. [Setting up a Runner Instance](#setting-up-a-runner-instance)
67
2. [Tweaking the Installation](#tweaking-the-installation)
8+
1. [Private Registries](#private-registries)
79
3. [License](#license)
810
4. [Links](#links)
911

@@ -72,10 +74,26 @@ The following variables are supported right now:
7274

7375
* `PODMAN_RUN_ARGS`: Customize how Podman spawns the containers.
7476

75-
Podman supports access to private Gitlab registries.
77+
### Private Registries
78+
79+
Podman supports access to private registries.
7680
You can set the `DOCKER_AUTH_CONFIG` variable under **Settings → CI / CD** and provide the credentials for accessing the private registry.
7781
Details how the variable has to look can be found under [using statically defined credentials][gitlab-static-credentials] in the Gitlab documentation.
7882

83+
Additionally, there are multiple ways to authenticate against Gitlab Registries.
84+
The script uses a configured deploy token (via `$CI_DEPLOY_PASSWORD`) to login.
85+
Alternatively, the CI job also provides access to the registry for the duraion of a single job.
86+
The scipt uses variables `$CI_JOB_TOKEN` and `$CI_REGISTRY_PASSWORD`, if available, to log into the registry.
87+
88+
The four methods are tried in order until one succeeds:
89+
90+
1. `DOCKER_AUTH_CONFIG`
91+
2. `CI_DEPLOY_PASSWORD`
92+
3. `CI_JOB_TOKEN`
93+
4. `CI_REGISTRY_PASSWORD`
94+
95+
More details about different authentication variants in the official documentation: <https://docs.gitlab.com/ee/user/packages/container_registry/index.html#authenticate-by-using-gitlab-cicd>
96+
7997
## License
8098

8199
Licensed under the [MIT license].

prepare.sh

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,40 @@ start_container() {
1919
mkdir -p "$CACHE_DIR"
2020
# Use value of ENV variable or {} as empty settings
2121
echo "${CUSTOM_ENV_DOCKER_AUTH_CONFIG:-{\}}" > "$CACHE_DIR"/_authfile_"$CONTAINER_ID"
22-
podman pull --authfile="$CACHE_DIR"/_authfile_"$CONTAINER_ID" "$IMAGE"
22+
23+
# Try logging into the Gitlab Registry if credentials are provided
24+
# https://docs.gitlab.com/ee/user/packages/container_registry/index.html#authenticate-by-using-gitlab-cicd
25+
if ! podman login --authfile "$CACHE_DIR"/_authfile_"$CONTAINER_ID" --get-login "$CUSTOM_ENV_CI_REGISTRY" 2>/dev/null && \
26+
[[ -n "$CUSTOM_ENV_CI_DEPLOY_USER" && -n "$CUSTOM_ENV_CI_DEPLOY_PASSWORD" ]]
27+
then
28+
echo "Login to ${CUSTOM_ENV_CI_REGISTRY} with CI_DEPLOY_USER"
29+
podman login --authfile "$CACHE_DIR"/_authfile_"$CONTAINER_ID" \
30+
--username "$CUSTOM_ENV_CI_DEPLOY_USER" \
31+
--password "$CUSTOM_ENV_CI_DEPLOY_PASSWORD" \
32+
"$CUSTOM_ENV_CI_REGISTRY"
33+
fi
34+
35+
if ! podman login --authfile "$CACHE_DIR"/_authfile_"$CONTAINER_ID" --get-login "$CUSTOM_ENV_CI_REGISTRY" 2>/dev/null && \
36+
[[ -n "$CUSTOM_ENV_CI_JOB_USER" && -n "$CUSTOM_ENV_CI_JOB_TOKEN" ]]
37+
then
38+
echo "Login to ${CUSTOM_ENV_CI_REGISTRY} with CI_JOB_USER"
39+
podman login --authfile "$CACHE_DIR"/_authfile_"$CONTAINER_ID" \
40+
--username "$CUSTOM_ENV_CI_JOB_USER" \
41+
--password "$CUSTOM_ENV_CI_JOB_TOKEN" \
42+
"$CUSTOM_ENV_CI_REGISTRY"
43+
fi
44+
45+
if ! podman login --authfile "$CACHE_DIR"/_authfile_"$CONTAINER_ID" --get-login "$CUSTOM_ENV_CI_REGISTRY" 2>/dev/null && \
46+
[[ -n "$CUSTOM_ENV_CI_REGISTRY_USER" && -n "$CUSTOM_ENV_CI_REGISTRY_PASSWORD" ]]
47+
then
48+
echo "Login to ${CUSTOM_ENV_CI_REGISTRY} with CI_REGISTRY_USER"
49+
podman login --authfile "$CACHE_DIR"/_authfile_"$CONTAINER_ID" \
50+
--username "$CUSTOM_ENV_CI_REGISTRY_USER" \
51+
--password "$CUSTOM_ENV_CI_REGISTRY_PASSWORD" \
52+
"$CUSTOM_ENV_CI_REGISTRY"
53+
fi
54+
55+
podman pull --authfile "$CACHE_DIR"/_authfile_"$CONTAINER_ID" "$IMAGE"
2356
rm "$CACHE_DIR"/_authfile_"$CONTAINER_ID"
2457
podman run \
2558
--detach \

0 commit comments

Comments
 (0)