Use github action to use CI/CD to deploy automatically your project from Github to your server.
- Git
- SSH
- Github account
cd ~/.ssh && ssh-keygen -t rsa -b 4096 -f deploy_githubTo define the private key that is being used to clone the git repository:
nano ~/.ssh/configcat << EOF > ~/.ssh/config
Host github.com
Hostname github.com
User git
IdentityFile ~/.ssh/deploy_github
EOFGet the public key:
cat ~/.ssh/deploy_github.pubTo add the public ssh key to your repository settings, go to your repository settings.
In Github on the following link: https://github.com/{github_user}/{github_repo}/settings/keys.
NB : Replace {github_repo} with your github username and {github_repo} with your repository name.
-
Click
Add deploy key. -
In the
Titlefield, provide a title. -
In the
Keyfield, paste your public key. -
Select
Allow write accessif you want this key to have write access to the repository. A deploy key with write access lets a deployment push to the repository. -
Click
Add key.
In your project folder create folder .github/workflows and create a file with name prod.yml and add the following code
name: 🚀 Deploy main branch
on:
push:
branches: [main]
jobs:
deploy:
name: 🛠 Deploy & Build
runs-on: ubuntu-latest
steps:
- name: Deploy to cloud server
uses: appleboy/ssh-action@master
with:
username: ${{secrets.SSH_USER}}
host: ${{secrets.SSH_HOST}}
port: ${{secrets.SSH_PORT}}
password: ${{secrets.SSH_PASSWORD}}
script: |
cd ${{secrets.SSH_APP_PATH}}
git pull origin main
composer install --no-interaction --prefer-dist
php artisan migrate --force
php artisan cache:clear
npm install --force
npm run buildTo initialize the CI/CD workflow in your server, run the following command:
git clone [email protected]:{github_user}/{github_repo}.gitCheckout the branch you want to deploy:
cd {github_repo}
git checkout {branch_name}NB : Replace {github_repo} with your github username, {github_repo} with your repository name and {branch_name} with the name of the branch you want to deploy.
To deploy your project, push or pull request your changes to the main branch.
And see the result in your server.