skip to Main Content

I want to auto deploy my private repository on my VPS whenever I push changes to my main branch. My yaml file looks like this:

name: push-and-deploy-to-server

on:
  push:
    branches: [ main ]

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: checkout repo
        uses: actions/checkout@v2
      - name: ssh and deploy
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          port: 22
          script: |
            git pull origin main
            git status
            npm install --only=prod
            pm2 restart index.js

this is not working, I get the following error:

err: fatal: could not read Username for 'https://github.com': No such device or address

When ssh-ing into my server and cloning the repo myself, it asks for my username and password (access token). When I provide it, it works, but with the yaml file, it doesnt.

How can I clone and deploy a private repo? It’s a nodejs project btw.

2

Answers


  1. Considering your setup

    1. private GitHub repository with given action on that repo.
    2. VPS

    What does your current configuration and github secrets setup do?

    1. you push code to your private repo
    2. action runs and using appleboy/ssh-action@master ssh’s into your VPS
    3. then executes your commands like git pull origin main in your VPS.

    Issue what you have is that your VPS is not authenticated to access your repository.

    You have multiple options.

    1. ssh to your VPS as user ${{ secrets.SSH_USERNAME }} and authenticate that user against github using your Github Personal access token which you can generate under your https://github.com/settings/tokens giving it a read repo permissions. Then test that you can clone your repo into VPS if so then your next build will succeed.
    2. Second option generate new ssh key inside VPS for ${{ secrets.SSH_USERNAME }} and add it under your repository settings Deploy keys. When using deploy key, you need to make sure that your repository remote in vps is using [email protected]:<username>/<repository>.git git url not https url.
    3. Third option: use appleboy/scp-action step before appleboy/ssh-action and copy all contents from current directory to your VPS and then run your npm install etc. with appleboy/ssh-action.
    Login or Signup to reply.
  2. You proceed may work if you have git installed on your server. But the scp project can also deploy the code directly to your server.

    name: push-and-deploy-to-server
    
    on:
      push:
        branches: [ main ]
    
    jobs:
      build-and-deploy:
        runs-on: ubuntu-latest
        steps:
          - name: checkout repo
            uses: actions/checkout@v2
          - name: ssh and deploy
            uses: appleboy/ssh-action@master
            with:
              host: ${{ secrets.SSH_HOST }}
              username: ${{ secrets.SSH_USERNAME }}
              key: ${{ secrets.SSH_PRIVATE_KEY }}
              source: "."
              target: "the/server/path"
    

    I used an IP address instead of a domain name, cos I felt like my hosting service was screwing with me.
    Check out this URL for more details

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search