R
Railway17mo ago
3.14ki

Executing "git rev-parse --short HEAD" crashes deployment

Hey there, I'm creating another instance of my project, currently it's running on AWS and I intend to create the Railway one too. In my NestJS project I have a line that "Health Check" status route that returns some data and alongside it a git hash. revision: child_process.execSync('git rev-parse --short HEAD').toString().trim() I'm using Docker as my builder, and in it I have a RUN apk add --no-cache git Deploy Logs are saying next:
> [email protected] start:prod
> node dist/main.js
fatal: not a git repository (or any of the parent directories): .git
node:child_process:960
throw err;
^
Error: Command failed: git rev-parse --short HEAD

fatal: not a git repository (or any of the parent directories): .git

at checkExecSyncError (node:child_process:885:11)
at Object.execSync (node:child_process:957:15)
at Object.<anonymous> (/usr/src/app/dist/app.module.js:121:63)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Module.require (node:internal/modules/cjs/loader:1143:19)
at require (node:internal/modules/cjs/helpers:110:18) {
status: 128,
signal: null,
output: [
null,
Buffer(0) [Uint8Array] [],
Buffer(69) [Uint8Array] [...],
pid: 24,
stdout: Buffer(0) [Uint8Array] [],
stderr: Buffer(69) [Uint8Array] [...]

}

Node.js v18.16.1
> [email protected] start:prod
> node dist/main.js
fatal: not a git repository (or any of the parent directories): .git
node:child_process:960
throw err;
^
Error: Command failed: git rev-parse --short HEAD

fatal: not a git repository (or any of the parent directories): .git

at checkExecSyncError (node:child_process:885:11)
at Object.execSync (node:child_process:957:15)
at Object.<anonymous> (/usr/src/app/dist/app.module.js:121:63)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Module.require (node:internal/modules/cjs/loader:1143:19)
at require (node:internal/modules/cjs/helpers:110:18) {
status: 128,
signal: null,
output: [
null,
Buffer(0) [Uint8Array] [],
Buffer(69) [Uint8Array] [...],
pid: 24,
stdout: Buffer(0) [Uint8Array] [],
stderr: Buffer(69) [Uint8Array] [...]

}

Node.js v18.16.1
I saw that Railway provides the env RAILWAY_GIT_COMMIT_SHA but , I'm not sure if I can refactor it and make it work on AWS too. I fetch the shortened SHA inside a config object of an imported module.
Solution:
You're right! What about ```javascript...
Jump to solution
14 Replies
Percy
Percy17mo ago
Project ID: f62c745f-4126-4c82-9c45-02417891d65e
3.14ki
3.14kiOP17mo ago
project-id: f62c745f-4126-4c82-9c45-02417891d65e
Brody
Brody17mo ago
just slap in some logic like if railway return RAILWAY_GIT_COMMIT_SHA else return git hash
Solution
3.14ki
3.14ki17mo ago
You're right! What about
process.env.RAILWAY_STATIC_URL ? RAILWAY_GIT_COMMIT_SHA?.substring(0,7).trim() : child_process.execSync('git rev-parse --short HEAD').toString().trim()
process.env.RAILWAY_STATIC_URL ? RAILWAY_GIT_COMMIT_SHA?.substring(0,7).trim() : child_process.execSync('git rev-parse --short HEAD').toString().trim()
Brody
Brody17mo ago
looks good to me but this raises the question, do you really need to be returning the git hash?
3.14ki
3.14kiOP17mo ago
In a health check route?
Brody
Brody17mo ago
yeah
3.14ki
3.14kiOP17mo ago
🤷‍♂️ That's the way it worked for a loooong time
Brody
Brody17mo ago
whats the purpose of returning the git hash?
3.14ki
3.14kiOP17mo ago
Checking if it deployed the right commit
Brody
Brody17mo ago
fair enough
3.14ki
3.14kiOP17mo ago
I'm trying out the logic and I hope that it will work 💪 @Brody You sir, are a gentleman and a scholar! Thanks a lot!
Brody
Brody17mo ago
pseudo code ftw
Brody
Brody17mo ago
happy to help! 🙂
Want results from more Discord servers?
Add your server