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

Specify env for build deploy pipeline job #704

Merged
merged 6 commits into from
Nov 11, 2024
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
9 changes: 7 additions & 2 deletions api/applications/applications_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,14 +536,14 @@ func (ah *ApplicationHandler) triggerPipelineBuildOrBuildDeploy(ctx context.Cont
}

branch := pipelineParameters.Branch
envName := pipelineParameters.ToEnvironment
commitID := pipelineParameters.CommitID

if strings.TrimSpace(appName) == "" || strings.TrimSpace(branch) == "" {
return nil, applicationModels.AppNameAndBranchAreRequiredForStartingPipeline()
}

log.Ctx(ctx).Info().Msgf("Creating build pipeline jobController for %s on branch %s for commit %s", appName, branch, commitID)

radixRegistration, err := ah.getUserAccount().RadixClient.RadixV1().RadixRegistrations().Get(ctx, appName, metav1.GetOptions{})
if err != nil {
return nil, err
Expand All @@ -559,6 +559,10 @@ func (ah *ApplicationHandler) triggerPipelineBuildOrBuildDeploy(ctx context.Cont
if len(targetEnvironments) == 0 {
return nil, applicationModels.UnmatchedBranchToEnvironment(branch)
}

if len(envName) > 0 && !slice.Any(targetEnvironments, func(targetEnvName string) bool { return targetEnvName == envName }) {
return nil, applicationModels.EnvironmentNotMappedToBranch(envName, branch)
}
}

jobParameters := pipelineParameters.MapPipelineParametersBuildToJobParameter()
Expand All @@ -568,7 +572,8 @@ func (ah *ApplicationHandler) triggerPipelineBuildOrBuildDeploy(ctx context.Cont
return nil, err
}

log.Ctx(ctx).Info().Msgf("Creating build pipeline job for %s on branch %s for commit %s", appName, branch, commitID)
log.Ctx(ctx).Info().Msgf("Creating build pipeline job for %s on branch %s for commit %s%s", appName, branch, commitID,
radixutils.TernaryString(len(envName) > 0, fmt.Sprintf(", for environment %s", envName), ""))

jobSummary, err := HandleStartPipelineJob(ctx, ah.accounts.UserAccount.RadixClient, appName, ah.pipelineImageTag, ah.tektonImageTag, pipeline, jobParameters)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion api/applications/models/application_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ func AppNameAndBranchAreRequiredForStartingPipeline() error {
return radixhttp.ValidationError("Radix Application Pipeline", "App name and branch are required")
}

// UnmatchedBranchToEnvironment Triggering a pipeline on a un-mapped branch is not allowed
// UnmatchedBranchToEnvironment Triggering a pipeline on an un-mapped branch is not allowed
func UnmatchedBranchToEnvironment(branch string) error {
return radixhttp.ValidationError("Radix Application Pipeline", fmt.Sprintf("Failed to match environment to branch: %s", branch))
}

// EnvironmentNotMappedToBranch Triggering a pipeline on an environment, not matched to a branch
func EnvironmentNotMappedToBranch(envName, branch string) error {
return radixhttp.ValidationError("Radix Application Pipeline", fmt.Sprintf("Failed to match environment %s to branch: %s", envName, branch))
}

// UserNotAllowedToTriggerPipelineError Triggering a pipeline is not allowed for this user and app
func UserNotAllowedToTriggerPipelineError(appName string) error {
return radixhttp.ValidationError("Radix Application Pipeline", fmt.Sprintf("user is not allowed to trigger pipeline for app %s", appName))
Expand Down
6 changes: 6 additions & 0 deletions api/applications/models/pipeline_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ type PipelineParametersBuild struct {
// example: master
Branch string `json:"branch"`

// Name of environment to build for
//
// example: prod
ToEnvironment string `json:"toEnvironment,omitempty"`

// CommitID the commit ID of the branch to build
// REQUIRED for "build" and "build-deploy" pipelines
//
Expand Down Expand Up @@ -93,6 +98,7 @@ type PipelineParametersBuild struct {
func (buildParam PipelineParametersBuild) MapPipelineParametersBuildToJobParameter() *jobModels.JobParameters {
return &jobModels.JobParameters{
Branch: buildParam.Branch,
ToEnvironment: buildParam.ToEnvironment,
CommitID: buildParam.CommitID,
PushImage: buildParam.PushImageToContainerRegistry(),
TriggeredBy: buildParam.TriggeredBy,
Expand Down
1 change: 1 addition & 0 deletions api/applications/start_job_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func buildPipelineJob(ctx context.Context, appName, cloneURL, radixConfigFullNam
buildSpec = v1.RadixBuildSpec{
ImageTag: imageTag,
Branch: jobSpec.Branch,
ToEnvironment: jobSpec.ToEnvironment,
CommitID: jobSpec.CommitID,
PushImage: jobSpec.PushImage,
OverrideUseBuildCache: jobSpec.OverrideUseBuildCache,
Expand Down
6 changes: 6 additions & 0 deletions api/deployments/models/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ type Deployment struct {
// required: true
// example: https://github.com/equinor/radix-canary-golang
Repository string `json:"repository,omitempty"`

// Name of the branch used to build the deployment
//
// required: false
// example: main
BuiltFromBranch string `json:"builtFromBranch,omitempty"`
}

func (d *Deployment) GetComponentByName(name string) *Component {
Expand Down
9 changes: 7 additions & 2 deletions api/deployments/models/deployment_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func (b *deploymentBuilder) buildDeploySummaryPipelineJobInfo() DeploymentSummar
if b.pipelineJob != nil {
jobInfo.CommitID = b.pipelineJob.Spec.Build.CommitID
jobInfo.PipelineJobType = string(b.pipelineJob.Spec.PipeLineType)
jobInfo.BuiltFromBranch = b.pipelineJob.Spec.Build.Branch
jobInfo.PromotedFromEnvironment = b.pipelineJob.Spec.Promote.FromEnvironment
}

Expand All @@ -199,7 +200,7 @@ func (b *deploymentBuilder) buildDeploySummaryPipelineJobInfo() DeploymentSummar

func (b *deploymentBuilder) BuildDeployment() (*Deployment, error) {
b.setSkipDeploymentForComponents()
return &Deployment{
deployment := Deployment{
Name: b.name,
Namespace: b.namespace,
Environment: b.environment,
Expand All @@ -210,5 +211,9 @@ func (b *deploymentBuilder) BuildDeployment() (*Deployment, error) {
GitCommitHash: b.gitCommitHash,
GitTags: b.gitTags,
Repository: b.repository,
}, b.buildError()
}
if b.pipelineJob != nil {
deployment.BuiltFromBranch = b.pipelineJob.Spec.Build.Branch
}
return &deployment, b.buildError()
}
6 changes: 4 additions & 2 deletions api/deployments/models/deployment_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
)

func Test_DeploymentBuilder_BuildDeploymentSummary(t *testing.T) {
deploymentName, envName, jobName, commitID, promoteFromEnv, activeFrom, activeTo :=
deploymentName, envName, jobName, commitID, promoteFromEnv, activeFrom, activeTo, buildFromBranch :=
"deployment-name", "env-name", "job-name", "commit-id", "from-env-name",
time.Now().Add(-10*time.Second).Truncate(1*time.Second), time.Now().Truncate(1*time.Second)
time.Now().Add(-10*time.Second).Truncate(1*time.Second), time.Now().Truncate(1*time.Second), "anybranch"

t.Run("build with deployment", func(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -81,6 +81,7 @@ func Test_DeploymentBuilder_BuildDeploymentSummary(t *testing.T) {
PipeLineType: radixv1.BuildDeploy,
Build: radixv1.RadixBuildSpec{
CommitID: commitID,
Branch: buildFromBranch,
},
Promote: radixv1.RadixPromoteSpec{
FromEnvironment: promoteFromEnv,
Expand All @@ -96,6 +97,7 @@ func Test_DeploymentBuilder_BuildDeploymentSummary(t *testing.T) {
CommitID: commitID,
PipelineJobType: string(radixv1.BuildDeploy),
PromotedFromEnvironment: promoteFromEnv,
BuiltFromBranch: buildFromBranch,
},
}
assert.Equal(t, expected, actual)
Expand Down
6 changes: 6 additions & 0 deletions api/deployments/models/deployment_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ type DeploymentSummaryPipelineJobInfo struct {
// example: build-deploy
PipelineJobType string `json:"pipelineJobType,omitempty"`

// Name of the branch used to build the deployment
//
// required: false
// example: main
BuiltFromBranch string `json:"builtFromBranch,omitempty"`

// Name of the environment the deployment was promoted from
// Applies only for pipeline jobs of type 'promote'
//
Expand Down
8 changes: 8 additions & 0 deletions api/jobs/models/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ type Job struct {
// example: qa
PromotedToEnvironment string `json:"promotedToEnvironment,omitempty"`

// DeployedToEnvironment the name of the environment that was deployed to
//
// required: false
// example: qa
DeployedToEnvironment string `json:"deployedToEnvironment,omitempty"`

// Array of steps
//
// required: false
Expand Down Expand Up @@ -160,9 +166,11 @@ func GetJobFromRadixJob(job *radixv1.RadixJob, jobDeployments []*deploymentModel
switch job.Spec.PipeLineType {
case radixv1.Build, radixv1.BuildDeploy:
jobModel.Branch = job.Spec.Build.Branch
jobModel.DeployedToEnvironment = job.Spec.Build.ToEnvironment
jobModel.CommitID = job.Spec.Build.CommitID
case radixv1.Deploy:
jobModel.ImageTagNames = job.Spec.Deploy.ImageTagNames
jobModel.DeployedToEnvironment = job.Spec.Deploy.ToEnvironment
jobModel.CommitID = job.Spec.Deploy.CommitID
case radixv1.Promote:
jobModel.PromotedFromDeployment = job.Spec.Promote.DeploymentName
Expand Down
2 changes: 1 addition & 1 deletion api/jobs/models/job_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type JobParameters struct {
// For promote pipeline: Environment to locate deployment to promote
FromEnvironment string `json:"fromEnvironment"`

// For promote pipeline: Target environment for promotion
// For build or promote pipeline: Target environment for building and promotion
ToEnvironment string `json:"toEnvironment"`

// ImageRepository of the component, without image name and image-tag
Expand Down
3 changes: 3 additions & 0 deletions api/models/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func BuildEnvironment(rr *radixv1.RadixRegistration, ra *radixv1.RadixApplicatio
if activeRd, ok := slice.FindFirst(rdList, isActiveDeploymentForAppAndEnv(ra.Name, re.Spec.EnvName)); ok {
activeDeployment = BuildDeployment(rr, ra, &activeRd, deploymentList, podList, hpaList, secretList, eventList, rjList, certs, certRequests, tlsValidator, scaledObjects)
secrets = BuildSecrets(secretList, secretProviderClassList, &activeRd)
if len(activeDeployment.BuiltFromBranch) > 0 {
buildFromBranch = activeDeployment.BuiltFromBranch
}
}

return &environmentModels.Environment{
Expand Down
14 changes: 11 additions & 3 deletions api/models/environment_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,27 @@ func BuildEnvironmentSummaryList(rr *radixv1.RadixRegistration, ra *radixv1.Radi
re = &foundRe
}

deploymentSummary := getActiveDeploymentSummary(ra.GetName(), e.Name, rdList)
buildFromBranch := e.Build.From
env := &environmentModels.EnvironmentSummary{
Name: e.Name,
BranchMapping: e.Build.From,
ActiveDeployment: getActiveDeploymentSummary(ra.GetName(), e.Name, rdList),
BranchMapping: buildFromBranch,
ActiveDeployment: deploymentSummary,
Status: getEnvironmentConfigurationStatus(re).String(),
}
envList = append(envList, env)
}

for _, re := range slice.FindAll(reList, predicate.IsOrphanEnvironment) {
deploymentSummary := getActiveDeploymentSummary(ra.GetName(), re.Spec.EnvName, rdList)
buildFromBranch := ""
if deploymentSummary != nil {
buildFromBranch = deploymentSummary.BuiltFromBranch
}
env := &environmentModels.EnvironmentSummary{
Name: re.Spec.EnvName,
ActiveDeployment: getActiveDeploymentSummary(ra.GetName(), re.Spec.EnvName, rdList),
BranchMapping: buildFromBranch,
ActiveDeployment: deploymentSummary,
Status: getEnvironmentConfigurationStatus(&re).String(),
}
envList = append(envList, env)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/cert-manager/cert-manager v1.15.0
github.com/equinor/radix-common v1.9.5
github.com/equinor/radix-job-scheduler v1.11.0
github.com/equinor/radix-operator v1.65.1
github.com/equinor/radix-operator v1.66.0
github.com/evanphx/json-patch/v5 v5.9.0
github.com/felixge/httpsnoop v1.0.4
github.com/golang-jwt/jwt/v5 v5.2.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ github.com/equinor/radix-common v1.9.5 h1:p1xldkYUoavwIMguoxxOyVkOXLPA6K8qMsgzez
github.com/equinor/radix-common v1.9.5/go.mod h1:+g0Wj0D40zz29DjNkYKVmCVeYy4OsFWKI7Qi9rA6kpY=
github.com/equinor/radix-job-scheduler v1.11.0 h1:8wCmXOVl/1cto8q2WJQEE06Cw68/QmfoifYVR49vzkY=
github.com/equinor/radix-job-scheduler v1.11.0/go.mod h1:yPXn3kDcMY0Z3kBkosjuefsdY1x2g0NlBeybMmHz5hc=
github.com/equinor/radix-operator v1.65.1 h1:3lcjWEktQREFj4x1RYUUk9TrQvDQqIN3sJSv+XZEYtA=
github.com/equinor/radix-operator v1.65.1/go.mod h1:bYXKrNGkeRL7ctqj4fwytRFsgHj4XR5ZRks7wXwqjXo=
github.com/equinor/radix-operator v1.66.0 h1:3gRM6mrmmaLMKnpF/hzfAFFuqeW8512XDlQRW1B0kXQ=
github.com/equinor/radix-operator v1.66.0/go.mod h1:bYXKrNGkeRL7ctqj4fwytRFsgHj4XR5ZRks7wXwqjXo=
github.com/evanphx/json-patch v5.9.0+incompatible h1:fBXyNpNMuTTDdquAq/uisOr2lShz4oaXpDTX2bLe7ls=
github.com/evanphx/json-patch v5.9.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.9.0 h1:kcBlZQbplgElYIlo/n1hJbls2z/1awpXxpRi0/FOJfg=
Expand Down
30 changes: 30 additions & 0 deletions swaggerui/html/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -6269,6 +6269,12 @@
"x-go-name": "ActiveTo",
"example": "2006-01-02T15:04:05Z"
},
"builtFromBranch": {
"description": "Name of the branch used to build the deployment",
"type": "string",
"x-go-name": "BuiltFromBranch",
"example": "main"
},
"components": {
"description": "Array of components",
"type": "array",
Expand Down Expand Up @@ -6377,6 +6383,12 @@
"x-go-name": "ActiveTo",
"example": "2006-01-02T15:04:05Z"
},
"builtFromBranch": {
"description": "Name of the branch used to build the deployment",
"type": "string",
"x-go-name": "BuiltFromBranch",
"example": "main"
},
"commitID": {
"description": "CommitID the commit ID of the branch to build",
"type": "string",
Expand Down Expand Up @@ -6444,6 +6456,12 @@
"DeploymentSummaryPipelineJobInfo": {
"type": "object",
"properties": {
"builtFromBranch": {
"description": "Name of the branch used to build the deployment",
"type": "string",
"x-go-name": "BuiltFromBranch",
"example": "main"
},
"commitID": {
"description": "CommitID the commit ID of the branch to build",
"type": "string",
Expand Down Expand Up @@ -6947,6 +6965,12 @@
"x-go-name": "Created",
"example": "2006-01-02T15:04:05Z"
},
"deployedToEnvironment": {
"description": "DeployedToEnvironment the name of the environment that was deployed to",
"type": "string",
"x-go-name": "DeployedToEnvironment",
"example": "qa"
},
"deployments": {
"description": "Array of deployments",
"type": "array",
Expand Down Expand Up @@ -7303,6 +7327,12 @@
"x-go-name": "PushImage",
"example": "true"
},
"toEnvironment": {
"description": "Name of environment to build for",
"type": "string",
"x-go-name": "ToEnvironment",
"example": "prod"
},
"triggeredBy": {
"description": "TriggeredBy of the job - if empty will use user token upn (user principle name)",
"type": "string",
Expand Down
Loading