ssh-action icon indicating copy to clipboard operation
ssh-action copied to clipboard

Env var not detected and Knex.js migration not working from Github Action

Open pradosh987 opened this issue 5 years ago • 5 comments

I have GitHub action which deploys NodeJS app and does migrate using "migrate": "knex migrate:latest --env production" script. Script works perfectly if I run it manually from terminal but fails with authentication error for database when run from GitHub action. Is this because of environment variables? Does commands executed from GitHub action have access to environment variables or not? Is there a better way to do this?

here's my GitHub action:

name: Deploy

on:
  push:
    branches:
      - master

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
    - name: SSH and deploy node app
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.SERVER_IP }}
        username: ${{ secrets.SSH_USERNAME }}
        key: ${{ secrets.SSH_KEY }}
        port: 22
        script: |
          cd ~/app/app
          git pull origin master
          npm install --production
          npm run build
          npm run migrate
          pm2 restart app

Here's the error from GitHub action logs

out: > knex migrate:latest --env production
135
out: 
136
out: Requiring external module ts-node/register
137
out: Using environment: production
138
err: error: password authentication failed for user "***"
139
err:     at Parser.parseErrorMessage (/home/***/odin/odin/node_modules/pg-protocol/src/parser.ts:357:11)
140
err:     at Parser.handlePacket (/home/***/odin/odin/node_modules/pg-protocol/src/parser.ts:186:21)
141
err:     at Parser.parse (/home/***/odin/odin/node_modules/pg-protocol/src/parser.ts:101:30)
142
err:     at Socket.<anonymous> (/home/***/odin/odin/node_modules/pg-protocol/src/index.ts:7:48)
143
err:     at Socket.emit (events.js:315:20)
144
err:     at addChunk (_stream_readable.js:295:12)
145
err:     at readableAddChunk (_stream_readable.js:271:9)
146
err:     at Socket.Readable.push (_stream_readable.js:212:10)
147
err:     at TCP.onStreamRead (internal/stream_base_commons.js:186:23)
148
err: npm ERR! code ELIFECYCLE
149
err: npm ERR! errno 1
150
err: npm ERR! [email protected] migrate: `knex migrate:latest --env production`

Regards Pradosh Gaonkar

pradosh987 avatar Sep 20 '20 04:09 pradosh987

Solved with workaround of sourcing environment variables from file inside script

source /etc/profile.d/app-env.sh

If anyone could explain I would like to know the issue in more details.

pradosh987 avatar Sep 20 '20 07:09 pradosh987

Similar to #21

appleboy avatar Sep 20 '20 14:09 appleboy

Yeah, same problem. Any idea why that happens? Environment variables are present on destination machines, so docker or no docker, ssh should be able to pick them up.

pradosh987 avatar Sep 20 '20 15:09 pradosh987

@pradosh987 I guess this is your issue.

shmshd avatar Nov 01 '20 07:11 shmshd

Why would the user settings not load?

nathanhannig avatar Nov 23 '20 06:11 nathanhannig