Skip to content

lambirou/git-auto-deploy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Automatic deployment from Github

Use github action to use CI/CD to deploy automatically your project from Github to your server.

Prerequisites

  • Git
  • SSH
  • Github account

Steps

1. Create deployment ssh key on server

cd ~/.ssh && ssh-keygen -t rsa -b 4096 -f deploy_github

2. Create a SSH Config File

To define the private key that is being used to clone the git repository:

nano ~/.ssh/config
cat << EOF > ~/.ssh/config
Host github.com
Hostname github.com
User git
IdentityFile ~/.ssh/deploy_github
EOF

3. Add public ssh key to your repository settings

Get the public key:

cat ~/.ssh/deploy_github.pub

To 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.

  1. Click Add deploy key.

  2. In the Title field, provide a title.

  3. In the Key field, paste your public key.

  4. Select Allow write access if you want this key to have write access to the repository. A deploy key with write access lets a deployment push to the repository.

  5. Click Add key.

4. Create github action file to you repository

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 build

5. Clone the repository to your server

To initialize the CI/CD workflow in your server, run the following command:

git clone [email protected]:{github_user}/{github_repo}.git

Checkout 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.

6. Deploy your project

To deploy your project, push or pull request your changes to the main branch. And see the result in your server.

About

CI/CD : Automatic deployment with Github Actions

Resources

License

Stars

Watchers

Forks