How to deploy Statamic CMS application to shared hosting with GitHub action

Published on Oct 21, 2022

If you want to automate your deployment to shared hosting, but you don't have SSH access, you still can handle it via FTP. With GitHub action, you can upload your modified files to your shared hosting space.

You need one GitHub repository with your existing code.

Create your FTP access in your shared hosting control panel, and save the credentials to your repository secret keys.
GitHub Action secret keys

As you can see in the picture, you have to navigate to the Settings in your GitHub repository and select on the left side the "Secrets" and "Actions" menus. Click on the "New repository secret" button. The "Name" of the secret will be used in our actions, the name has to be screaming snake case format, and the "Secret" field will be for example the name of the hosting or the password.

GitHub new secret key

I call the keys like FTP_HOST, FTP_PASSWORD, FTP_USER (and one more key for the Statamic license key STATAMIC_LICENSE_KEY.

For the Statamic license key create your site on the official Statamic website: Statamic create license key

Now you should create the GitHub action file. Place this file in the .github/worklows folder and name it deploy.yml.

My GitHub action file looks like this:

name: Deploy Site after Push to master branch
on:
  push:
    branches:
      - master
jobs:
  web-deploy:
    name: Deploy
    runs-on: ubuntu-latest
    steps:
      - name: Get the latest code
        uses: actions/checkout@v2.3.2
      - uses: actions/setup-node@master
      - name: Installing project dependencies
        run: npm install
      - name: Building the project
        run: npm run production
      - name: Copy .env
        run: cp .env.hosting .env
      - name: Add .env variables
        run : |
          echo APP_ENV=production >> .env
          echo APP_DEBUG=false >> .env
          echo APP_URL=https://YOURSITEDOMAIN.com >> .env
          echo STATAMIC_LICENSE_KEY=${{ secrets.STATAMIC_LICENSE_KEY }} >> .env
      - name: Install Dependencies
        run: composer install --no-dev
      - name: Generate key
        run: php artisan key:generate
      - name: Cache clear & stache clear
        run: php artisan cache:clear && php please stache:clear && php please cache:clear
      - name: Directory Permissions
        run: chmod -R 777 storage bootstrap/cache
      - name: Sync files
        uses: SamKirkland/FTP-Deploy-Action@4.0.0
        with:
          server: ${{ secrets.FTP_HOST }}
          username: ${{ secrets.FTP_USER }}
          password: ${{ secrets.FTP_PASSWORD }}
          server-dir: YOUR_SERVER_DIR/public_html/

This will install the npm and composer dependencies. It will copy our .env.hosting (please make this file to your repository) file content and it will write the ENV variables to the end of the .env file. The .env.hosting file does not contains the APP_ENV, APP_DEBUG, APP_URL and STATAMIC_LICENSE_KEY variables. Please change the server-dir folder where you want to upload your files.

After the action clears the caches, it gives permission to the folders and at the end of the process, it will upload the modified files to the FTP space.

Afterward, for the first time, it will upload all the folders and files (like the vendor folder) and it should take 1-2 hours. After the first upload is completed, the next uploading process will only take a few minutes per push. With this GitHub action, our action is triggered when we push to the master branch, however, the master branch can be changed to any other branch.

If you can't change the domain default folder you need to upload all your files to the public_html folder and add one .htaccess file to the root folder in the Statamic application.

RewriteEngine On
Options +FollowSymlinks
RewriteBase /

RewriteCond %{HTTP_HOST} ^.*$
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.*)$ public/$1 [L]

This code block will tell your Apache to redirect the incoming request to the public folder.

That's all! Feel free to contact me if you have further questions. 😉