Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 953a4be

Browse files
committed
Add example to create Sentry releases.
- Create a new release each time Netlify publishes a new Production Deploy. Signed-off-by: David Calavera <[email protected]>
1 parent 7cb99a0 commit 953a4be

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function_examples/sentry_releases/functions
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build-functions:
2+
mkdir -p functions
3+
go get -v ./...
4+
go build -o functions/deploy-succeeded cmd/deploy-succeeded/main.go
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Sentry release example
2+
3+
This is an example of using [Netlify Functions](https://www.netlify.com/docs/functions/) to create a release in Sentry every time Netlify publishes a new Production deploy.
4+
5+
## Configuration
6+
7+
When you deploy your site to Netlify, you'll need to define the following [build environment variables](https://www.netlify.com/docs/continuous-deployment/#build-environment-variables) in the UI.
8+
9+
1- Authentication token to talk with Sentry's API:
10+
11+
* `SENTRY_AUTH_TOKEN` = "secret token issued by sentry"
12+
13+
2- Your Sentry's organization slug:
14+
15+
* `SENTRY_ORG_SLUG` = "netlify"
16+
17+
3- Your Sentry's project slug:
18+
19+
* `SENTRY_PROJECT_SLUG` = "code-examples"
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"os"
7+
8+
sentry "github.com/atlassian/go-sentry-api"
9+
"github.com/aws/aws-lambda-go/events"
10+
"github.com/aws/aws-lambda-go/lambda"
11+
)
12+
13+
type deploy struct {
14+
CommitRef string `json:"commit_ref"`
15+
Context string `json:"context"`
16+
}
17+
18+
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
19+
var d deploy
20+
21+
if err := json.NewDecoder(bytes.NewReader([]byte(request.Body))).Decode(&d); err != nil {
22+
return events.APIGatewayProxyResponse{
23+
StatusCode: 500,
24+
Body: err.Error(),
25+
}, nil
26+
}
27+
28+
if d.Context != "production" {
29+
// Ignore deploys that don't go to production
30+
return events.APIGatewayProxyResponse{
31+
StatusCode: 200,
32+
Body: "I ran after a deploy was created",
33+
}, nil
34+
}
35+
36+
c, err := sentry.NewClient(os.Getenv("SENTRY_AUTH_TOKEN"), nil, nil)
37+
if err != nil {
38+
return events.APIGatewayProxyResponse{
39+
StatusCode: 500,
40+
Body: err.Error(),
41+
}, nil
42+
}
43+
44+
orgSlug := os.Getenv("SENTRY_ORG_SLUG")
45+
org := sentry.Organization{
46+
Slug: &orgSlug,
47+
}
48+
49+
proSlug := os.Getenv("SENTRY_PROJECT_SLUG")
50+
pro := sentry.Project{
51+
Slug: &proSlug,
52+
}
53+
54+
rel := sentry.NewRelease{
55+
Version: d.CommitRef,
56+
Ref: &d.CommitRef,
57+
}
58+
59+
if _, err := c.CreateRelease(org, pro, rel); err != nil {
60+
return events.APIGatewayProxyResponse{
61+
StatusCode: 500,
62+
Body: err.Error(),
63+
}, nil
64+
}
65+
66+
return events.APIGatewayProxyResponse{
67+
StatusCode: 200,
68+
Body: "I ran after a deploy was created",
69+
}, nil
70+
}
71+
72+
func main() {
73+
// Make the handler available for Remote Procedure Call by AWS Lambda
74+
lambda.Start(handler)
75+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[build]
2+
functions = "functions"
3+
publish = "site"
4+
command = "make build-functions"
5+
6+
[build.environment]
7+
# change this value, you can set it in Netlify's UI too
8+
GO_IMPORT_PATH = "github.com/netlify/code-examples"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html>
2+
<body>
3+
<h1>Example of function creating Sentry releases</h1>
4+
<p>This project includes a function that's triggered after each deploy. It creates a new release in Sentry if the deploy context is production. It ignores deploy previews and branch deploys, but it could be easier to modify to not do that</p>
5+
</body>
6+
</html>

0 commit comments

Comments
 (0)