1 min read
This article also exists in: Arabic

You can deploy very easily on a Linux server by Github Actions so after every push to the main branch, it will deploy automatically to your Linux server.

1- Push condition to the main branch (if bush to the main branch will do step 2)

on:
  push:
    tags:
      - v*.*.*
    branches:
      - main

2- Copy files via SCP, it will copy files from the repo to the Linux server

    - name: Copy files via scp
      uses: appleboy/scp-action@master              
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        port: ${{ secrets.PORT }}
        key: ${{ secrets.SSHKEY }}
        passphrase: ${{ secrets.PASSPHRASE }}
        source: "./public"
        target: ${{ secrets.COPY_TO }}

3- Run script to copy the file on server directory and restart the Nginx or any other action you want to do by ssh or bash syntax

    - name: Move files to correct place
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        port: ${{ secrets.PORT }}
        key: ${{ secrets.SSHKEY }}
        passphrase: ${{ secrets.PASSPHRASE }}
        script: rm -rf ${{ secrets.COPY_LAST }};mv ${{ secrets.COPY_FROM }} ${{ secrets.COPY_LAST }};chcon -Rt httpd_sys_content_t ${{ secrets.COPY_LAST }}; nginx -s reload

We have to create the file on the repository on this path .github/workflows and the file name will be deploy.yml full deploy.yml example to deploy Gatsbyjs site.


name: Deploy On Server

on:
  push:
    tags:
      - v*.*.*
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@master

    - name: Set up Node.js
      uses: actions/setup-node@master
      with:
        node-version: 14.x

    - name: install gatsby cli
      run: npm install -g gatsby-cli

    - name: Install dependencies
      run: npm install

    - name: Build
      run: gatsby build

    - name: Copy files via scp
      uses: appleboy/scp-action@master              
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        port: ${{ secrets.PORT }}
        key: ${{ secrets.SSHKEY }}
        passphrase: ${{ secrets.PASSPHRASE }}
        source: "./public"
        target: ${{ secrets.COPY_TO }}

    - name: Move files to correct place
      uses: appleboy/ssh-action@master
      with:
        host: ${{ secrets.HOST }}
        username: ${{ secrets.USERNAME }}
        port: ${{ secrets.PORT }}
        key: ${{ secrets.SSHKEY }}
        passphrase: ${{ secrets.PASSPHRASE }}
        script: rm -rf ${{ secrets.COPY_LAST }};mv ${{ secrets.COPY_FROM }} ${{ secrets.COPY_LAST }};chcon -Rt httpd_sys_content_t ${{ secrets.COPY_LAST }}; nginx -s reload

About the author

For the last decade, Shady Nagy has worked with a variety of web technologies. He is currently focused on code quality. On his day to day job, he is working as a team leader engineer. As a digital nomad, he is living where the WiFi and sun are 😎
Do you want to know more? Visit my website!