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

Workflow run refactor #969

Merged
merged 11 commits into from
Jun 27, 2024
Merged

Workflow run refactor #969

merged 11 commits into from
Jun 27, 2024

Conversation

godrei
Copy link
Contributor

@godrei godrei commented Jun 19, 2024

Checklist

Version

Requires a NO version update

Context

This PR breaks WorkflowRunner.activateAndRunSteps into smaller functions, to prepare the code base for introducing Step Bundles. The activateAndRunSteps function was lengthy (300+ lines), incorporating many aspects of the execution of the workflow steps.

The single-step execution is implemented in activateAndRunStep and the functionality is broken down into new subfunctions like activateStep and prepareEnvsForStepRun next to the existing runStep.

The new hierarchy of the workflow run function in the run_util.go is:

  • runWorkflow
    • activateAndRunSteps
      • activateAndRunStep
        • activateStep
        • prepareEnvsForStepRun
        • runStep

The code change is about redistributing existing functionality.

@@ -180,34 +180,29 @@ func CleanupStepWorkDir() error {
return nil
}

// GetBuildFailedEnvironments ...
func GetBuildFailedEnvironments(failed bool) []string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused function

@@ -267,6 +267,9 @@ func (r WorkflowRunner) runWorkflows(tracker analytics.Tracker) (models.BuildRun
if err := os.Setenv("BITRISE_TRIGGERED_WORKFLOW_TITLE", targetWorkflow.Title); err != nil {
return models.BuildRunResultsModel{}, fmt.Errorf("failed to set BITRISE_TRIGGERED_WORKFLOW_TITLE env: %w", err)
}
if err := bitrise.SetBuildFailedEnv(false); err != nil {
Copy link
Contributor Author

@godrei godrei Jun 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Earlier build status env was set and updated before running a step (and before evaluating the Step's RunIf condition). Because of this we couldn't merge and move all the env var preparations for a step execution into a new function.

With this change, we initialize the build status env before the workflow execution and update it after each step execution.

stepStartedProperties,
)

*environments = append(*environments, result.OutputEnvironments...)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the single place where a step run modifies the "build"'s env vars, subsequent functions are just reading the env vars, and that's when they receive environments as a value argument.

if mergedStep.RunIf != nil && *mergedStep.RunIf != "" {
buildFailedEnvs := bitrise.BuildFailedEnvs(buildRunResults.IsBuildFailed())
runIfEnvs := append(environments, buildFailedEnvs...)
runIfEnvList, err := envman.ConvertToEnvsJSONModel(runIfEnvs, true, false, &envmanEnv.DefaultEnvironmentSource{})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously the env vars were read from the step's Input EnvStore file:

envList, err := tools.EnvmanReadEnvList(configs.InputEnvstorePath)

Because of this, we had to initialize the Step Input EnvStore file and add the current build env list to the file.

With this change, we can do the Step Input EnvStore file init after evaluating the Step's run conditions (RunIf and IsAlwaysRun) and also all the step env var preparation work could be merged and moved to the new prepareEnvsForStepRun function.

@godrei godrei marked this pull request as ready for review June 20, 2024 08:53
ofalvai
ofalvai previously approved these changes Jun 20, 2024
tothszabi
tothszabi previously approved these changes Jun 21, 2024
}
} else if stepIDData.SteplibSource != "" {
isUpdated := buildRunResults.IsStepLibUpdated(stepIDData.SteplibSource)
isUpdated := isStepLibUpdated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The isUpdated local variable is only used in the function call right below it. We could remove this and just use the isStepLibUpdated as the parameter directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 645be80

}
} else if stepIDData.SteplibSource == "_" {
log.Debugf("[BITRISE_CLI] - Steplib independent step, with direct git uri: (uri:%s) (tag-or-branch:%s)", stepIDData.IDorURI, stepIDData.Version)

// Steplib independent steps are completly defined in workflow
// StepLib independent steps are completely defined in workflow
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This was already like this before your changes but I think a the is missing in front of the workflow word

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 645be80

cli/run_util.go Outdated
// and shutting down the last set of containers at the end of the workflow run
currentStepGroupID := ""
lastStepGroupID := ""
defer func() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I was looking at the code and noticed this defer statement here? With this defer it is not easy to see when the stopContainersForStepGroup function is called because we only know that it will be called before exiting this function. And as I was looking at the function I noticed that it has a single exit point at the end.

So my question is that would it make simpler if we kill this defer and move

if lastStepGroupID != "" {
 	r.stopContainersForStepGroup(lastStepGroupID, plan.WorkflowTitle)
 }

on line 140 or 144?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in b4448e2

@godrei godrei dismissed stale reviews from tothszabi and ofalvai via 645be80 June 24, 2024 12:09
tothszabi
tothszabi previously approved these changes Jun 25, 2024
@godrei godrei merged commit 2509588 into master Jun 27, 2024
5 checks passed
@godrei godrei deleted the workflow-run-refactor branch June 27, 2024 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants