Skip to content

Commit 40a0db6

Browse files
committed
use commit SHA in github and gitlab job search
1 parent 7b9330c commit 40a0db6

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

helpers/foundation-deployer/gitlab/gitlab.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,37 @@ func triggerNewBuild(t testing.TB, ctx context.Context, owner, project, token st
7777
return job.ID, nil
7878
}
7979

80-
// GetLastJobStatus returns the status of the latest executed job
81-
func (g GL) GetLastJobStatus(t testing.TB, ctx context.Context, owner, project, token string) (string, int, error) {
80+
// GetLastJobStatusForSHA finds the latest job associated with a specific commit SHA
81+
func (g GL) GetLastJobStatusForSHA(t testing.TB, ctx context.Context, owner, project, token, sha string) (string, int, error) {
8282
git, err := gitlab.NewClient(token)
8383
if err != nil {
8484
return "", 0, fmt.Errorf("failed to create client: %v", err)
8585
}
8686

87+
projectPath := fmt.Sprintf("%s/%s", owner, project)
88+
89+
pipelineOpts := &gitlab.ListProjectPipelinesOptions{
90+
ListOptions: gitlab.ListOptions{
91+
PerPage: 1,
92+
Page: 1,
93+
OrderBy: "id",
94+
Sort: "desc",
95+
},
96+
SHA: &sha,
97+
}
98+
99+
pipelines, _, err := git.Pipelines.ListProjectPipelines(projectPath, pipelineOpts, gitlab.WithContext(ctx))
100+
if err != nil {
101+
return "", 0, fmt.Errorf("error listing pipelines for SHA %s: %w", sha, err)
102+
}
103+
104+
if len(pipelines) == 0 {
105+
return "", 0, fmt.Errorf("no pipelines found for project '%s' at SHA '%s'", projectPath, sha)
106+
}
107+
108+
latestPipelineID := pipelines[0].ID
87109
includeRetried := true
88-
opts := &gitlab.ListJobsOptions{
110+
jobOpts := &gitlab.ListJobsOptions{
89111
ListOptions: gitlab.ListOptions{
90112
PerPage: 1,
91113
Page: 1,
@@ -95,13 +117,13 @@ func (g GL) GetLastJobStatus(t testing.TB, ctx context.Context, owner, project,
95117
IncludeRetried: &includeRetried,
96118
}
97119

98-
jobs, _, err := git.Jobs.ListProjectJobs(fmt.Sprintf("%s/%s", owner, project), opts, gitlab.WithContext(ctx))
120+
jobs, _, err := git.Jobs.ListPipelineJobs(projectPath, latestPipelineID, jobOpts, gitlab.WithContext(ctx))
99121
if err != nil {
100-
return "", 0, fmt.Errorf("error listing project jobs: %v", err)
122+
return "", 0, fmt.Errorf("error listing jobs for pipeline %d: %w", latestPipelineID, err)
101123
}
102124

103125
if len(jobs) == 0 {
104-
return "", 0, fmt.Errorf("no jobs found for project: %s/%s", owner, project)
126+
return "", 0, fmt.Errorf("no jobs found for pipeline %d", latestPipelineID)
105127
}
106128

107129
return jobs[0].Status, jobs[0].ID, nil
@@ -175,7 +197,7 @@ func (g GL) GetFinalJobStatus(t testing.TB, ctx context.Context, owner, project,
175197
}
176198

177199
// WaitBuildSuccess waits for the current job in a project to finish.
178-
func (g GL) WaitBuildSuccess(t testing.TB, owner, project, token, failureMsg string, maxBuildRetry, maxErrorRetries int, timeBetweenErrorRetries time.Duration) error {
200+
func (g GL) WaitBuildSuccess(t testing.TB, owner, project, token, commitSha, failureMsg string, maxBuildRetry, maxErrorRetries int, timeBetweenErrorRetries time.Duration) error {
179201
var status string
180202
var jobID int
181203
var err error
@@ -184,7 +206,7 @@ func (g GL) WaitBuildSuccess(t testing.TB, owner, project, token, failureMsg str
184206
// wait job creation
185207
time.Sleep(30 * time.Second)
186208

187-
status, jobID, err = g.GetLastJobStatus(t, ctx, owner, project, token)
209+
status, jobID, err = g.GetLastJobStatusForSHA(t, ctx, owner, project, token, commitSha)
188210
if err != nil {
189211
return err
190212
}

helpers/foundation-deployer/stages/apply.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,28 @@ func buildGitLabCICDImage(t testing.TB, s steps.Steps, tfvars GlobalTFVars, c Co
6161
return err
6262
}
6363

64+
commitSha, err := conf.GetCommitSha()
65+
if err != nil {
66+
return err
67+
}
68+
6469
fmt.Println("")
6570
fmt.Println("# Follow job execution in GitLab:")
6671
fmt.Printf("# %s\n", fmt.Sprintf("https://gitlab.com/%s/%s/-/jobs", tfvars.GitRepos.Owner, *tfvars.GitRepos.CICDRunner))
6772

6873
failureMsg := fmt.Sprintf("CI/CD runner image job failed %s/%s repository.", tfvars.GitRepos.Owner, *tfvars.GitRepos.CICDRunner)
69-
err = gl.WaitBuildSuccess(t, tfvars.GitRepos.Owner, *tfvars.GitRepos.CICDRunner, c.GitToken, failureMsg, MaxBuildRetries, MaxErrorRetries, TimeBetweenErrorRetries)
74+
err = gl.WaitBuildSuccess(t, tfvars.GitRepos.Owner, *tfvars.GitRepos.CICDRunner, c.GitToken, commitSha, failureMsg, MaxBuildRetries, MaxErrorRetries, TimeBetweenErrorRetries)
7075
if err != nil {
7176
return err
7277
}
78+
7379
projectsToAdd := make([]string, 5)
7480
projectsToAdd[0] = tfvars.GitRepos.Bootstrap
7581
projectsToAdd[1] = tfvars.GitRepos.Organization
7682
projectsToAdd[2] = tfvars.GitRepos.Environments
7783
projectsToAdd[3] = tfvars.GitRepos.Networks
7884
projectsToAdd[4] = tfvars.GitRepos.Projects
85+
7986
return gl.AddProjectsToJobTokenScope(t, tfvars.GitRepos.Owner, *tfvars.GitRepos.CICDRunner, c.GitToken, projectsToAdd)
8087
}
8188

helpers/foundation-deployer/stages/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ func NewGitLabExecutor(owner, project, token string) *GitLabExecutor {
8181
}
8282

8383
func (e *GitLabExecutor) WaitBuildSuccess(t testing.TB, commitSha, failureMsg string) error {
84-
return e.executor.WaitBuildSuccess(t, e.owner, e.project, e.token, failureMsg, MaxBuildRetries, MaxErrorRetries, TimeBetweenErrorRetries)
84+
return e.executor.WaitBuildSuccess(t, e.owner, e.project, e.token, commitSha, failureMsg, MaxBuildRetries, MaxErrorRetries, TimeBetweenErrorRetries)
8585
}

0 commit comments

Comments
 (0)