Search code examples
node.jstypescriptgithubherokugithub-actions

App crashing right away after deploy from Github Actions


Well, I have pushed my TypeScript project to github, and it has these scripts:

"scripts": {
    "test": "jest",
    "test:cov": "jest --coverage -i",
    "test:unit": "jest unit --coverage -i",
    "test:int": "jest int --coverage -i",
    "build": "rimraf ./dist && tsc",
    "start": "node ./dist/index.js",
    "dev": "ts-node-dev --respawn --transpile-only --inspect -- ./src/index.ts"
  }

After that, I created a github action workflow, making it run my tests and if they all pass, it deploys to heroku, but when the deployment is made, the heroku app crashes instantly.

This is the workflow: main.yml:

name: Main workflow
on:
  pull_request:
    branches:
      - main
  push:
    branches:
      - main
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.18.1]

    steps:
      - uses: actions/checkout@v2

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/[email protected]
        with:
          node-version: ${{ matrix.node-version }}
      - name: Install deps
        run: yarn --frozen-lockfile
      - name: Test
        env:
          MONGODB_URL_TEST: ${{secrets.MONGODB_URL_TEST}}
          AUTH_TOKEN: ${{secrets.AUTH_TOKEN}}
        run: yarn test
      - name: Build the project
        run: yarn build
      - name: Deploy the project
        if: ${{github.ref == 'refs/heads/main'}}
        uses: akhileshns/[email protected]
        with:
          heroku_api_key: ${{secrets.HEROKU_API_KEY}}
          heroku_app_name: "testjvcapi"
          heroku_email: "[email protected]"
        env:
          HD_MONGODB_URL: ${{secrets.MONGODB_URL}}
          HD_AUTH_TOKEN: ${{secrets.AUTH_TOKEN}}

This is the last action that I triggered: https://github.com/joaocasarin/test-selfapi/actions/runs/1365239970

This is the repo: https://github.com/joaocasarin/test-selfapi/

This is the log after the deployment to heroku:

2021-10-20T20:33:33.000000+00:00 app[api]: Build started by user *****@hotmail.com
2021-10-20T20:34:16.052764+00:00 app[api]: Release v6 created by user *****@hotmail.com
2021-10-20T20:34:16.052764+00:00 app[api]: Deploy 1192ce7c by user *****@hotmail.com
2021-10-20T20:34:16.359307+00:00 heroku[web.1]: State changed from crashed to starting
2021-10-20T20:34:18.914264+00:00 heroku[web.1]: Starting process with command `npm start`
2021-10-20T20:34:20.660465+00:00 app[web.1]: [heroku-exec] Starting
2021-10-20T20:34:20.956196+00:00 app[web.1]:
2021-10-20T20:34:20.956213+00:00 app[web.1]: > [email protected] start /app
2021-10-20T20:34:20.956214+00:00 app[web.1]: > node ./dist/index.js
2021-10-20T20:34:20.956214+00:00 app[web.1]:
2021-10-20T20:34:21.713756+00:00 app[web.1]: Listening on port 8260
2021-10-20T20:34:21.775734+00:00 heroku[web.1]: State changed from starting to up
2021-10-20T20:34:21.911902+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-10-20T20:34:21.912248+00:00 app[web.1]: npm ERR! errno 1
2021-10-20T20:34:21.926340+00:00 app[web.1]: npm ERR! [email protected] start: `node ./dist/index.js`
2021-10-20T20:34:21.926408+00:00 app[web.1]: npm ERR! Exit status 1
2021-10-20T20:34:21.926490+00:00 app[web.1]: npm ERR!
2021-10-20T20:34:21.926548+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2021-10-20T20:34:21.926604+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-10-20T20:34:21.933864+00:00 app[web.1]:
2021-10-20T20:34:21.933977+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-10-20T20:34:21.934035+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-10-20T20_34_21_928Z-debug.log
2021-10-20T20:34:22.121378+00:00 heroku[web.1]: Process exited with status 1
2021-10-20T20:34:22.265731+00:00 heroku[web.1]: State changed from up to crashed
2021-10-20T20:34:22.288685+00:00 heroku[web.1]: State changed from crashed to starting
2021-10-20T20:34:24.000000+00:00 app[api]: Build succeeded
2021-10-20T20:34:24.543897+00:00 heroku[web.1]: Starting process with command `npm start`
2021-10-20T20:34:25.605048+00:00 app[web.1]: [heroku-exec] Starting
2021-10-20T20:34:25.900511+00:00 app[web.1]:
2021-10-20T20:34:25.900518+00:00 app[web.1]: > [email protected] start /app
2021-10-20T20:34:25.900518+00:00 app[web.1]: > node ./dist/index.js
2021-10-20T20:34:25.900519+00:00 app[web.1]:
2021-10-20T20:34:26.661060+00:00 app[web.1]: Listening on port 33178
2021-10-20T20:34:26.671025+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-10-20T20:34:26.674395+00:00 app[web.1]: npm ERR! errno 1
2021-10-20T20:34:26.676324+00:00 app[web.1]: npm ERR! [email protected] start: `node ./dist/index.js`
2021-10-20T20:34:26.676389+00:00 app[web.1]: npm ERR! Exit status 1
2021-10-20T20:34:26.676462+00:00 app[web.1]: npm ERR!
2021-10-20T20:34:26.676511+00:00 app[web.1]: npm ERR! Failed at the [email protected] start script.
2021-10-20T20:34:26.676557+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-10-20T20:34:26.682667+00:00 app[web.1]:
2021-10-20T20:34:26.682776+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-10-20T20:34:26.682829+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-10-20T20_34_26_677Z-debug.log
2021-10-20T20:34:26.877812+00:00 heroku[web.1]: Process exited with status 1
2021-10-20T20:34:27.141848+00:00 heroku[web.1]: State changed from starting to crashed

