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

runservice: add AGOLA_RUN_COUNTER variable #473

Merged
merged 1 commit into from
Jan 16, 2024

Conversation

alessandro-sorint
Copy link
Contributor

@alessandro-sorint alessandro-sorint commented Jan 10, 2024

add AGOLA_RUN_COUNTER variable by default with run counter value to the run environment

fix #362

internal/services/runservice/common/common.go Show resolved Hide resolved
Comment on lines 7032 to 7035
{ type: 'run', name:'check AGOLA_RUN_COUNTER variable exists', command: '
if [ "$AGOLA_RUN_COUNTER" != "%d" ]; then
exit 1
fi
Copy link
Member

Choose a reason for hiding this comment

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

Don't do the check inside the run but in the test code. Se TestConfigContext

@@ -7008,3 +7008,88 @@ func TestProjectCommitStatusRedelivery(t *testing.T) {
}
})
}

func TestAgolaRunCounterVariableIsDefined(t *testing.T) {
Copy link
Member

@sgotti sgotti Jan 11, 2024

Choose a reason for hiding this comment

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

This test should be generalized to check all the required environment variables and the name should be something like TestRunRequiredEnvVariables.

@sgotti
Copy link
Member

sgotti commented Jan 11, 2024

fix #362

#362 asked for a unique run id. The run counter is unique for the project. Is it enough?

The unique (per agola environment) run id is the run sequence.

@alessandro-sorint alessandro-sorint changed the title runservice: add AGOLA_RUN_COUNTER variable runservice: add AGOLA_RUN_ID variable Jan 11, 2024
@alessandro-sorint
Copy link
Contributor Author

fix #362

#362 asked for a unique run id. The run counter is unique for the project. Is it enough?

The unique (per agola environment) run id is the run sequence.

I think we can add both counter and sequence as AGOLA_RUN_COUNTER and sequence as AGOLA_RUN_ID if you agree

@alessandro-sorint alessandro-sorint changed the title runservice: add AGOLA_RUN_ID variable runservice: add AGOLA_RUN_COUNTER variable Jan 12, 2024
@@ -127,6 +128,9 @@ func GenExecutorTaskSpecData(r *types.Run, rt *types.RunTask, rc *types.RunConfi
// run config Environment variables ovverride every other environment variable
mergeEnv(environment, rc.Environment)

// AGOLA_RUN_COUNTER is generated at runtime since Counter doesn't exist before creating the run
Copy link
Member

Choose a reason for hiding this comment

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

This isn't really true, counter is created before creating the run.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean it is not defined before call the api (it is not defined in RunCreateRequest run api types).
The counter is created in the same transaction before creating the run, but I thought it is not a good place to add variables to the run environment.

The comment could be for example AGOLA_RUN_COUNTER is generated at runtime since Counter is not defined before the save run transaction ?

Copy link
Member

@sgotti sgotti Jan 15, 2024

Choose a reason for hiding this comment

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

I think it should just be

The AGOLA_RUN_COUNTER environment variable is not saved in the runconfig StaticEnvironment map but populated here using the run counter.

testutil.NilError(t, err)

// update commit sha from annotations since it will change at every test
tt.env["AGOLA_GIT_COMMITSHA"] = run.Annotations["commit_sha"]
Copy link
Member

Choose a reason for hiding this comment

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

move it at the start of the test.

Comment on lines 7047 to 7052
{
name: "test push with run count 1",
config: fmt.Sprintf(config, 1),
runNumber: 1,
env: map[string]string{
"AGOLA_RUN_COUNTER": "1",
"AGOLA_GIT_REF_TYPE": "branch",
"AGOLA_GIT_REF": "refs/heads/master",
"AGOLA_GIT_BRANCH": "master",
"AGOLA_GIT_TAG": "",
"AGOLA_GIT_COMMITSHA": "",
},
},
{
name: "test push with run count 2",
config: fmt.Sprintf(config, 2),
runNumber: 2,
env: map[string]string{
"AGOLA_RUN_COUNTER": "2",
"AGOLA_GIT_REF_TYPE": "branch",
"AGOLA_GIT_REF": "refs/heads/master",
"AGOLA_GIT_BRANCH": "master",
"AGOLA_GIT_TAG": "",
"AGOLA_GIT_COMMITSHA": "",
},
},
{
name: "test push with run count 3",
config: fmt.Sprintf(config, 3),
runNumber: 3,
env: map[string]string{
"AGOLA_RUN_COUNTER": "3",
"AGOLA_GIT_REF_TYPE": "branch",
"AGOLA_GIT_REF": "refs/heads/master",
"AGOLA_GIT_BRANCH": "master",
"AGOLA_GIT_TAG": "",
"AGOLA_GIT_COMMITSHA": "",
},
},
{
name: "test push with run count 4",
config: fmt.Sprintf(config, 4),
runNumber: 4,
env: map[string]string{
"AGOLA_RUN_COUNTER": "4",
"AGOLA_GIT_REF_TYPE": "branch",
"AGOLA_GIT_REF": "refs/heads/master",
"AGOLA_GIT_BRANCH": "master",
"AGOLA_GIT_TAG": "",
"AGOLA_GIT_COMMITSHA": "",
},
},
{
name: "test push with run count 5",
config: fmt.Sprintf(config, 5),
runNumber: 5,
env: map[string]string{
"AGOLA_RUN_COUNTER": "5",
"AGOLA_GIT_REF_TYPE": "branch",
"AGOLA_GIT_REF": "refs/heads/master",
"AGOLA_GIT_BRANCH": "master",
"AGOLA_GIT_TAG": "",
"AGOLA_GIT_COMMITSHA": "",
},
},
Copy link
Member

Choose a reason for hiding this comment

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

Why 5 identical tests?

"AGOLA_GIT_REF": "refs/heads/master",
"AGOLA_GIT_BRANCH": "master",
"AGOLA_GIT_TAG": "",
"AGOLA_GIT_COMMITSHA": "",
Copy link
Member

Choose a reason for hiding this comment

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

From this looks like AGOLA_GIT_COMMITSHA should be empty. No need to define it here if you're going to populate it later from annotations.

@@ -127,6 +128,9 @@ func GenExecutorTaskSpecData(r *types.Run, rt *types.RunTask, rc *types.RunConfi
// run config Environment variables ovverride every other environment variable
mergeEnv(environment, rc.Environment)

// The AGOLA_RUN_COUNTER environment variable is not saved in the runconfig StaticEnvironment map but it populated here using the run counter
Copy link
Member

@sgotti sgotti Jan 15, 2024

Choose a reason for hiding this comment

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

I updated the comment (remove spurious 'it')

Comment on lines 7082 to 7084
for _, tt := range tests {
push(t, tt.config, giteaRepo.CloneURL, giteaToken, "commit", false)
}
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't makes sense to push for every test. All of this should be put inside the tests loop (that could be parallelized).

tests/setup_test.go Show resolved Hide resolved
],
},
steps: [
{ type: 'run', command: 'env -u AGOLA_SSHPRIVKEY' },
Copy link
Member

Choose a reason for hiding this comment

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

is -u ... really needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes because AGOLA_SSHPRIVKEY can contains value with equal characters and the ParseEnv split will fail

Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about which is the issue but there're already other tests that use don't have such issue. Anyway it should be fixed in ParseEnvs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I are investigating well, in the other tests we get the log of directruns and I see they have AGOLA_SSHPRIVKEY variable with empty value, so they work.
In my test the AGOLA_SSHPRIVKEY is a multiline variable, so I think the best way is to skip this variable with -u

"AGOLA_GIT_REF": "refs/heads/master",
"AGOLA_GIT_BRANCH": "master",
"AGOLA_GIT_TAG": "",
"AGOLA_GIT_COMMITSHA": "",
Copy link
Member

Choose a reason for hiding this comment

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

remove this.

{
name: "test push with run count 1",
env: map[string]string{
"AGOLA_RUN_COUNTER": "",
Copy link
Member

Choose a reason for hiding this comment

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

remove this

@alessandro-sorint alessandro-sorint force-pushed the agolaruncounter_var branch 2 times, most recently from 5a2aed6 to c4ae143 Compare January 16, 2024 10:01
add AGOLA_RUN_COUNTER variable by default with run counter value to the run environment
@sgotti sgotti merged commit 9c7bc1b into agola-io:master Jan 16, 2024
1 check passed
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.

Access Run ID from task/step
2 participants