Skip to content

Commit 7a4eabc

Browse files
add support for setting credentials via environment variables
1 parent a7136fa commit 7a4eabc

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ export APIKEY__QUAY_IO=my-api-key
115115
docker pushrm quay.io/my-user/my-repo
116116
```
117117

118+
## Log in with environment variables (for CI)
119+
120+
Alternatively credentials can be set as environment variables. Environment variables take precedence over the Docker credentials store. Environment variables can be specified with or without a server name. The variant without a server name takes precedence.
121+
122+
This is intended for running `docker-pushrm` as a standalone tool in a CI environment (no full Docker installation needed).
123+
124+
- `DOCKER_USER` and `DOCKER_PASS`
125+
- `DOCKER_USER__<SERVER>_<DOMAIN>` and `DOCKER_PASS__<SERVER>_<DOMAIN>`
126+
(example for server `docker.io`: `DOCKER_USER__DOCKER_IO=my-user` and `DOCKER_PASS__DOCKER_IO=my-password`)
127+
128+
The provider 'quay' needs an additional env var for the API key in form of `APIKEY__<SERVERNAME>_<DOMAIN>=<apikey>`.
129+
130+
Example:
131+
132+
```
133+
DOCKER_USER=my-user DOCKER_PASS=mypass docker-pushrm my-user/my-repo
134+
```
135+
136+
137+
118138
#### configure Quay API key in Docker config file
119139

120140
In the Docker config file (default: `$HOME/.docker/config.json`) add a json key `plugins.docker-pushrm.apikey_<servername>` with the api key as string value.
@@ -144,7 +164,7 @@ You can still use `docker-pushrm` as standalone executable.
144164

145165
The only obstacle is that you need to provide it credentials in the Docker style.
146166

147-
The easiest way for that is to set up a minimal Docker config file with the registry server logins that you need.
167+
The easiest way for that is to set up a minimal Docker config file with the registry server logins that you need. (Alternatively credentials can be passed [in environment variables](#log-in-with-environment-variables-for-ci) )
148168

149169
You can either create this config file on a computer with Docker installed (by running `docker login` and then copying the `$HOME/.docker/config.json` file).
150170

cmd/docker-cli-plugin-metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var dockerCliPluginMetadataCmd = &cobra.Command{
4040
enc := json.NewEncoder(os.Stdout)
4141
enc.SetIndent("", " ")
4242
// specs: https://docs.docker.com/engine/extend/cli_plugins/#the-docker-cli-plugin-metadata-subcommand
43-
d := map[string]string{"SchemaVersion": "0.1.0", "Vendor": "Christian Korneck", "Version": "1.0.3", "ShortDescription": "Push Readme to container registry"}
43+
d := map[string]string{"SchemaVersion": "0.1.0", "Vendor": "Christian Korneck", "Version": "1.1.0", "ShortDescription": "Push Readme to container registry"}
4444
enc.Encode(d)
4545
},
4646
}

cmd/pushrm.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,23 @@ var pushrmCmd = &cobra.Command{
8686
For some providers an API key needs to be specified
8787
via env var or Docker config file.
8888
89+
Alternatively credentials can be set as environment
90+
variables. Environment variables take precedence over
91+
the Docker credentials store.
8992
93+
Environment variables can be specified with or without
94+
a server name. The variant without a server name takes
95+
precedence:
96+
97+
- DOCKER_USER and DOCKER_PASS
98+
- DOCKER_USER__<SERVER>_<DOMAIN> and DOCKER_PASS__<SERVER>_<DOMAIN>
99+
(example for server 'docker.io': DOCKER_USER__DOCKER_IO=my-user
100+
and DOCKER_PASS__DOCKER_IO=my-password)
101+
102+
The provider 'quay' needs an additional env var for the API key
103+
in form of APIKEY__<SERVERNAME>_<DOMAIN>=<apikey>.
104+
105+
90106
Dockerhub
91107
---------
92108
run 'docker login'
@@ -233,12 +249,29 @@ var pushrmCmd = &cobra.Command{
233249
}
234250

235251
var dockerUser string
236-
dockerUser = os.Getenv("DOCKER_USERNAME")
237252
var dockerPasswd string
238-
dockerPasswd = os.Getenv("DOCKER_PASSWORD")
239253
var err error
240254

255+
// generic env var (no servername specified) takes precedence
256+
dockerUser = os.Getenv("DOCKER_USER")
257+
dockerPasswd = os.Getenv("DOCKER_PASS")
258+
if dockerUser != "" && dockerPasswd != "" {
259+
log.Debug("using credentials for user " + dockerUser + " from generic env var")
260+
}
261+
262+
// env var with servername is next
263+
if dockerUser == "" || dockerPasswd == "" {
264+
suffix := strings.ToUpper(strings.Replace(servername, ".", "_", -1))
265+
dockerUser = os.Getenv("DOCKER_USER__" + suffix)
266+
dockerPasswd = os.Getenv("DOCKER_PASS__" + suffix)
267+
if dockerUser != "" && dockerPasswd != "" {
268+
log.Debug("using credentials for user " + dockerUser + " from env var for suffix " + suffix)
269+
}
270+
}
271+
272+
// if credentials are not found in env vars, look in the Docker credentials store
241273
if dockerUser == "" || dockerPasswd == "" {
274+
log.Debug("no credentials found in env vars. Trying Docker credentials store")
242275
log.Debug("Using config file: ", viper.ConfigFileUsed())
243276

244277
if viper.ConfigFileUsed() == "" {

0 commit comments

Comments
 (0)