8000 feat(api): add git.tag support (#3200) · ovh/cds@d4cac55 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit d4cac55

Browse files
bnjjjfsamin
authored andcommitted
feat(api): add git.tag support (#3200)
1 parent b4873be commit d4cac55

34 files changed

+466
-61
lines changed

docs/content/workflows/pipelines/actions/builtin/checkout-application.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ chapter = true
66

77
**CheckoutApplication** is a builtin action, you can't modify it.
88

9-
This action clones a repository into a directory.
9+
This action clones a repository into a directory. If you want to clone a tag from your repository in this way, in your workflow payload you can add a key in your JSON like `"git.tag": "0.2"`.
1010

1111
This will run git clone with the following options:
1212

docs/content/workflows/pipelines/actions/builtin/gitclone.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ Advanced parameters:
2222

2323
* depth - optional - 50 by default. You can remove --depth with the value 'false'
2424
* submodules - true by default, you can set false to avoid this.
25+
* tag - optional - empty by default, you can set to `{{.git.tag}}` to clone a tag from your repository. In this way, in your workflow payload you can add a key in your JSON like `"git.tag": "0.2"`.
2526

2627
Notes:
2728

28-
By defaut, depth is 50 and git clone with `--single-branch` automatically.
29+
By default, depth is 50 and git clone with `--single-branch` automatically.
2930
So, if you want to do in a step script `git diff anotherBranch`, you have to set depth to 'false'.
3031

3132
If there is no user && password && sshkey setted in action GitClone, CDS checks on Application VCS Strategy if some auth parameters can be used.

docs/content/workflows/pipelines/variables.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Here is the list of builtin variables, generated for every build:
4242
- `{{.cds.application}}` The name of the current application
4343
- `{{.cds.job}}` The name of the current job
4444
- `{{.cds.manual}}` true if current pipeline is manually run, false otherwise
45-
- `{{.cds.pipeline}}` The name of the current pipeline
45+
- `{{.cds.pipeline}}` The name of the current pipeline
4646
- `{{.cds.project}}` The name of the current project
4747
- `{{.cds.run}}` Run Number of current workflow, example: 3.0
4848
- `{{.cds.run.number}}` Number of current workflow, example: 3 if `{{.cds.run}} = 3.0`
@@ -97,6 +97,7 @@ Here is the list of git variables:
9797
- `{{.git.url}}`
9898
- `{{.git.http_url}}`
9999
- `{{.git.branch}}`
100+
- `{{.git.tag}}`
100101
- `{{.git.author}}`
101102
- `{{.git.message}}`
102103
- `{{.git.server}}`
@@ -159,4 +160,4 @@ Helpers available and some examples:
159160
- b64dec
160161
- escape : replace '_', '/', '.' by '-'
161162

162-
You're a go developper? See all helpers on https://github.com/ovh/cds/blob/master/sdk/interpolate/interpolate_helper.go#L23
163+
You're a go developper? See all helpers on https://github.com/ovh/cds/blob/master/sdk/interpolate/interpolate_helper.go#L23

engine/api/action/builtin.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ The public key have to be granted on your repository`,
123123
Type: sdk.BooleanParameter,
124124
Advanced: true,
125125
})
126+
gitclone.Parameter(sdk.Parameter{
127+
Name: "tag",
128+
Description: "Useful when you want to git clone a specific tag",
129+
Value: "",
130+
Type: sdk.StringParameter,
131+
Advanced: true,
132+
})
126133
gitclone.Requirement("git", sdk.BinaryRequirement, "git")
127134

128135
if err := checkBuiltinAction(db, gitclone); err != nil {

engine/api/repositoriesmanager/repositories_manager.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ func (c *vcsClient) Commits(ctx context.Context, fullname, branch, since, until
297297
return commits, nil
298298
}
299299

300+
func (c *vcsClient) CommitsBetweenRefs(ctx context.Context, fullname, base, head string) ([]sdk.VCSCommit, error) {
301+
var commits []sdk.VCSCommit
302+
path := fmt.Sprintf("/vcs/%s/repos/%s/commits?base=%s&head=%s", c.name, fullname, url.QueryEscape(base), url.QueryEscape(head))
303+
if code, err := c.doJSONRequest(ctx, "GET", path, nil, &commits); err != nil {
304+
if code != http.StatusNotFound {
305+
return nil, err
306+
}
307+
}
308+
return commits, nil
309+
}
310+
300311
func (c *vcsClient) Commit(ctx context.Context, fullname, hash string) (sdk.VCSCommit, error) {
301312
commit := sdk.VCSCommit{}
302313
path := fmt.Sprintf("/vcs/%s/repos/%s/commits/%s", c.name, fullname, hash)

engine/api/suggest.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func (api *API) getVariablesHandler() service.Handler {
134134
gitVar := []string{
135135
"{{.git.hash}}",
136136
"{{.git.branch}}",
137+
"{{.git.tag}}",
137138
"{{.git.author}}",
138139
"{{.git.project}}",
139140
"{{.git.repository}}",

engine/api/workflow/dao_node_run.go

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ workflow_node_run.triggers_run,
4242
workflow_node_run.vcs_repository,
4343
workflow_node_run.vcs_hash,
4444
workflow_node_run.vcs_branch,
45+
workflow_node_run.vcs_tag,
4546
workflow_node_run.vcs_server,
4647
workflow_node_run.workflow_node_name,
4748
workflow_node_run.header
@@ -247,6 +248,9 @@ func fromDBNodeRun(rr NodeRun, opts LoadRunOptions) (*sdk.WorkflowNodeRun, error
247248
if rr.VCSBranch.Valid {
248249
r.VCSBranch = rr.VCSBranch.String
249250
}
251+
if rr.VCSTag.Valid {
252+
r.VCSTag = rr.VCSTag.String
253+
}
250254
if rr.VCSServer.Valid {
251255
r.VCSServer = rr.VCSServer.String
252256
}
@@ -339,6 +343,8 @@ func makeDBNodeRun(n sdk.WorkflowNodeRun) (*NodeRun, error) {
339343
nodeRunDB.VCSHash.String = n.VCSHash
340344
nodeRunDB.VCSBranch.Valid = true
341345
nodeRunDB.VCSBranch.String = n.VCSBranch
346+
nodeRunDB.VCSTag.Valid = true
347+
nodeRunDB.VCSTag.String = n.VCSTag
342348
nodeRunDB.VCSRepository.Valid = true
343349
nodeRunDB.VCSRepository.String = n.VCSRepository
344350

@@ -476,16 +482,16 @@ func GetNodeRunBuildCommits(ctx context.Context, db gorp.SqlExecutor, store cach
476482
if errclient != nil {
477483
return nil, cur, sdk.WrapError(errclient, "GetNodeRunBuildCommits> Cannot get client")
478484
}
479-
480485
cur.Remote = nodeRun.VCSRepository
481486
cur.Branch = nodeRun.VCSBranch
482487
cur.Hash = nodeRun.VCSHash
488+
cur.Tag = nodeRun.VCSTag
483489

484490
if cur.Remote == "" {
485491
cur.Remote = app.RepositoryFullname
486492
}
487493

488-
if cur.Branch == "" {
494+
if cur.Branch == "" && cur.Tag == "" {
489495
branches, errBr := client.Branches(ctx, cur.Remote)
490496
if errBr != nil {
491497
return nil, cur, sdk.WrapError(errBr, "GetNodeRunBuildCommits> Cannot load branches from vcs api remote %s", cur.Remote)
@@ -514,7 +520,7 @@ func GetNodeRunBuildCommits(ctx context.Context, db gorp.SqlExecutor, store cach
514520
}
515521

516522
var lastCommit sdk.VCSCommit
517-
if cur.Hash == "" {
523+
if cur.Hash == "" && cur.Tag == "" && cur.Branch != "" {
518524
//If we only have the current branch, search for the branch
519525
br, err := client.Branch(ctx, repo, cur.Branch)
520526
if err != nil {
@@ -548,24 +554,46 @@ func GetNodeRunBuildCommits(ctx context.Context, db gorp.SqlExecutor, store cach
548554
if prev.Hash != "" && cur.Hash == prev.Hash {
549555
log.Debug("GetNodeRunBuildCommits> there is not difference between the previous build and the current build for node %s", nodeRun.WorkflowNodeName)
550556
} else if prev.Hash != "" {
551-
if cur.Hash == "" {
557+
switch {
558+
case cur.Hash == "" && cur.Tag == "":
552559
br, err := client.Branch(ctx, repo, cur.Branch)
553560
if err != nil {
554561
return nil, cur, sdk.WrapError(err, "GetNodeRunBuildCommits> Cannot get branch %s", cur.Branch)
555562
}
556563
cur.Hash = br.LatestCommit
557-
}
558-
//If we are lucky, return a true diff
559-
commits, err := client.Commits(ctx, repo, cur.Branch, prev.Hash, cur.Hash)
560-
if err != nil {
561-
return nil, cur, sdk.WrapError(err, "GetNodeRunBuildCommits> Cannot get commits")
562-
}
563-
if commits != nil {
564-
res = commits
564+
//If we are lucky, return a true diff
565+
commits, err := client.Commits(ctx, repo, cur.Branch, prev.Hash, cur.Hash)
566+
if err != nil {
567+
return nil, cur, sdk.WrapError(err, "GetNodeRunBuildCommits> Cannot get commits")
568+
}
569+
if commits != nil {
570+
res = commits
571+
}
572+
573+
case cur.Hash == "" && cur.Tag != "":
574+
c, err := client.CommitsBetweenRefs(ctx, repo, prev.Hash, cur.Tag)
575+
if err != nil {
576+
return nil, cur, sdk.WrapError(err, "GetNodeRunBuildCommits> Cannot get commits")
577+
}
578+
if c != nil {
579+
res = c
580+
}
565581
}
566582
} else if prev.Hash == "" {
567583
if lastCommit.Hash != "" {
568584
res = []sdk.VCSCommit{lastCommit}
585+
} else if cur.Tag != "" {
586+
base := prev.Tag
587+
if base == "" {
588+
base = prev.Hash
589+
}
590+
c, err := client.CommitsBetweenRefs(ctx, repo, base, cur.Tag)
591+
if err != nil {
592+
return nil, cur, sdk.WrapError(err, "GetNodeRunBuildCommits> Cannot get commits")
593+
}
594+
if c != nil {
595+
res = c
596+
}
569597
}
570598
} else {
571599
//If we only get current node run hash
@@ -593,7 +621,7 @@ func PreviousNodeRun(db gorp.SqlExecutor, nr sdk.WorkflowNodeRun, n sdk.Workflow
593621
`, nodeRunFields)
594622
var nodeRun sdk.WorkflowNodeRun
595623
var rr = NodeRun{}
596-
if err := db.SelectOne(&rr, query, n.Name, workflowID, nr.VCSBranch, nr.Number, nr.WorkflowNodeID, nr.ID); err != nil {
624+
if err := db.SelectOne(&rr, query, n.Name, workflowID, nr.VCSBranch, nr.VCSTag, nr.Number, nr.WorkflowNodeID, nr.ID); err != nil {
597625
return nodeRun, sdk.WrapError(err, "PreviousNodeRun> Cannot load previous run on workflow %d node %s", workflowID, n.Name)
598626
}
599627
pNodeRun, errF := fromDBNodeRun(rr, LoadRunOptions{})
@@ -610,11 +638,11 @@ func PreviousNodeRun(db gorp.SqlExecutor, nr sdk.WorkflowNodeRun, n sdk.Workflow
610638
//If you don't have environment linked set envID to 0 or -1
611639
func PreviousNodeRunVCSInfos(db gorp.SqlExecutor, projectKey string, wf *sdk.Workflow, nodeName string, current sdk.BuildNumberAndHash, appID int64, envID int64) (sdk.BuildNumberAndHash, error) {
612640
var previous sdk.BuildNumberAndHash
613-
var prevHash, prevBranch, prevRepository sql.NullString
641+
var prevHash, prevBranch, prevTag, prevRepository sql.NullString
614642
var previousBuildNumber sql.NullInt64
615643

616644
queryPrevious := `
617-
SELECT workflow_node_run.vcs_branch, workflow_node_run.vcs_hash, workflow_node_run.vcs_repository, workflow_node_run.num
645+
SELECT workflow_node_run.vcs_branch, workflow_node_run.vcs_tag, workflow_node_run.vcs_hash, workflow_node_run.vcs_repository, workflow_node_run.num
618646
FROM workflow_node_run
619647
JOIN workflow_node ON workflow_node.name = workflow_node_run.workflow_node_name AND workflow_node.name = $1 AND workflow_node.workflow_id = $2
620648
JOIN workflow_node_context ON workflow_node_context.workflow_node_id = workflow_node.id
@@ -630,7 +658,7 @@ func PreviousNodeRunVCSInfos(db gorp.SqlExecutor, projectKey string, wf *sdk.Wor
630658
}
631659
queryPrevious += fmt.Sprintf(" ORDER BY workflow_node_run.num DESC LIMIT 1")
632660

633-
errPrev := db.QueryRow(queryPrevious, argPrevious...).Scan(&prevBranch, &prevHash, &prevRepository, &previousBuildNumber)
661+
errPrev := db.QueryRow(queryPrevious, argPrevious...).Scan(&prevBranch, &prevTag, &prevHash, &prevRepository, &previousBuildNumber)
634662
if errPrev == sql.ErrNoRows {
635663
log.Warning("PreviousNodeRunVCSInfos> no result with previous %d %s , arguments %v", current.BuildNumber, nodeName, argPrevious)
636664
return previous, nil
@@ -642,6 +670,9 @@ func PreviousNodeRunVCSInfos(db gorp.SqlExecutor, projectKey string, wf *sdk.Wor
642670
if prevBranch.Valid {
643671
previous.Branch = prevBranch.String
644672
}
673+
if prevTag.Valid {
674+
previous.Tag = prevTag.String
675+
}
645676
if prevHash.Valid {
646677
previous.Hash = prevHash.String
647678
}

engine/api/workflow/execute_node_run.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ func SyncNodeRunRunJob(ctx context.Context, db gorp.SqlExecutor, nodeRun *sdk.Wo
766766

767767
type vcsInfos struct {
768768
Repository string
769+
Tag string
769770
Branch string
770771
Hash string
771772
Author string
@@ -783,6 +784,7 @@ func getVCSInfos(ctx context.Context, db gorp.SqlExecutor, store cache.Store, vc
783784
var vcsInfos vcsInfos
784785
vcsInfos.Repository = gitValues[tagGitRepository]
785786
vcsInfos.Branch = gitValues[tagGitBranch]
787+
vcsInfos.Tag = gitValues[tagGitTag]
786788
vcsInfos.Hash = gitValues[tagGitHash]
787789
vcsInfos.Author = gitValues[tagGitAuthor]
788790
vcsInfos.Message = gitValues[tagGitMessage]
@@ -849,6 +851,7 @@ func getVCSInfos(ctx context.Context, db gorp.SqlExecutor, store cache.Store, vc
849851
}
850852
vcsInfos.Hash = ""
851853
vcsInfos.Repository = applicationRepositoryFullname
854+
vcsInfos.Tag = ""
852855
}
853856
}
854857

@@ -869,6 +872,9 @@ func getVCSInfos(ctx context.Context, db gorp.SqlExecutor, store cache.Store, vc
869872
vcsInfos.HTTPUrl = repo.HTTPCloneURL
870873

871874
if vcsInfos.Branch == "" && !isChildNode {
875+
if vcsInfos.Tag != "" {
876+
return vcsInfos, nil
877+
}
872878
return vcsInfos, sdk.WrapError(sdk.ErrBranchNameNotProvided, "computeVCSInfos> should not have an empty branch")
873879
}
874880

engine/api/workflow/gorp_model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type NodeRun struct {
6666
TriggersRun sql.NullString `db:"triggers_run"`
6767
VCSRepository sql.NullString `db:"vcs_repository"`
6868
VCSBranch sql.NullString `db:"vcs_branch"`
69+
VCSTag sql.NullString `db:"vcs_tag"`
6970
VCSHash sql.NullString `db:"vcs_hash"`
7071
VCSServer sql.NullString `db:"vcs_server"`
7172
Header sql.NullString `db:"header"`

engine/api/workflow/process.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,12 @@ func processWorkflowNodeRun(ctx context.Context, db gorp.SqlExecutor, store cach
620620
// Tag VCS infos : add in tag only if it does not exist
621621
if !w.TagExists(tagGitRepository) {
622622
w.Tag(tagGitRepository, run.VCSRepository)
623-
w.Tag(tagGitBranch, run.VCSBranch)
623+
if run.VCSBranch != "" {
624+
w.Tag(tagGitBranch, run.VCSBranch)
625+
}
626+
if run.VCSTag != "" {
627+
w.Tag(tagGitTag, run.VCSTag)
628+
}
624629
if len(run.VCSHash) >= 7 {
625630
w.Tag(tagGitHash, run.VCSHash[:7])
626631
} else {
@@ -720,11 +725,13 @@ func processWorkflowNodeRun(ctx context.Context, db gorp.SqlExecutor, store cach
720725
func setValuesGitInBuildParameters(run *sdk.WorkflowNodeRun, vcsInfos vcsInfos) {
721726
run.VCSRepository = vcsInfos.Repository
722727
run.VCSBranch = vcsInfos.Branch
728+
run.VCSTag = vcsInfos.Tag
723729
run.VCSHash = vcsInfos.Hash
724730
run.VCSServer = vcsInfos.Server
725731

726732
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitRepository, sdk.StringParameter, run.VCSRepository)
727733
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitBranch, sdk.StringParameter, run.VCSBranch)
734+
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitTag, sdk.StringParameter, run.VCSTag)
728735
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitHash, sdk.StringParameter, run.VCSHash)
729736
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitAuthor, sdk.StringParameter, vcsInfos.Author)
730737
sdk.ParameterAddOrSetValue(&run.BuildParameters, tagGitMessage, sdk.StringParameter, vcsInfos.Message)

0 commit comments

Comments
 (0)
0