diff --git a/docs/content/docs/concepts/workflow/retention.md b/docs/content/docs/concepts/workflow/retention.md index 5d4d3e43a5..c432e20f83 100644 --- a/docs/content/docs/concepts/workflow/retention.md +++ b/docs/content/docs/concepts/workflow/retention.md @@ -29,6 +29,7 @@ Retention policy is defined through a lua condition. This condition should be ev You will be able to use these variables in conditions: * **run_days_before** (number): count of days between Workflow creation date and now. + * **has_git_branch** (string: true|false): True if a *git.branch* variable is set **(added in 0.48.1)**. * **git_branch_exist** (string: true|false): True if a *git.branch* variable is set and branch still exists on the git repository. * **run_status** (string: Success|Fail|...): the Workflow Run status. * **gerrit_change_merged** (string: true|false): to identify if the gerrit change has been merged. @@ -41,6 +42,19 @@ Examples: -- Keep Run for 365 days return run_days_before < 365 ```` +```lua + -- Keep Run for 365 days if git_branch is set and exists in VCS or only 2 days for removed branches + -- Else keep Run for 365 days if no git_branch info is set + if(has_git_branch == "true") then + if(git_branch_exist == "true") then + return run_days_before < 365 + else + return run_days_before < 2 + end + else + return run_days_before < 365 + end +``` ```lua -- Keep Run for ever return true diff --git a/engine/api/purge/purge_run.go b/engine/api/purge/purge_run.go index 336b0853bc..d2feb5aa71 100644 --- a/engine/api/purge/purge_run.go +++ b/engine/api/purge/purge_run.go @@ -28,6 +28,7 @@ type MarkAsDeleteOptions struct { const ( RunStatus = "run_status" RunDaysBefore = "run_days_before" + RunHasGitBranch = "has_git_branch" RunGitBranchExist = "git_branch_exist" RunChangeExist = "gerrit_change_exist" RunChangeMerged = "gerrit_change_merged" @@ -36,7 +37,7 @@ const ( ) func GetRetentionPolicyVariables() []string { - return []string{RunDaysBefore, RunStatus, RunGitBranchExist, RunChangeMerged, RunChangeAbandoned, RunChangeDayBefore, RunChangeExist} + return []string{RunDaysBefore, RunStatus, RunHasGitBranch, RunGitBranchExist, RunChangeMerged, RunChangeAbandoned, RunChangeDayBefore, RunChangeExist} } func markWorkflowRunsToDelete(ctx context.Context, store cache.Store, db *gorp.DbMap, workflowRunsMarkToDelete *stats.Int64Measure) error { @@ -218,10 +219,14 @@ func purgeComputeVariables(ctx context.Context, luaCheck *luascript.Check, run s } // If we have a branch in payload, check if it exists on repository branches list - if b, has := vars["git.branch"]; has { - _, exist := branchesMap[b] - vars[RunGitBranchExist] = strconv.FormatBool(exist) + b, has := vars["git.branch"] + var exist bool + if has { + _, exist = branchesMap[b] } + vars[RunHasGitBranch] = strconv.FormatBool(has) + vars[RunGitBranchExist] = strconv.FormatBool(exist) + vars[RunStatus] = run.Status varsFloats[RunDaysBefore] = math.Floor(time.Now().Sub(run.LastModified).Hours() / 24)