Search code examples
phplaravelgithubgithub-actionsphp-deployer

Deploying Laravel through Deployer and GitHub Actions always results in Permission denied (publickey,keyboard-interactive)


I have quite a frustrating situation. I am trying to use Deployer to deploy my laravel projects with GitHub Actions and it just keeps kicking me back and giving me a Permission denied (publickey,keyboard-interactive) error.

On the server I want to deploy to (Ubuntu) ... I created a user and then ran the following to generate an SSH key

ssh-keygen -t ed25519 -C "[email protected]"

I then ran the following to create the known_hosts

ssh-keyscan -t ed25519 -p 22342 my.public.server.ip >> known_hosts

I went into my github repo, then to Settings -> Secrets and Variables -> Actions and then created 2 PRIVATE_KEY and KNOWN_HOSTS with the values from the above commands.

My github actions file looks like so;

name: Building

on: [push, pull_request]

jobs:
  build:
    name: Deploy to production
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.1
          extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite
          coverage: none

      - name: Deploy
        uses: deployphp/action@master
        with:
          dep: deploy production -f ./path/to/my/deploy.php
          private-key: ${{ secrets.PRIVATE_KEY }}
          known-hosts: ${{ secrets.KNOWN_HOSTS }}
          deployer-version: "7.1.4"

and finally my deploy.php is as follows;

<?php

namespace Deployer;

require 'recipe/laravel.php';

set('application', 'my_api');
set('repository', '[email protected]:organisation/my-repo.git');
set('git_tty', false);
set('ssh_multiplexing', false);

// Shared files/dirs between deploys
add('shared_files', []);
add('shared_dirs', []);
// Writable dirs by web server
add('writable_dirs', []);

set('allow_anonymous_stats', false);

host('production')
    ->set('hostname', 'my.public.server.ip')
    ->set('port', '22342')
    ->set('remote_user', 'myusername')
    ->set('branch', 'main')
    ->set('deploy_path', '/var/www/api');

// [Optional] if deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');

// Migrate database before symlink new release.
before('deploy:symlink', 'artisan:migrate');

Pretty standard stuff from what I have read ... But it just doesnt seem to work ... When running the action it always gets stuck with the following error;

[email protected]: Permission denied (publickey,keyboard-interactive)

Im going nuts as I have used this deploy.php before without github actions and its worked fine.

Please help


Solution

  • but weirdly its still asking for a password

    That means the public key way not added to the remote server [email protected]/.ssh/authorized_keys.

    On the server I want to deploy to (Ubuntu) ... I created a user and then ran the following to generate an SSH key

    That would create a key pair at [email protected]/.ssh, but would not update the authorized_keys file with the public key generated there.
    You need to add said public key there in order for the GitHub Action (which has the private key as a secret) to be able to SSH connect to the server as myusername.