Deploy code to shared hosting using Git actions

Using Git is a great way to track changes to your code, control who can work on it, and manage how features are developed.

However, figuring out how to get your code from your repository to your server can be difficult. Since version control systems like Git aren’t particularly interested in this aspect of your development process, you have to choose a way that works best for you. We’ll use GitHub actions and GitHub to deploy our website to the shared hosting.

That website is going to have a staging branch and a master branch so that master branch is going to go to our production and the staging branch is going to go to our staging server. We can see the changes we made and if we approve them then we can push those changes to production or the live website. So that is what we are going to be doing in this article.

So first of all let’s create our project with this file structure.

             -GitHub-actions

                             -.github

                                             -workflows

                                                             master.yml

                                                             staging.yml                                                    

             index.html

Now copy paste the following code to master.yml

[code lang="yml"]
on:
  push:
	branches: [ master ]
name: Deploy website on push
jobs:
  web-deploy:
	name: Deploy
	runs-on: ubuntu-latest
	steps:
	- name: Get latest code
  	uses: actions/checkout@v2
	- name: Sync files
  	uses: SamKirkland/FTP-Deploy-Action@4.1.0
  	with:
        server: expressosoft.com
    	username: ${{ secrets.ftp_username }}
    	password: ${{ secrets.ftp_password }}
    	server-dir: /yourdomain.com/public_html/
[/code]

Copy the following code to staging.yml

[code lang="yml"]
on:
  push:
	branches: [ staging ]
name:  Deploy website on push
jobs:
  web-deploy:
	name: Deploy
	runs-on: ubuntu-latest
	steps:
	- name: Get latest code
  	uses: actions/checkout@v2
	- name:  Sync files
  	uses: SamKirkland/FTP-Deploy-Action@4.1.0
  	with:
    	server: expressosoft.com
    	username: ${{ secrets.ftp_username }}
    	password: ${{ secrets.ftp_password }}
server-dir: /yourdomain.com/public_html/
[/code]

Setting up ftp password and ftp username in secrets:

1.       Select the repository you want to deploy using git actions

2.       Go to the “Settings -> Secrets” tab

3.       Add new secret for ftp username and password

4.       Save settings.

Now add following code to your index.html file.

<!DOCTYPE html>
<html>
<head>
   	<title>This is Deploy</title>
</head>
<body>
   	<h1>deploy to staging branch</h1>
</body>
</html>

Now push the code to staging branch and it should automatically deploy to your website.

Share -

related blog

What is SEO & how does it work? (Part-1)

Shefaul Shifat

October 6, 2022

Why you should choose Elementor?

Sharif Shadab Alam

September 11, 2022

view all

Schedule Meeting

Discuss now
Schedule an initial FREE 30 minute consulting call to discuss your needs.