Skip to content

Commit 3f1d912

Browse files
authored
GitHub action example (#1)
1 parent cf0894e commit 3f1d912

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed
File renamed without changes.

app.ts

+47-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Octokit } from "@octokit/core";
1+
import {Octokit} from "@octokit/core";
22
import express, {NextFunction, Request, Response} from "express";
33
import {Webhook, WebhookUnbrandedRequiredHeaders, WebhookVerificationError} from "standardwebhooks"
44

@@ -14,6 +14,7 @@ const renderAPIToken = process.env.RENDER_API_TOKEN || '';
1414
const githubAPIToken = process.env.GITHUB_TOKEN || '';
1515
const githubOwnerName = process.env.GITHUB_OWNER_NAME || '';
1616
const githubRepoName = process.env.GITHUB_REPO_NAME || '';
17+
const githubWorkflowID = process.env.GITHUB_WORKFLOW_ID || 'example.yaml';
1718

1819
const octokit = new Octokit({
1920
auth: githubAPIToken
@@ -34,6 +35,17 @@ interface RenderService {
3435
id: string
3536
name: string
3637
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
3749
}
3850

3951
app.post("/webhook", express.raw({type: 'application/json'}), (req: Request, res: Response, next: NextFunction) => {
@@ -85,16 +97,21 @@ async function handleWebhook(payload: WebhookPayload) {
8597
return
8698
}
8799

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+
88106
const service: RenderService = await fetchServiceInfo(payload)
89107

90108
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}`)
92110
return
93111
}
94112

95-
// TODO trigger github action
96113
console.log(`triggering github workflow for ${githubOwnerName}/${githubRepoName} for ${service.name}`)
97-
await triggerWorkflow()
114+
await triggerWorkflow(service.name, service.branch)
98115
return
99116
default:
100117
console.log(`unhandled webhook type ${payload.type} for service ${payload.data.serviceId}`)
@@ -104,10 +121,15 @@ async function handleWebhook(payload: WebhookPayload) {
104121
}
105122
}
106123

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', {
109126
owner: githubOwnerName,
110127
repo: githubRepoName,
128+
workflow_id: githubWorkflowID,
129+
ref: branch,
130+
inputs: {
131+
// service: serviceName
132+
},
111133
headers: {
112134
'X-GitHub-Api-Version': '2022-11-28'
113135
}
@@ -136,6 +158,25 @@ async function fetchEventInfo(payload: WebhookPayload) {
136158
}
137159
}
138160

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+
139180
async function fetchServiceInfo(payload: WebhookPayload) {
140181
const res = await fetch(
141182
`${renderAPIURL}/services/${payload.data.serviceId}`,

0 commit comments

Comments
 (0)