a year ago
Hello,
I would like to know if there is a way to trigger a build for a service by a webhook. My project is an Astro + Ghost stack and I would like to trigger a rebuild for my Astro service when a Ghost article is posted or updated using Ghost webhook.
If anyone has ever done this in railway, I would be happy to have some info regarding my need.
Regards
10 Replies
a year ago
Hello,
What will be sending the webhook?
Does it have the ability to send a POST request with a body and headers?
If it does, you could have it call Railways public API to rebuild your site.
https://docs.railway.com/reference/public-api
Best,
Brody
Status changed to Awaiting User Response Railway • over 1 year ago
a year ago
Thank you for the info, at the moment, I have an issue with the authentication apparently as the API is always responding with a Not Authorized response.
This occurs with Postman and also in Local with a function service.
PS : I loaded the public API json file provided in the documentation.
Status changed to Awaiting Railway Response Railway • over 1 year ago
Status changed to Awaiting User Response Railway • over 1 year ago
a year ago
The one given by the collection : Bearer RAILWAY_TOKEN
Status changed to Awaiting Railway Response Railway • over 1 year ago
a year ago
I added the postman screenshot, the token seems to be properly set.
Attachments
a year ago
This mutation works -
mutation serviceInstanceRedeploy($environmentId: String!, $serviceId: String!) {
serviceInstanceRedeploy(environmentId: $environmentId, serviceId: $serviceId)
}{
"environmentId": "",
"serviceId": ""
}Status changed to Awaiting User Response Railway • over 1 year ago
a year ago
That's the one I'm trying to reach, I created another token and still doesn't work.
I also created a function service with the following function
// Appelle l'API Railway pour redéclencher un build
const railwayDeployUrl = "https://backboard.railway.app/graphql/v2";
const railwayToken = import.meta.env.RAILWAY_TOKEN;
const serviceId = import.meta.env.ASTRO_SERVICE_ID;
const environmentId = import.meta.env.RAILWAY_ENVIRONMENT_ID;
app.use("/*", cors());
app.get("/", (c) => c.text("Hello world!"));
app.get("/api/health", (c) => c.json({ status: "ok" }));
app.post("/api/rebuild", async (c) => {
// changed: added async keyword
// ...existing code...
// const payload = await c.req.json(); // si Ghost envoie un JSON
console.log("Webhook reçu de Ghost:");
const query = `
mutation serviceInstanceRedeploy {
serviceInstanceRedeploy(environmentId: "${environmentId}", serviceId: "${serviceId}")
}
`; // modified: environment variables wrapped in quotes
const response = await fetch(railwayDeployUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${railwayToken}`,
},
body: JSON.stringify({ query }),
});
const data = await response.json();
console.log("Railway build déclenché:", data);
return new Response(
JSON.stringify({ message: "Déploiement déclenché", railwayResponse: data }),
{
status: 200,
}
);
});Status changed to Awaiting Railway Response Railway • over 1 year ago
Status changed to Awaiting User Response Railway • over 1 year ago
a year ago
I used the project Token, not the dashboard token. I created one and it works now sorry for the inconvenience... I've read earlier in an other thread that we should use the project token. I think I misread somewhere.
Anyway the code block is working as expected, I trigger it with the ghost webhook to then trigger the redeploy
Status changed to Awaiting Railway Response Railway • over 1 year ago
a year ago
Technically speaking it is an environment token, it will only work for the environment it was generated for, not for the entire project, It is also possible that token type just outright can't be used for serviceInstanceRedeploy
So I'm glad you got it working now!
Status changed to Awaiting User Response Railway • over 1 year ago
Status changed to Solved brody • over 1 year ago
