We have the Git client available at Kinsta so you can SSH in and pull your own Git repo down from Github, Gitlab, Bitbucket, or any other third-party. SSH access is available on all of Kinsta’s hosting plans.
If you’re just starting out, make sure to read our guide on git vs Github.
However, we don’t yet have the feature released where you can just
git push kinsta my_site and it automatically gets deployed. This part is coming. But you can still easily use Git at Kinsta, just follow the instructions below.
How to Use Git
You can create a deployment script in a few minutes that will SSH into your Google Cloud Linux container and pull down the latest version of your repo. Your SSH details for your Kinsta site can be found on the “Info” page of your site.
ssh [email protected] -p PORT "cd /www/my_site/public && git pull"
Another way of doing this is to utilize WP Pusher. Many of our clients use this and it makes it super easy, as you don’t have to know how to use Git or SSH.
Austin also has an excellent tutorial on how to set up automatic Git deploy with Kinsta using SSH.
To clone a repo:
git clone https://github.com/USER/REPO.git
When using Git for private repositories, you use your GitHub username and password which are passed to the server.
git clone https://username:[email protected]/USER/REPO.git
If two-factor authentication is enabled, an OAuth token must be used in place of your standard credentials. Follow these instructions on how to create an OAuth token.
To clone a repo that has 2FA enabled:
git clone https://[email protected]/USER/REPO.git
If it’s a private repo, credentials need to be added to the git pull:
ssh [email protected] -p PORT "cd /www/my_site/public && git pull https://username:[email protected]/USER/REPO"
Private Repo With 2FA
If two-factor authentication is enabled, an OAuth token must be used in place of your standard credentials. Follow these instructions on how to create an OAuth token. To deploy a repo that has 2FA enabled:
ssh [email protected] -p PORT "cd /www/my_site/public && git pull https://[email protected]/USER/REPO"
If the git pull above doesn’t contain credentials and the HTTPS path, it will attempt to look locally (rather than at the hosted repo) and throw the message: “Everything up-to-date.”
Dealing With Conflicts Found
If you change something on remote the deployment commands above will abort due to conflicts found. What should you do then? It depends on which one you want to treat as “King.” In this example below, we will treat the Git repo as “King” and forget the conflicts.
Fair warning. The following overwrites changes that exist on remote with what is in the Git repo.
ssh [email protected] -p PORT "cd /www/MY_SITE/public && git fetch https://[email protected]/USER/REPO.git && git reset –hard kinsta/mysite"
If you have questions regarding how to use Git at Kinsta our support department will be happy to help once you’re up and running.
How to Auto Deploy to Kinsta with GitLab CI/CD (Advanced)
For more advanced users, GitLab CI/CD (continuous integration/continuous delivery) can be used to automatically push code changes to your Kinsta site whenever a new commit is pushed to the relevant branch. This method allows you to continuously push code to your live environment on Kinsta without overwriting the WordPress MySQL database.
Create a Kinsta Site (optional)
For the purposes of this tutorial, we’ll be creating a new empty site on Kinsta, and we will install WordPress later on. If you already have a Kinsta site, you can still follow along with this tutorial.
In MyKinsta, go to your “Sites” page and click Add Site in the upper right corner. Select the “Don’t Install WordPress option, name your site, select a location, and click the Add Site button to finish the site creation process.
Create a GitLab Project
To get started, head over to GitLab and click Create a Project to create a new repository for your Kinsta site.
Select the Blank Project option, and fill in the “Project Name” and “Project Slug” fields.
For the “Visibility Level” option, we recommend making the repository private. Since this repository may contain premium plugin and theme code, leaving it accessible over the public Internet would allow anyone to download the products you paid for.
Once you’re finished configuring the new project, click Create Project to continue.
Configure SSH Keys
Next, you’ll need to add SSH keys to GitLab and MyKinsta to allow the two platforms to communicate with each other. Let’s start with adding an SSH key in GitLab.
Add SSH Public Key to GitLab
First, SSH into your Kinsta live environment, and run the
cat command below to display your SSH public key.
Your public key should look similar to the one below. Keep your Terminal window open for the now, and go back to GitLab in your web browser.
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC7zdjwd6UIUJ5t4YnXNi6yhdHgrvIV2xgjvJ3592wd1Hzmnta4dau7yOIxQDU7/oNIr6DIskuIUii5ruGi4YZpIwB/AWGrn/uWb+FXJFiQHMZq9rCFcqtZXT0fyzKiSNkblV4F1RaPQJRvxfSUsSQ3jJU3FX8CF1c4R40CiBKkSHM8uavVIIESzgIHRiWVxkL9F6SKzg8GeTctJaAa3W+q1F1T60OKYmzH3OLdA37ZNmkm1CYWa8SF0JjOszxOnPwhfQ49P5r+rftXRFLz/7hGJ8CnUyzErSiUdUKNknUAB+w4KEZshQSNuiken0+GKqIw83dWWSyjiJJigcyDez2s+3AqDLHPG45NoBEXuBXjPQ9C08hokVyKlbzd/P2qvcnzUT5S6zTuaYOW+50+fiXeYkJlEoYYxoGRVx6FFdFqWyJx5UyaDv7QY3iQH0qct1iq9XGXMhBxIecIAEPUwF8nOp15in8L+5UIFMiNnihztTAXysc+8xvVwbuRlQIeR/E= ansible-generated on fvj-kinstagit
Click on the user icon dropdown in the upper right corner of GitLab, and click Settings.
On the “Settings” page, click on SSH Keys in the sidebar.
Paste your SSH public key into the text field. The title field should auto-populate with a name, but feel free to change it if you’d like. GitLab also allows you to set an expiration date for the SSH key. This option is useful if you invalidate and rotate SSH keys as part of your infrastructure security protocol. If you don’t, feel free to leave the “Expires At” field blank.
After you’ve double checked your configuration, click Add Key to finalize the process.
Add SSH Public Key to MyKinsta
Now that your SSH public key has been to GitLab, you’ll need to add the same key to MyKinsta. In MyKinsta, click on your user icon in the lower left corner and click User Settings. This step adds the Kinsta public key to your site’s
authorized_keys file (only writable by root and MyKinsta), and will allow GitLab to SSH into your site to push code changes.
Scroll down and click Add SSH Key.
In the modal window, specify a name for the SSH key, paste your public key in the text field, and click Add SSH Key to finalize the process.
Add SSH Private Key to GitLab
Next, you’ll need to add your Kinsta live environment’s SSH private key as an environment variable in GitLab. The private key will allow GitLab to SSH into your Kinsta site to push code changes directly.
To find your private key, run the Terminal command below in your Kinsta live environment. Be sure not to share your SSH private key anywhere outside GitLab.
In GitLab, click on the project you created earlier. In the sidebar, hover over “Settings” and click CI/CD.
Scroll down to the “Variables” section, click the “Expand” button, and click Add Variable. Use
SSH_PRIVATE_KEY for the key name, paste your private key into the text field, and click Add Variable to continue.
Be sure you are adding your SSH private key. It should start with
-----BEGIN OPENSSH PRIVATE KEY-----
After the variable has been added, you’ll be able see it in your repository settings.
Clone GitLab Repo to MyKinsta
Now that the necessary SSH keys have been added, you can clone your GitLab repository to your Kinsta live environment. In GitLab, navigate to your repository, click on the blue Clone button, and copy the URL under “Clone with SSH”.
Next, SSH into your Kinsta live environment and navigate to your home directory. If you are unsure how to get to your home directory, use the command below.
Once you’re in the home directory, run the
git clone command with the URL you copied from GitLab.
git clone [your-repository-url-here]
After cloning the repository, you’ll see a warning that says “you appear to have cloned an empty repository”. This is to be expected because the repo is empty at the moment.
At Kinsta, our Nginx configuration serves content from the
~/public folder. Thus, you’ll want to delete the current
~/public folder and rename your cloned repo folder to
~/public. To do this, run the two commands below.
rm -rf ~/public mv ~/your-repo-folder-name ~/public
Configure GitLab Repo
Next, use the command below to navigate to the
Run the two commands below to configure your GitLab repo. Be sure to specify a valid email address and a username of your choice.
git config --global user.email "[email protected]" git config --global user.name "brian"
Install WordPress with WP-CLI
Now that the GitLab repo has been cloned to your Kinsta live environment and configured, let’s install WordPress. You can either install WordPress manually by downloading it from wordpress.org, or you can install it with WP-CLI. For this example, we’ll use WP-CLI to install WordPress with the command below in the
wp core download
After installing WordPress, your
~/public folder should look like this.
Next, go to the “Domains” page in MyKinsta for your site, and visit your “Primary Domain”.
You’ll be greeted with WordPress’ five minute installation process. If you’re unsure about how to set up WordPress, we have an in-depth guide here.
After WordPress has been installed, you should be able to see your new WordPress site by visiting your primary domain.
Configure the GitLab CI/CD Pipeline
Next, you’ll need to set up a special configuration file to instruct GitLab to initiate automatic deployments after the repo’s master brand has been updated. To do this, navigate to the
~/public folder in your Kinsta live environment and create a new file called
.gitlab-ci.yml with the command below.
cd ~/public && touch .gitlab-ci.yml
After the file has been created, add the following text to the file. You can either edit the file with nano or vim in Terminal, or with an SFTP editor. If you use a SFTP client, be sure to enable hidden files in order to see the
before_script: - apt-get update -qq - apt-get install -qq git # Setup SSH deploy keys - 'which ssh-agent || ( apt-get install -qq openssh-client )' - eval $(ssh-agent -s) - ssh-add <(echo "$SSH_PRIVATE_KEY") - mkdir -p ~/.ssh- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' deploy_live: type: deploy environment: name: Live url: your-primary-domain script: - ssh [email protected] -p port-number "cd public-root && git checkout master && git pull origin master && exit" only: - master
Be sure to change the
script parameters in the
deploy_live block. You’ll want to replace the bolded values with your site’s settings from the MyKinsta dashboard.
- Replace your-domain with your site’s primary domain.
- Replace ssh [email protected] -p port-number with the “SSH Terminal Command” in MyKinsta.
- Replace public root with the “Path” in MyKinsta.
only: - master section of the file refers to the repository branch that will trigger GitLab’s CI/CD process. With this configuration, pushes to the
master branch will initiate a deployment to your Kinsta live environment. If you would like to specify another branch to trigger deployment, feel free to change the configuration.
If you’d like to implement additional granularity to the configuration to account for a staging environment, you can add an additional
deploy_staging block with the URL and SSH details for your Kinsta staging environment.
deploy_staging: type: deploy environment: name: Staging url: your-staging-domain script: - ssh [email protected] -p port-number "cd public-root && git checkout master && git pull origin staging && exit" only: - staging
only: - staging section as well – this means pushes to the
staging branch will initiate GitLab’s CI/CD process.
Initial Commit to GitLab
Now that WordPress and the GitLab configuration file are set up, let’s make an initial commit to GitLab. To do this, we’ll create a new branch called
v0.1, start tracking the files in the repo, commit the changes, and push the changes to GitLab on the
v0.1 branch. Run the commands below from
~/public to trigger the initial commit.
git checkout -b v0.1 git add . git commit -a -m 'Initial Commit' git push origin v0.1
If you check your repository in GitLab, you’ll now see the files from your initial commit.
Configure Kinsta Staging Environment
The next step is configure a Kinsta staging environment so you have a safe place to test out new plugins, themes, and code without impacting your production environment. To do this, first create a staging environment in MyKinsta.
SSH into the staging environment, and run the command below to check for any code changes.
cd ~/public && git status
You should see a message like the one below. As you can see, the branch is the
v0.1 one that was created earlier. In addition, the
wp-config.php file has changed. This happens because Kinsta adds a few additional lines to
wp-config.php during the push from live to staging.
Commit and Push Changes to Live
Finally, let’s make some changes to the staging environment and push a new commit to the Kinsta live environment using GitLab. Keep in mind the purpose of this method is to bypass Kinsta’s normal “push to live” feature which overwrites the WordPress database as well.
For this example, let’s install the Yoast SEO plugin using WP-CLI with the command below.
cd ~/public && wp plugin install wordpress-seo
After the Yoast SEO plugin has been installed, run the commands below to make a commit and push the changes to the
git add . git commit -a -m ‘Installed Yoast SEO plugin.’ git push origin v0.1
Finally, let’s switch over to the
master branch, merge
master, and push
master to GitLab with the commands below. This will cause GitLab to initiate a deployment process to your Kinsta live environment because of the
only: - master setting in the GitLab configuration file.
git checkout master git merge v0.1 git push origin master
In GitLab, go to CI/CD > Jobs in your repo’s sidebar, and you’ll see the job running. This “job” is GitLab pushing the Yoast SEO plugin to your Kinsta live environment.
After the job is finished, run
wp plugin list on your Kinsta live environment. You should see
wordpress-seo – the Yoast SEO plugin that was installed on the staging environment earlier.
If you’re a theme or plugin developer who works with Kinsta sites, setting up auto deploys via GitLab CI/CD can help speed up your workflow. This setup allows you to deploy code from either your local environment or Kinsta staging environment without having to overwrite the MySQL database on your live production site.
If you enjoyed this tutorial, then you’ll love our support. All Kinsta’s hosting plans include 24/7 support from our veteran WordPress developers and engineers. Chat with the same team that backs our Fortune 500 clients. Check out our plans