Integrate Github Actions with LocalWP
Last updated
Last updated
In this guide, we'll walk through the process on how to create a GitHub WorkFlow that'll sync any changes you make to your website locally to the production website.
LocalWP - WordPress Website
GitHub Desktop - Sync Code
VSCode - Edit Code
.gitignore - Sync only theme and plugins. (Credit: LocalWP )
main.yml - Sync main rep with production website. (Credit: Cross-Link)
Check out here for how you can utilize CloudPanel on a droplet. With that in mind we will focus on the bottom part of this flow chart. All work will be associated with the "Local WordPress Environment". These changes will not automatically synchronize with the production server. To manage version control and collaboration, GitHub Desktop will be used for syncing your repository and pushing all modifications to a development branch. This ensures all changes are coordinated without impacting the live server.
After completing and approving your work locally, you’ll merge it into the main repository. This action triggers a GitHub workflow, automating the deployment of your new theme. Consequently, the main repository’s content is pushed to the droplet, seamlessly updating the website with your latest theme.
Local WordPress is very simple to setup and really doesnt need much explanation to get running. Simply install the application, follow the installation prompts, and once done select "Create a new site"
Select "Go to site folder" and save this path for the next part of this guide. It should look similar to this below...
Once installed you will need to sign into your github account. Once done we will then create a new repository and point it to the theme we wish to sync.
Go into Command Prompt and run as admin. From there type something similar to this below...
mklink - Creates a directory or file symbolic or hard link.
"/D" - Creates a directory symbolic link.
"C:\Users\WinUser\Documents\websites\unluckytheme-wp" - Path to create the symbolic link.
"C:\Users\WinUser\Local Sites\unluckytechxyz\app\public\wp-content" - Path to website.
There is plenty you can configure with VSCode but we don't necessarily need to do much for our case. Install the application and select the first icon on the top left. From there we can use it as our code editor.
With the Website PATH, open wp-content in VSCode so we can add the necessary scripts for our workflow.
Get the template here. We want to take a look at two specific parts of the template.
Replace "TEMPLATE" with your custom theme.
If you have any plugins you need to sync then uncomment and add as needed here...
Once changed save the file in your websites wp-content.
From GitHub Desktop select "Create a New Repository on your local drive..." and input the necessary info for your website.
Name - The name of you repository. This should be the same name you set for your symlink.
Local Path - Location to your symbolic link.
For "Local Path" do not include the symbolic link in your path.
CORRECT
INCORRECT
SSH into your production server and enter the command below to generate the necessary keys.
RSA is outdated refer here for further explanation.
Alternatives...
Once you generated your keys type this below to create and add your public key to the file.
Take your public key and add it to your GitHub account. This way github can have access to our production server.
Profile -> Settings -> SSH and GPG Keys -> New SSH key
Before we create our WorkFlow we need to define a few secrets from within our new repository. From your repository...
Settings -> Secrets and variables -> Actions -> Secrets
SSH_PRIVATE_KEY
RSA PRIVATE KEY
REMOTE_HOST
IP ADDRESS
REMOTE_USER
USER
REMOTE_PORT
22
REMOTE_TARGET
/home/USER/htdocs/DOMAIN/wp-content
From your repository select "Actions" -> "set up a workflow yourself". Paste this template into your workflow and name it "main.yml". Briefly we will go over the script and what it does.
"name" displays the names of your workflows under your repository's "Actions" tab
When a push is made to the main branch the workflow will trigger.
A virtual machine will be made to temporarily host and run the commands in the script.
We take the secrets we defined earlier and specify them in the script.
The script will then initiate and begin copying the main repository into the production server.
If you need version control enabled then uncomment and specify the necessary version you need for your website.
If you already have a repository with a similar format to what we did above then you can also clone it instead of creating a new one. Create the symbolic link and name the directory the repository name. Clone the repository and save all files within wp-content.
In GitHub Desktop, select "Add existing repository" and paste the path to your Symbolic Link like below...
You should now have your repository synced with GitHub Desktop.
Now whenever you update your main branch, your website will automatically update with the necessary changes you have made locally.