Skip to content

Commit

Permalink
Merge pull request #63 from ninech/build-retry
Browse files Browse the repository at this point in the history
Add app build retries
  • Loading branch information
pawelkuc authored Sep 13, 2023
2 parents e3d5adc + afac7e5 commit 6b9eacb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
10 changes: 10 additions & 0 deletions api/util/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ func EnvVarToString(envs apps.EnvVars) string {
return strings.Join(keyValuePairs, ";")
}

func EnvVarByName(envVars apps.EnvVars, name string) *apps.EnvVar {
for _, e := range envVars {
if e.Name == name {
return &e
}
}

return nil
}

type GitAuth struct {
Username *string
Password *string
Expand Down
13 changes: 11 additions & 2 deletions update/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// BuildTrigger is used to request a retry-build for the application.
const BuildTrigger = "BUILD_TRIGGER"

// all fields need to be pointers so we can detect if they have been set by
// the user.
type applicationCmd struct {
Expand All @@ -27,6 +30,7 @@ type applicationCmd struct {
BuildEnv *map[string]string `help:"Environment variables names which are passed to the app build process."`
DeleteBuildEnv *[]string `help:"Build environment variables which are to be deleted."`
DeployJob *deployJob `embed:"" prefix:"deploy-job-"`
RetryBuild *bool `help:"Retries build for the application if set to true." placeholder:"false"`
}

type gitConfig struct {
Expand Down Expand Up @@ -137,10 +141,15 @@ func (cmd *applicationCmd) applyUpdates(app *apps.Application) {
}
app.Spec.ForProvider.Config.Env = util.UpdateEnvVars(app.Spec.ForProvider.Config.Env, env, delEnv)

var buildEnv map[string]string
if cmd.Env != nil {
buildEnv := make(map[string]string)
if cmd.BuildEnv != nil {
buildEnv = *cmd.BuildEnv
}

if cmd.RetryBuild != nil && *cmd.RetryBuild {
buildEnv[BuildTrigger] = time.Now().UTC().Format(time.RFC3339)
}

var buildDelEnv []string
if cmd.DeleteEnv != nil {
buildDelEnv = *cmd.DeleteBuildEnv
Expand Down
22 changes: 22 additions & 0 deletions update/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ func TestApplication(t *testing.T) {
assert.Equal(t, *cmd.DeployJob.Name, updated.Spec.ForProvider.Config.DeployJob.Name)
assert.Equal(t, *cmd.DeployJob.Timeout, updated.Spec.ForProvider.Config.DeployJob.Timeout.Duration)
assert.Equal(t, *cmd.DeployJob.Retries, *updated.Spec.ForProvider.Config.DeployJob.Retries)
// RetryBuild should be not set by default:
assert.Nil(t, util.EnvVarByName(updated.Spec.ForProvider.BuildEnv, BuildTrigger))
},
},
"reset env variables": {
Expand Down Expand Up @@ -209,6 +211,26 @@ func TestApplication(t *testing.T) {
assert.Nil(t, updated.Spec.ForProvider.Config.DeployJob)
},
},
"retry build": {
orig: existingApp,
cmd: applicationCmd{
Name: pointer.String(existingApp.Name),
RetryBuild: pointer.Bool(true),
},
checkApp: func(t *testing.T, cmd applicationCmd, orig, updated *apps.Application) {
assert.NotNil(t, util.EnvVarByName(updated.Spec.ForProvider.BuildEnv, BuildTrigger))
},
},
"do not retry build": {
orig: existingApp,
cmd: applicationCmd{
Name: pointer.String(existingApp.Name),
RetryBuild: pointer.Bool(false),
},
checkApp: func(t *testing.T, cmd applicationCmd, orig, updated *apps.Application) {
assert.Nil(t, util.EnvVarByName(updated.Spec.ForProvider.BuildEnv, BuildTrigger))
},
},
}

for name, tc := range cases {
Expand Down

0 comments on commit 6b9eacb

Please sign in to comment.