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

Add push support #84

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,20 @@ Next, any of the following optional parameters may be specified:
* `$TARGET_FILE` (default empty): path to a file containing the name of the
target build stage to build.

* `$PUSH` (default empty): If non-empty the image will use the PUSH variable
as name and push the image. If credentials is required, they can be supplied
as three seperate parameters `$DOCKER_USERNAME`, `DOCKER_PASSWORD` and
`DOCKER_REGISTRY`

Example here would push the image to docker.io/concourse/oci-build.task
```
DOCKER_USERNAME: ((docker_username.secret))
DOCKER_PASSWORD: ((docker_password.secret))
DOCKER_REGISTRY: ((docker_registry.secret))
PUSH: docker.io/concourse/oci-build.task

```

* `$ADDITIONAL_TARGETS` (default empty): a comma-separated (`,`) list of
additional target build stages to build.

Expand Down
6 changes: 6 additions & 0 deletions cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ func main() {
}
}

//CheckForDockerCredentials only returns an error, if there
//was a problem saving the credentials
//No credentials will just be ignored
err = task.CheckForDockerCredentials()
failIf("docker auth check", err)

logrus.Debugf("read config from env: %#v\n", req.Config)

reqPayload, err := json.Marshal(req)
Expand Down
55 changes: 55 additions & 0 deletions docker_auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package task

import (
"encoding/json"
"os"

"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/types"
"github.com/sirupsen/logrus"
)

func CheckForDockerCredentials() error {
username := os.Getenv("DOCKER_USERNAME")
password := os.Getenv("DOCKER_PASSWORD")
registry := os.Getenv("DOCKER_REGISTRY")

if username == "" || password == "" || registry == "" {
logrus.Debugf("No docker credentials in environment variables")
return nil
}

config := configfile.ConfigFile{
AuthConfigs: map[string]types.AuthConfig{
registry: types.AuthConfig{
Username: username,
Password: password,
},
},
}

fileContents, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err

}

fileMode := os.FileMode(0444)

homedir, err := os.UserHomeDir()
if err != nil {
return err
}

configPath := homedir + "/.docker"
if err := os.MkdirAll(configPath, fileMode); err != nil {
return err
}

filePath := configPath + "/config.json"
if err := os.WriteFile(filePath, fileContents, fileMode); err != nil {
return err
}

return nil
}
5 changes: 5 additions & 0 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func Build(buildkitd *Buildkitd, outputsDir string, req Request) (Response, erro
)
}

if cfg.Push != "" {
buildctlArgs = append(buildctlArgs,
"--output", fmt.Sprintf("type=image,name=%s,push=true", cfg.Push))
}

if cfg.AddHosts != "" {
buildctlArgs = append(buildctlArgs,
"--opt", "add-hosts="+cfg.AddHosts,
Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Config struct {
Target string `json:"target" envconfig:"optional"`
TargetFile string `json:"target_file" envconfig:"optional"`
AdditionalTargets []string `json:"additional_targets" envconfig:"ADDITIONAL_TARGETS,optional"`
Push string `json:"push" envconfig:"optional"`

BuildArgs []string `json:"build_args" envconfig:"optional"`
BuildArgsFile string `json:"build_args_file" envconfig:"optional"`
Expand Down