R
Railway•15mo ago
msolecki

How to rebuild service from code?

I have PaylaodCMS +NextJS website. I need to rebuild nextjs website on any changes in payloadCMS. How can I do that using ny webhooks or code?
13 Replies
Percy
Percy•15mo ago
Project ID: f1ffdfca-0eb5-4966-b060-cd7eb7df4fed
msolecki
msoleckiOP•15mo ago
f1ffdfca-0eb5-4966-b060-cd7eb7df4fed
Brody
Brody•15mo ago
you would have to call railway's public api I think the serviceInstanceRedeploy mutation would work best for your usecase https://docs.railway.app/reference/public-api
msolecki
msoleckiOP•15mo ago
any example? I am trying 2 different tokens (team and personal) and getting errors
Brody
Brody•15mo ago
mutation serviceInstanceRedeploy($environmentId: String!, $serviceId: String!) {
serviceInstanceRedeploy(environmentId: $environmentId, serviceId: $serviceId)
}
mutation serviceInstanceRedeploy($environmentId: String!, $serviceId: String!) {
serviceInstanceRedeploy(environmentId: $environmentId, serviceId: $serviceId)
}
what errors are you getting? how are you using the tokens to authenticate the api requests? id like to note, if you are trying to redeploy a service that resides within a team/pro account, then you need to use the corresponding team token, otherwise use your personal token
Brody
Brody•15mo ago
/graphql/v2
msolecki
msoleckiOP•15mo ago
try {
const serviceID = process.env.SITE_SERVICE_ID
const environmentID = process.env.SITE_ENVIRONMENT_ID

const gqlReq = async (options) => {
const req = await fetch(`https://backboard.railway.app/graphql/v2?q=${options.operationName}`, {
headers: {
"cache-control": "no-cache",
"content-type": "application/json",
},
method: "POST",
body: JSON.stringify({
operationName: options.operationName,
query: options.query,
variables: options.variables,
}),
mode: "cors",
credentials: "include",
})

if (req.status != 200) {
return [null, Error(`Non 200 status code returned from API: ${req.status}`)]
}


const res = await req.json()

if (res.errors != undefined) {
return [null, Error(res.errors[0].message)]
}


if (res.data == null) {
return [null, Error("The API returned the null data type")]
}


return [res.data[options.operationName], null]
}


const [serviceInstanceRedeploy, serviceInstanceRedeployError] = await gqlReq({
operationName: "serviceInstanceRedeploy",
query: `mutation serviceInstanceRedeploy($environmentId: String!, $serviceId: String!) {\n serviceInstanceRedeploy(environmentId: $environmentId, serviceId: $serviceId)\n}`,
variables: {
"environmentId": environmentID,
"serviceId": serviceID,
},
})

if (serviceInstanceRedeployError != null) {
console.error('Error hitting webhook', serviceInstanceRedeployError)
return
}

publishWebhookTimeout = null
} catch (err) {
// eslint-disable-next-line no-console
console.error('Error hitting webhook', err)
}
try {
const serviceID = process.env.SITE_SERVICE_ID
const environmentID = process.env.SITE_ENVIRONMENT_ID

const gqlReq = async (options) => {
const req = await fetch(`https://backboard.railway.app/graphql/v2?q=${options.operationName}`, {
headers: {
"cache-control": "no-cache",
"content-type": "application/json",
},
method: "POST",
body: JSON.stringify({
operationName: options.operationName,
query: options.query,
variables: options.variables,
}),
mode: "cors",
credentials: "include",
})

if (req.status != 200) {
return [null, Error(`Non 200 status code returned from API: ${req.status}`)]
}


const res = await req.json()

if (res.errors != undefined) {
return [null, Error(res.errors[0].message)]
}


if (res.data == null) {
return [null, Error("The API returned the null data type")]
}


return [res.data[options.operationName], null]
}


const [serviceInstanceRedeploy, serviceInstanceRedeployError] = await gqlReq({
operationName: "serviceInstanceRedeploy",
query: `mutation serviceInstanceRedeploy($environmentId: String!, $serviceId: String!) {\n serviceInstanceRedeploy(environmentId: $environmentId, serviceId: $serviceId)\n}`,
variables: {
"environmentId": environmentID,
"serviceId": serviceID,
},
})

if (serviceInstanceRedeployError != null) {
console.error('Error hitting webhook', serviceInstanceRedeployError)
return
}

publishWebhookTimeout = null
} catch (err) {
// eslint-disable-next-line no-console
console.error('Error hitting webhook', err)
}
personal token still Not Authorized
Brody
Brody•15mo ago
hey I wrote that code well kinda it doesn't use a bearer token in the headers because it runs in user space and has access to the pages cookies, so it makes the request to the graphql endpoint with the users cookie instead of a token, that is also why it uses the internal endpoint but if you are using that code in your own code, you need to specify your token in a bearer header
msolecki
msoleckiOP•15mo ago
just "Authorization": Bearer ${railwayToken}, ?
Brody
Brody•15mo ago
looks good
msolecki
msoleckiOP•15mo ago
and now it works 😄 really thanks @Brody
Brody
Brody•15mo ago
no problem!
Want results from more Discord servers?
Add your server