1
- import { Octokit } from "@octokit/core" ;
1
+ import { Octokit } from "@octokit/core" ;
2
2
import express , { NextFunction , Request , Response } from "express" ;
3
3
import { Webhook , WebhookUnbrandedRequiredHeaders , WebhookVerificationError } from "standardwebhooks"
4
4
@@ -14,6 +14,7 @@ const renderAPIToken = process.env.RENDER_API_TOKEN || '';
14
14
const githubAPIToken = process . env . GITHUB_TOKEN || '' ;
15
15
const githubOwnerName = process . env . GITHUB_OWNER_NAME || '' ;
16
16
const githubRepoName = process . env . GITHUB_REPO_NAME || '' ;
17
+ const githubWorkflowID = process . env . GITHUB_WORKFLOW_ID || 'example.yaml' ;
17
18
18
19
const octokit = new Octokit ( {
19
20
auth : githubAPIToken
@@ -34,6 +35,17 @@ interface RenderService {
34
35
id : string
35
36
name : string
36
37
repo : string
38
+ branch : string
39
+ }
40
+
41
+ interface RenderDeploy {
42
+ id : string
43
+ commit ?: Commit
44
+ }
45
+
46
+ interface Commit {
47
+ id : string
48
+ message : string
37
49
}
38
50
39
51
app . post ( "/webhook" , express . raw ( { type : 'application/json' } ) , ( req : Request , res : Response , next : NextFunction ) => {
@@ -85,16 +97,21 @@ async function handleWebhook(payload: WebhookPayload) {
85
97
return
86
98
}
87
99
100
+ const deploy : RenderDeploy = await fetchDeployInfo ( payload . data . serviceId , event . details . deployId )
101
+ if ( ! deploy . commit ) {
102
+ console . log ( `ignoring deploy success for image backed service: ${ payload . data . serviceId } ` )
103
+ return
104
+ }
105
+
88
106
const service : RenderService = await fetchServiceInfo ( payload )
89
107
90
108
if ( ! service . repo . includes ( `${ githubOwnerName } /${ githubRepoName } ` ) ) {
91
- console . log ( `received deploy success for another service: ${ service . name } ` )
109
+ console . log ( `ignoring deploy success for another service: ${ service . name } ` )
92
110
return
93
111
}
94
112
95
- // TODO trigger github action
96
113
console . log ( `triggering github workflow for ${ githubOwnerName } /${ githubRepoName } for ${ service . name } ` )
97
- await triggerWorkflow ( )
114
+ await triggerWorkflow ( service . name , service . branch )
98
115
return
99
116
default :
100
117
console . log ( `unhandled webhook type ${ payload . type } for service ${ payload . data . serviceId } ` )
@@ -104,10 +121,15 @@ async function handleWebhook(payload: WebhookPayload) {
104
121
}
105
122
}
106
123
107
- async function triggerWorkflow ( ) {
108
- await octokit . request ( 'GET /repos/{owner}/{repo}/actions/workflows' , {
124
+ async function triggerWorkflow ( serviceName : string , branch : string ) {
125
+ await octokit . request ( 'POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches ' , {
109
126
owner : githubOwnerName ,
110
127
repo : githubRepoName ,
128
+ workflow_id : githubWorkflowID ,
129
+ ref : branch ,
130
+ inputs : {
131
+ // service: serviceName
132
+ } ,
111
133
headers : {
112
134
'X-GitHub-Api-Version' : '2022-11-28'
113
135
}
@@ -136,6 +158,25 @@ async function fetchEventInfo(payload: WebhookPayload) {
136
158
}
137
159
}
138
160
161
+ async function fetchDeployInfo ( serviceId : string , deployId : string ) {
162
+ const res = await fetch (
163
+ `${ renderAPIURL } /services/${ serviceId } /deploys/${ deployId } ` ,
164
+ {
165
+ method : "get" ,
166
+ headers : {
167
+ "Content-Type" : "application/json" ,
168
+ Accept : "application/json" ,
169
+ Authorization : `Bearer ${ renderAPIToken } ` ,
170
+ } ,
171
+ } ,
172
+ )
173
+ if ( res . ok ) {
174
+ return res . json ( )
175
+ } else {
176
+ throw new Error ( `unable to fetch deploy info; received code :${ res . status . toString ( ) } ` )
177
+ }
178
+ }
179
+
139
180
async function fetchServiceInfo ( payload : WebhookPayload ) {
140
181
const res = await fetch (
141
182
`${ renderAPIURL } /services/${ payload . data . serviceId } ` ,
0 commit comments