DEV Community

Cover image for PHP deployment to SSH server using Github action and bash script
es404020
es404020

Posted on

PHP deployment to SSH server using Github action and bash script

In this tutorial we would lean how to deploy our PHP code to a remote server using github action .

Step 1.

ssh into remote server by

for Windows users use Power shell, while Mac users use Terminal.

yyyy : username
xxxx.xxxx.xxxx : Remote IP address
-p :Reference to a port
ZZZZ: port number

Step 2

In the root directory on your linux server create a bash file and write into by using


touch phpdeploy.sh && vi phpdeploy.sh

Step 3

To start typing on the vi editor type press the "a" key and input the following

echo "Deploying changes..."


cd /var/www/html

DIR="ECOMM"

if [ -d "$DIR" ]; then

  cd DIR
  echo "Pull update "
  git  pull 
fi

else 
 echo "Clone repo"
 git clone 
 https://username:password@github.com/username/projectname.git


fi

Enter fullscreen mode Exit fullscreen mode

To exit insert mode on vi editor press escape key followed by shift column and type wq(save and quite).

Step 4

This is our final step, this involves creating our .github/main.yaml file in the root of our project.

name:project name
on:
  push:
    branches: main
jobs:
  build-and-deploy:
    name: Build and deploy next site
    runs-on: ubuntu-latest
    steps:
        - name: copy file via ssh key
        uses: appleboy/scp-action@master
        env:
          HOST:  XXXXXXX
          PORT:  XXXXX
          USERNAME:  XXXXX
          PASSWORD: XXXXXX
          command_timeout: 100m
          script: sh phpdeploy.sh

Enter fullscreen mode Exit fullscreen mode

For every time your code is merger to "main" branch the yaml file above would execute.Thanks for reading.

Top comments (0)