Can someone please give me a light? This is the first time I try github actions.

Thanks

Edit.: This is the Deploy the project step information:

Run akhileshns/[email protected]
Created and wrote to ~/.netrc
Successfully logged into heroku
 ›   Warning: Our terms of service have changed: 
 ›   https://dashboard.heroku.com/terms-of-service
Added git remote heroku
Setting MONGODB_URL, AUTH_TOKEN and restarting testjvcapi... done, v5
remote: Compressing source files... done.        
remote: Building source:        
remote: 
remote: -----> Building on the Heroku-20 stack        
remote: -----> Using buildpack: heroku/nodejs        
remote: -----> Node.js app detected        
remote:                
remote: -----> Creating runtime environment        
remote:                
remote:        NPM_CONFIG_LOGLEVEL=error        
remote:        USE_YARN_CACHE=true        
remote:        NODE_VERBOSE=false        
remote:        NODE_ENV=production        
remote:        NODE_MODULES_CACHE=true        
remote:                
remote: -----> Installing binaries        
remote:        engines.node (package.json):  unspecified        
remote:        engines.npm (package.json):   unspecified (use default)        
remote:        engines.yarn (package.json):  unspecified (use default)        
remote:                
remote:        Resolving node version 14.x...        
remote:        Downloading and installing node 14.18.1...        
remote:        Using default npm version: 6.14.15        
remote:        Resolving yarn version 1.22.x...        
remote:        Downloading and installing yarn (1.22.17)        
remote:        Installed yarn 1.22.17        
remote:                
remote: -----> Restoring cache        
remote:        - yarn cache        
remote:                
remote: -----> Installing dependencies        
remote:        Installing node modules (yarn.lock)        
remote:        yarn install v1.22.17        
remote:        [1/4] Resolving packages...        
remote:        [2/4] Fetching packages...        
remote:        [3/4] Linking dependencies...        
remote:        [4/4] Building fresh packages...        
remote:        Done in 7.59s.        
remote:                
remote: -----> Build        
remote:        Running build (yarn)        
remote:        yarn run v1.22.17        
remote:        $ rimraf ./dist && tsc        
remote:        Done in 11.61s.        
remote:                
remote: -----> Pruning devDependencies        
remote:        yarn install v1.22.17        
remote:        [1/4] Resolving packages...        
remote:        [2/4] Fetching packages...        
remote:        [3/4] Linking dependencies...        
remote:        [4/4] Building fresh packages...        
remote:        warning Ignored scripts due to flag.        
remote:        Done in 3.65s.        
remote:                
remote: -----> Caching build        
remote:        - yarn cache        
remote:                
remote: -----> Build succeeded!        
remote: -----> Discovering process types        
remote:        Procfile declares types     -> (none)        
remote:        Default types for buildpack -> web        
remote: 
remote: -----> Compressing...        
remote:        Done: 41.9M        
remote: -----> Launching...        
remote:        Released v6        
remote:        https://testjvcapi.herokuapp.com/ deployed to Heroku        
remote: 
remote: Verifying deploy... done.        
To https://git.heroku.com/testjvcapi.git
   f9075d9..1192ce7  HEAD -> main

Solution

  • As Bertrand said, there was something wrong with the mongodb connection, so I just found that the variable MONGODB_URL was not set, only the test one was, so it was making the app crash because it was undefined for mongoose, making it exit.