Skip to content

Commit

Permalink
Add support for GitHub deployment reference
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Castro <[email protected]>
  • Loading branch information
carloscastrojumo committed Mar 1, 2024
1 parent ec7fcb4 commit 69e2e96
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
25 changes: 14 additions & 11 deletions docs/services/github.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

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

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

## Configuration

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

```yaml
apiVersion: v1
Expand Down Expand Up @@ -77,16 +77,19 @@ template.app-deployed: |
requiredContexts: []
autoMerge: true
transientEnvironment: false
reference: v1.0.0
pullRequestComment:
content: |
Application {{.app.metadata.name}} is now running new version of deployments manifests.
See more here: {{.context.argocdUrl}}/applications/{{.app.metadata.name}}?operation=true
```
**Notes**:
- If the message is set to 140 characters or more, it will be truncated.
- If `github.repoURLPath` and `github.revisionPath` are same as above, they can be omitted.
- Automerge is optional and `true` by default for github deployments to ensure the requested ref is up to date with the default branch.
Setting this option to `false` is required if you would like to deploy older refs in your default branch.
For more information see the [GitHub Deployment API Docs](https://docs.github.com/en/rest/deployments/deployments?apiVersion=2022-11-28#create-a-deployment).
- If `github.pullRequestComment.content` is set to 65536 characters or more, it will be truncated.
- 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.
21 changes: 19 additions & 2 deletions pkg/services/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type GitHubDeployment struct {
RequiredContexts []string `json:"requiredContexts"`
AutoMerge *bool `json:"autoMerge,omitempty"`
TransientEnvironment *bool `json:"transientEnvironment,omitempty"`
Reference string `json:"reference,omitempty"`
}

type GitHubPullRequestComment struct {
Expand Down Expand Up @@ -102,7 +103,7 @@ func (g *GitHubNotification) GetTemplater(name string, f texttemplate.FuncMap) (
}
}

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

reference, err = texttemplate.New(name).Funcs(f).Parse(g.Deployment.Reference)
if err != nil {
return nil, err
}

Check warning on line 126 in pkg/services/github.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L125-L126

Added lines #L125 - L126 were not covered by tests

logURL, err = texttemplate.New(name).Funcs(f).Parse(g.Deployment.LogURL)
if err != nil {
return nil, err
Expand Down Expand Up @@ -220,6 +226,11 @@ func (g *GitHubNotification) GetTemplater(name string, f texttemplate.FuncMap) (
notification.GitHub.Deployment.TransientEnvironment = g.Deployment.TransientEnvironment
}

var referenceData bytes.Buffer
if err := reference.Execute(&referenceData, vars); err != nil {
return err
}

Check warning on line 232 in pkg/services/github.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L231-L232

Added lines #L231 - L232 were not covered by tests
notification.GitHub.Deployment.Reference = referenceData.String()
notification.GitHub.Deployment.RequiredContexts = g.Deployment.RequiredContexts
}

Expand Down Expand Up @@ -351,6 +362,12 @@ func (g gitHubService) Send(notification Notification, _ Destination) error {
return err
}

// if no reference is provided, use the revision
ref := notification.GitHub.Deployment.Reference
if ref == "" {
ref = notification.GitHub.revision
}

Check warning on line 369 in pkg/services/github.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L366-L369

Added lines #L366 - L369 were not covered by tests

var deployment *github.Deployment
if len(deployments) != 0 {
deployment = deployments[0]
Expand All @@ -360,7 +377,7 @@ func (g gitHubService) Send(notification Notification, _ Destination) error {
u[0],
u[1],
&github.DeploymentRequest{
Ref: &notification.GitHub.revision,
Ref: &ref,

Check warning on line 380 in pkg/services/github.go

View check run for this annotation

Codecov / codecov/patch

pkg/services/github.go#L380

Added line #L380 was not covered by tests
Environment: &notification.GitHub.Deployment.Environment,
RequiredContexts: &notification.GitHub.Deployment.RequiredContexts,
AutoMerge: notification.GitHub.Deployment.AutoMerge,
Expand Down
2 changes: 2 additions & 0 deletions pkg/services/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func TestGetTemplater_GitHub_Deployment(t *testing.T) {
RepoURLPath: "{{.sync.spec.git.repo}}",
RevisionPath: "{{.sync.status.lastSyncedCommit}}",
Deployment: &GitHubDeployment{
Reference: "v0.0.1",
State: "success",
Environment: "production",
EnvironmentURL: "https://argoproj.github.io",
Expand Down Expand Up @@ -177,6 +178,7 @@ func TestGetTemplater_GitHub_Deployment(t *testing.T) {
assert.Len(t, notification.GitHub.Deployment.RequiredContexts, 0)
assert.Equal(t, &f, notification.GitHub.Deployment.AutoMerge)
assert.Equal(t, &tr, notification.GitHub.Deployment.TransientEnvironment)
assert.Equal(t, "v0.0.1", notification.GitHub.Deployment.Reference)
}

func TestNewGitHubService_GitHubOptions(t *testing.T) {
Expand Down

0 comments on commit 69e2e96

Please sign in to comment.