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

Log in to DockerHub during staging before starting build #1979

Merged
merged 5 commits into from
Mar 26, 2021
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
2 changes: 2 additions & 0 deletions gcb/stage/cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ secrets:
- kmsKeyName: projects/k8s-releng-prod/locations/global/keyRings/release/cryptoKeys/encrypt-0
secretEnv:
GITHUB_TOKEN: CiQAIkWjMMb2OjCHCt1hHer4ZBqqD1Mj7+dfTpvkkuzS64nrGm0SUQBLz1D+4Zwp6te6JByO/zvGpHMhW6/i3BJMRYXtNTkyBGMnZuH+J3gQfhcGL2HXjr75lHaCxF4bxPr45xen5D/Kk1+paL+XpOU3KJIADy7uGQ==
DOCKERHUB_TOKEN: CiQAIkWjMJDKU6Hu3pJIBf31dIl7xJQQEiccN7k1cCzGadGoT2gSTQBLz1D+uc9PMusYnFvXtJi25OqEUktDJs28d09jDyj9gcyTx9iX/JvOE3Dn2qnhrjwrUMk5bBhFjR7dHCr4mJgEVD5dXrd6yLGbDBu2

steps:
- name: gcr.io/cloud-builders/git
Expand Down Expand Up @@ -50,6 +51,7 @@ steps:
- "TOOL_REF=${_TOOL_REF}"
secretEnv:
- GITHUB_TOKEN
- DOCKERHUB_TOKEN
args:
- "bin/krel"
- "stage"
Expand Down
65 changes: 65 additions & 0 deletions pkg/anago/anagofakes/fake_stage_impl.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pkg/anago/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ type stageImpl interface {
CommitEmpty(repo *git.Repo, msg string) error
Tag(repo *git.Repo, name, message string) error
CheckReleaseBucket(options *build.Options) error
DockerHubLogin() error
MakeCross(version string) error
GenerateChangelog(options *changelog.Options) error
StageLocalSourceTree(
Expand Down Expand Up @@ -216,6 +217,10 @@ func (d *defaultStageImpl) MakeCross(version string) error {
return build.NewMake().MakeCross(version)
}

func (d *defaultStageImpl) DockerHubLogin() error {
return release.DockerHubLogin()
}

func (d *defaultStageImpl) GenerateChangelog(options *changelog.Options) error {
return changelog.New(options).Run()
}
Expand Down Expand Up @@ -459,6 +464,12 @@ func (d *DefaultStage) TagRepository() error {
}

func (d *DefaultStage) Build() error {
// Log in to Docker Hub to avoid getting rate limited
if err := d.impl.DockerHubLogin(); err != nil {
return errors.Wrap(err, "loging into Docker Hub")
}

// Call MakeCross for each of the versions we are building
for _, version := range d.state.versions.Ordered() {
if err := d.impl.MakeCross(version); err != nil {
return errors.Wrap(err, "build artifacts")
Expand Down
6 changes: 6 additions & 0 deletions pkg/anago/stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,12 @@ func TestBuild(t *testing.T) {
},
shouldError: true,
},
{ // DockerHubLogin fails
prepare: func(mock *anagofakes.FakeStageImpl) {
mock.DockerHubLoginReturns(err)
},
shouldError: true,
},
} {
opts := anago.DefaultStageOptions()
sut := anago.NewDefaultStage(opts)
Expand Down
23 changes: 23 additions & 0 deletions pkg/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ const (
// Publishing bot issue repository
PubBotRepoOrg = "k8s-release-robot"
PubBotRepoName = "sig-release"

DockerHubEnvKey = "DOCKERHUB_TOKEN" // Env var containing the docker key
DockerHubUserName = "k8sreleng" // Docker Hub username
)

var (
Expand Down Expand Up @@ -679,3 +682,23 @@ func CreatePubBotBranchIssue(branchName string) error {
logrus.Infof("Publishing bot issue created #%d!", issue.GetNumber())
return nil
}

// Calls docker login to log into docker hub using a token from the environment
func DockerHubLogin() error {
// Check the environment variable is set
if os.Getenv(DockerHubEnvKey) == "" {
return errors.New("Unable to find docker token in the environment")
}
// Pipe the token into docker login
cmd := command.New(
"docker", "login", fmt.Sprintf("--username=%s", DockerHubUserName),
"--password", os.Getenv(DockerHubEnvKey),
)
// Run docker login:
if err := cmd.RunSuccess(); err != nil {
errStr := strings.ReplaceAll(err.Error(), os.Getenv(DockerHubEnvKey), "**********")
return errors.Wrap(errors.New(errStr), "logging into Docker Hub")
}
logrus.Infof("User %s successfully logged into Docker Hub", DockerHubUserName)
return nil
}