DEV Community

Automate your Laravel app deployment with Github Actions

Kenean on December 10, 2021

Hello again my fellow artisans!! In this article, I will go through the step-by-step process of how you can automate your Laravel app deployment...
Collapse
 
nablidev profile image
nablidev

This is a very helpful article that I use as a reference and keep visiting over and over. However, I leave this note comment as a future reference and help for myself and others.

Because the Laravel app uses npm to build its assets and thus also the deploy.sh script on the server, then the deploy.yml needs to indicate that the github action server also needs npm to build the assets. So to the deploy.yml file you need to add the following:

steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v3
        with:
          node-version: latest
Enter fullscreen mode Exit fullscreen mode

And if you are using nvm on the server to install node then maybe you need to add the following to the deploy.sh script

export NVM_DIR=~/.nvm
source ~/.nvm/nvm.sh
Enter fullscreen mode Exit fullscreen mode
Collapse
 
nablidev profile image
nablidev

thank you for this post, following it helped me set my continuous deployment using laravel and github actions, however some small things are missing in this guide.
For example you need to grant execution permissions on your server for the ./.scripts/deploy.sh in order for it to be able to execute

Collapse
 
kenean50 profile image
Kenean

Thank you for pointing that out, its updated now.

Collapse
 
precampio profile image
precamp-io

Thanks for your post. I have created two workflows one for staging one for prod. When I run $ git fetch

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Collapse
 
nablidev profile image
nablidev

@precampio as @hammykl20 pointed out you need to add github to the ssh known_hosts list like the following:

ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts

However, this is not enough as you have to also add your public key id_rsa.pub to your github account by going to your account's settings, clicking on "SSH and GPG keys," and then clicking "New SSH key."

Then just to be safe I would run the following two commands:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

Collapse
 
hammykl20 profile image
Hammy.kl20
Collapse
 
souravdutt profile image
souravdutt • Edited

When I run following command replacing with my repo ssh key on my vps:
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
it give fatal: No such remote 'origin' error.
Can you please help me in this? do I need to login with github credentials?

Collapse
 
souravdutt profile image
souravdutt

A note to myself and whoever facing similar issues

If deploying a new repo, first need to clone it on server using (make sure both ssh keys have been added on github):
git clone git@github.com:USERNAME/REPOSITORY.git

cloning will auto set origin to current repo but you can check once using below command:
git remote -v
In case no origin found, then add origin using:
git remote set-url origin git@github.com:USERNAME/REPOSITORY.git

as you may need to make some permission changes (such as inside storage directory or .script directory) so ignore permission changes tracking using below command else your next action will fail:
git config core.fileMode false

Collapse
 
asoko2 profile image
Anang Hariyanto N

Hi, i've followed all the instruction, and i ended up getting this error,

Run appleboy/ssh-action@master
Run echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH
Run entrypoint.sh
Will download drone-ssh-1.7.4-linux-amd64 from github.com/appleboy/drone-ssh/rele...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0

100 5580k 100 5580k 0 0 16.5M 0 --:--:-- --:--:-- --:--:-- 16.5M
======CMD======
cd /var/www/travellist && ./.scripts/deploy.sh
======END======
2024/05/05 03:45:01 dial tcp :: connect: connection timed out

i don't know what's wrong, i've tried to add timeout: 3600s to the deploy.yml, but it's still not working

Collapse
 
kenean50 profile image
Kenean

I am not quite sure why you are getting this error but the following points might help debugging

  1. Make sure you don't have firewall rules on your server blocking Github action from connecting with SSH
  2. Make sure you are connecting through the right SSH port on your server
Collapse
 
diazsasak profile image
Diaz Guntur Febrian

Thanks mate..