From 69e2e96a9c169c2a8f43451cf1e4171eed61f179 Mon Sep 17 00:00:00 2001 From: Carlos Castro Date: Fri, 1 Mar 2024 10:48:47 +0000 Subject: [PATCH] Add support for GitHub deployment reference Signed-off-by: Carlos Castro --- docs/services/github.md | 25 ++++++++++++++----------- pkg/services/github.go | 21 +++++++++++++++++++-- pkg/services/github_test.go | 2 ++ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/docs/services/github.md b/docs/services/github.md index 1fa1a985..36fbda5a 100644 --- a/docs/services/github.md +++ b/docs/services/github.md @@ -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 @@ -77,6 +77,7 @@ 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. @@ -84,9 +85,11 @@ template.app-deployed: | ``` **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. diff --git a/pkg/services/github.go b/pkg/services/github.go index 6200c8f4..5ed0a5c0 100644 --- a/pkg/services/github.go +++ b/pkg/services/github.go @@ -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 { @@ -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 { @@ -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 + } + logURL, err = texttemplate.New(name).Funcs(f).Parse(g.Deployment.LogURL) if err != nil { return nil, err @@ -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 + } + notification.GitHub.Deployment.Reference = referenceData.String() notification.GitHub.Deployment.RequiredContexts = g.Deployment.RequiredContexts } @@ -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 + } + var deployment *github.Deployment if len(deployments) != 0 { deployment = deployments[0] @@ -360,7 +377,7 @@ func (g gitHubService) Send(notification Notification, _ Destination) error { u[0], u[1], &github.DeploymentRequest{ - Ref: ¬ification.GitHub.revision, + Ref: &ref, Environment: ¬ification.GitHub.Deployment.Environment, RequiredContexts: ¬ification.GitHub.Deployment.RequiredContexts, AutoMerge: notification.GitHub.Deployment.AutoMerge, diff --git a/pkg/services/github_test.go b/pkg/services/github_test.go index 250f41f2..c9649974 100644 --- a/pkg/services/github_test.go +++ b/pkg/services/github_test.go @@ -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", @@ -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) {