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

Support specifying (to|from)-registry-(username|password). Fixes #55 #56

Merged
merged 1 commit into from
Jul 26, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@ Options:
--additional-tag TAG Additional tag for the image, e.g latest. Repeat to add multiple tags
--label LABEL=VALUE Set a label for the image, e.g. GIT_COMMIT=${CI_COMMIT_SHORT_SHA}. Repeat to add multiple labels.
--user USER Set the user and group to run the container as. Valid formats are: user, uid, user:group, uid:gid, uid:group, user:gid
--registry-username USER Set the username to use when deploying to registry, e.g. gitlab-ci-token.
--registry-password PASSWORD Set the password to use when deploying to registry, e.g. ${CI_JOB_TOKEN}.
--from-registry-username USER Set the username to use when pulling base image from registry, e.g. gitlab-ci-token.
--from-registry-password PASSWORD Set the password to use when pulling base image from registry, e.g. ${CI_JOB_TOKEN}.
--to-registry-username USER Set the username to use when deploying to registry, e.g. gitlab-ci-token.
--to-registry-password PASSWORD Set the password to use when deploying to registry, e.g. ${CI_JOB_TOKEN}.
-q, --quiet Don't print a progress bar
-v, --verbose Print status of image building
-m, --main SYMBOL Main namespace
Expand Down
40 changes: 27 additions & 13 deletions src/mach/pack/alpha/jib.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@
jib-container-builder
labels))

(defn jib [{::tools-deps/keys [paths lib-map]} {:keys [image-name image-type tar-file base-image target-dir include additional-tags labels user registry-username registry-password quiet verbose main]}]
(defn explicit-credentials [username password]
(when (and (string? username) (string? password))
(reify CredentialRetriever
(retrieve [_]
(Optional/of (Credential/from username password))))))

(defn jib [{::tools-deps/keys [paths lib-map]} {:keys [image-name image-type tar-file base-image target-dir include additional-tags labels user to-registry-username to-registry-password from-registry-username from-registry-password quiet verbose main]}]
(when-not quiet
(println "Building" image-name))
(let [lib-jars-layer (reduce (fn [acc {:keys [path] :as all}]
Expand Down Expand Up @@ -104,8 +110,13 @@
{:builder (-> (LayerConfiguration/builder)
(.setName "project directories"))
:container-paths nil}
paths)]
(-> (cond-> (Jib/from base-image)
paths)
base-image-with-creds (-> (RegistryImage/named ^String base-image)
(.addCredentialRetriever (or
(explicit-credentials from-registry-username from-registry-password)
(-> (CredentialRetrieverFactory/forImage (ImageReference/parse base-image))
(.dockerConfig)))))]
(-> (cond-> (Jib/from base-image-with-creds)
include (.addLayer [(Paths/get (first (.split include ":")) string-array)]
(AbsoluteUnixPath/get (last (.split include ":"))))
(seq labels) (add-labels labels)
Expand All @@ -125,10 +136,7 @@
(.saveTo (Paths/get tar-file string-array)))
:registry (-> (RegistryImage/named image-name)
(.addCredentialRetriever (or
(when (and (string? registry-username) (string? registry-password))
(reify CredentialRetriever
(retrieve [_]
(Optional/of (Credential/from registry-username registry-password)))))
(explicit-credentials to-registry-username to-registry-password)
(-> (CredentialRetrieverFactory/forImage (ImageReference/parse image-name))
(.dockerConfig)))))))
(seq additional-tags) (add-additional-tags additional-tags)
Expand Down Expand Up @@ -159,8 +167,10 @@
[nil "--label LABEL=VALUE" "Set a label for the image, e.g. GIT_COMMIT=${CI_COMMIT_SHORT_SHA}. Repeat to add multiple labels."
:assoc-fn #(update %1 %2 conj (str/split %3 #"="))]
[nil "--user USER" "Set the user and group to run the container as. Valid formats are: user, uid, user:group, uid:gid, uid:group, user:gid"]
[nil "--registry-username USER" "Set the username to use when deploying to registry, e.g. gitlab-ci-token."]
[nil "--registry-password PASSWORD" "Set the password to use when deploying to registry, e.g. ${CI_JOB_TOKEN}."]
[nil "--from-registry-username USER" "Set the username to use when pulling from registry, e.g. gitlab-ci-token."]
[nil "--from-registry-password PASSWORD" "Set the password to use when pulling from registry, e.g. ${CI_JOB_TOKEN}."]
[nil "--to-registry-username USER" "Set the username to use when deploying to registry, e.g. gitlab-ci-token."]
[nil "--to-registry-password PASSWORD" "Set the password to use when deploying to registry, e.g. ${CI_JOB_TOKEN}."]
["-q" "--quiet" "Don't print a progress bar nor a start of build message"
:default false]
["-v" "--verbose" "Print status of image building"
Expand Down Expand Up @@ -196,8 +206,10 @@
additional-tag
label
user
registry-username
registry-password
to-registry-username
to-registry-password
from-registry-username
from-registry-password
quiet
verbose
main]
Expand All @@ -221,8 +233,10 @@
:additional-tags additional-tag
:labels label
:user user
:registry-username registry-username
:registry-password registry-password
:to-registry-username to-registry-username
:to-registry-password to-registry-password
:from-registry-username from-registry-username
:from-registry-password from-registry-password
:quiet quiet
:verbose verbose
:main main}))))