Skip to content

Commit d4eaae8

Browse files
Add support for GitHub deployment reference (#225)
Signed-off-by: Carlos Castro <carlos.castro@jumo.world>
1 parent ec7fcb4 commit d4eaae8

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

docs/services/github.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44

55
The GitHub notification service changes commit status using [GitHub Apps](https://docs.github.com/en/developers/apps) and requires specifying the following settings:
66

7-
* `appID` - the app id
8-
* `installationID` - the app installation id
9-
* `privateKey` - the app private key
10-
* `enterpriseBaseURL` - optional URL, e.g. https://git.example.com/
7+
- `appID` - the app id
8+
- `installationID` - the app installation id
9+
- `privateKey` - the app private key
10+
- `enterpriseBaseURL` - optional URL, e.g. https://git.example.com/
1111

1212
## Configuration
1313

1414
1. Create a GitHub Apps using https://github.com/settings/apps/new
15-
2. Change repository permissions to enable write commit statuses and/or deployments and/or pull requests comments
16-
![2](https://user-images.githubusercontent.com/18019529/108397381-3ca57980-725b-11eb-8d17-5b8992dc009e.png)
17-
3. Generate a private key, and download it automatically
18-
![3](https://user-images.githubusercontent.com/18019529/108397926-d4a36300-725b-11eb-83fe-74795c8c3e03.png)
19-
4. Install app to account
20-
5. Store privateKey in `argocd-notifications-secret` Secret and configure GitHub integration
21-
in `argocd-notifications-cm` ConfigMap
15+
1. Change repository permissions to enable write commit statuses and/or deployments and/or pull requests comments
16+
![2](https://user-images.githubusercontent.com/18019529/108397381-3ca57980-725b-11eb-8d17-5b8992dc009e.png)
17+
1. Generate a private key, and download it automatically
18+
![3](https://user-images.githubusercontent.com/18019529/108397926-d4a36300-725b-11eb-83fe-74795c8c3e03.png)
19+
1. Install app to account
20+
1. Store privateKey in `argocd-notifications-secret` Secret and configure GitHub integration
21+
in `argocd-notifications-cm` ConfigMap
2222

2323
```yaml
2424
apiVersion: v1
@@ -77,16 +77,19 @@ template.app-deployed: |
7777
requiredContexts: []
7878
autoMerge: true
7979
transientEnvironment: false
80+
reference: v1.0.0
8081
pullRequestComment:
8182
content: |
8283
Application {{.app.metadata.name}} is now running new version of deployments manifests.
8384
See more here: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true
8485
```
8586
8687
**Notes**:
88+
8789
- If the message is set to 140 characters or more, it will be truncated.
8890
- If `github.repoURLPath` and `github.revisionPath` are same as above, they can be omitted.
8991
- Automerge is optional and `true` by default for github deployments to ensure the requested ref is up to date with the default branch.
9092
Setting this option to `false` is required if you would like to deploy older refs in your default branch.
9193
For more information see the [GitHub Deployment API Docs](https://docs.github.com/en/rest/deployments/deployments?apiVersion=2022-11-28#create-a-deployment).
9294
- If `github.pullRequestComment.content` is set to 65536 characters or more, it will be truncated.
95+
- Reference is optional. When set, it will be used as the ref to deploy. If not set, the revision will be used as the ref to deploy.

pkg/services/github.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type GitHubDeployment struct {
5555
RequiredContexts []string `json:"requiredContexts"`
5656
AutoMerge *bool `json:"autoMerge,omitempty"`
5757
TransientEnvironment *bool `json:"transientEnvironment,omitempty"`
58+
Reference string `json:"reference,omitempty"`
5859
}
5960

6061
type GitHubPullRequestComment struct {
@@ -102,7 +103,7 @@ func (g *GitHubNotification) GetTemplater(name string, f texttemplate.FuncMap) (
102103
}
103104
}
104105

105-
var deploymentState, environment, environmentURL, logURL *texttemplate.Template
106+
var deploymentState, environment, environmentURL, reference, logURL *texttemplate.Template
106107
if g.Deployment != nil {
107108
deploymentState, err = texttemplate.New(name).Funcs(f).Parse(g.Deployment.State)
108109
if err != nil {
@@ -119,6 +120,11 @@ func (g *GitHubNotification) GetTemplater(name string, f texttemplate.FuncMap) (
119120
return nil, err
120121
}
121122

123+
reference, err = texttemplate.New(name).Funcs(f).Parse(g.Deployment.Reference)
124+
if err != nil {
125+
return nil, err
126+
}
127+
122128
logURL, err = texttemplate.New(name).Funcs(f).Parse(g.Deployment.LogURL)
123129
if err != nil {
124130
return nil, err
@@ -220,6 +226,11 @@ func (g *GitHubNotification) GetTemplater(name string, f texttemplate.FuncMap) (
220226
notification.GitHub.Deployment.TransientEnvironment = g.Deployment.TransientEnvironment
221227
}
222228

229+
var referenceData bytes.Buffer
230+
if err := reference.Execute(&referenceData, vars); err != nil {
231+
return err
232+
}
233+
notification.GitHub.Deployment.Reference = referenceData.String()
223234
notification.GitHub.Deployment.RequiredContexts = g.Deployment.RequiredContexts
224235
}
225236

@@ -351,6 +362,12 @@ func (g gitHubService) Send(notification Notification, _ Destination) error {
351362
return err
352363
}
353364

365+
// if no reference is provided, use the revision
366+
ref := notification.GitHub.Deployment.Reference
367+
if ref == "" {
368+
ref = notification.GitHub.revision
369+
}
370+
354371
var deployment *github.Deployment
355372
if len(deployments) != 0 {
356373
deployment = deployments[0]
@@ -360,7 +377,7 @@ func (g gitHubService) Send(notification Notification, _ Destination) error {
360377
u[0],
361378
u[1],
362379
&github.DeploymentRequest{
363-
Ref: &notification.GitHub.revision,
380+
Ref: &ref,
364381
Environment: &notification.GitHub.Deployment.Environment,
365382
RequiredContexts: &notification.GitHub.Deployment.RequiredContexts,
366383
AutoMerge: notification.GitHub.Deployment.AutoMerge,

pkg/services/github_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func TestGetTemplater_GitHub_Deployment(t *testing.T) {
129129
RepoURLPath: "{{.sync.spec.git.repo}}",
130130
RevisionPath: "{{.sync.status.lastSyncedCommit}}",
131131
Deployment: &GitHubDeployment{
132+
Reference: "v0.0.1",
132133
State: "success",
133134
Environment: "production",
134135
EnvironmentURL: "https://argoproj.github.io",
@@ -177,6 +178,7 @@ func TestGetTemplater_GitHub_Deployment(t *testing.T) {
177178
assert.Len(t, notification.GitHub.Deployment.RequiredContexts, 0)
178179
assert.Equal(t, &f, notification.GitHub.Deployment.AutoMerge)
179180
assert.Equal(t, &tr, notification.GitHub.Deployment.TransientEnvironment)
181+
assert.Equal(t, "v0.0.1", notification.GitHub.Deployment.Reference)
180182
}
181183

182184
func TestNewGitHubService_GitHubOptions(t *testing.T) {

0 commit comments

Comments
 (0)