Blog

Automatic deploys from GitHub to shared hosting, even when the server can't build

A push to main that becomes a build and then a release into public_html

My site is built with Astro. Before it goes live it has to be built with npm run build, which turns the code into HTML pages inside the dist/ folder. The hosting is shared, on Hostinger: no Node.js on the server, just files.

For weeks I published like this: build on my computer, zip, File Manager, extract. It works, but it’s slow, and things get left behind. In my case the most important file got left behind.

Now I push to GitHub and about a minute later the site is updated. Here’s how.

The problem: Hostinger’s Git doesn’t build

Hostinger has a GitHub integration: you connect a repository and a branch, and every push to that branch lands in the site’s folder. But, as its documentation says, it runs no build: the files in the repository are the files that get served.

My repository holds the source code, though, not the built site. If I connected main, public_html would get src/, package.json and the rest. The site wouldn’t work, and the code would be readable by anyone. With a manual upload, that really happened to me: I tell the story in I put my own site through vaglio.

The fix: a branch that holds only the site

The idea is to split the work:

  1. GitHub Actions builds the site on every push to main.
  2. It puts the result, and only that, on a separate branch: deploy.
  3. Hostinger is connected to the deploy branch and copies those files into public_html.

The flow: a push to main starts the build on GitHub, the result goes to the deploy branch, Hostinger copies it into public_html

The deploy branch has no source code, no node_modules, no README. It has exactly what belongs in public_html: the pages, the images and .htaccess.

The workflow

It goes in .github/workflows/deploy.yml:

name: Deploy

on:
  push:
    branches: [main]
  workflow_dispatch:

permissions:
  contents: write

concurrency:
  group: deploy
  cancel-in-progress: false

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version-file: .nvmrc
          cache: npm

      - run: npm ci
      - run: npm run build

      - name: Check that .htaccess is there
        run: test -f dist/.htaccess && test -f dist/index.html

      - name: Publish dist/ to the deploy branch
        env:
          SHA: ${{ github.sha }}
        run: |
          rm -f dist/_redirects dist/_headers
          git config --global user.name "github-actions[bot]"
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          if git ls-remote --exit-code --heads origin deploy >/dev/null; then
            git fetch --depth 1 origin deploy
            git worktree add --detach out FETCH_HEAD
            git -C out checkout -B deploy
          else
            git worktree add --detach out
            git -C out checkout --orphan deploy
          fi
          git -C out rm -rf --quiet . || true
          cp -a dist/. out/
          git -C out add -A
          if git -C out diff --cached --quiet; then
            echo "No changes to the site"
            exit 0
          fi
          git -C out commit -m "Site from ${SHA::7}"
          git -C out push origin deploy

The details that matter:

  • permissions: contents: write lets the Action push to the deploy branch with the token GitHub gives it on its own. No keys to create.
  • git worktree opens the deploy branch in a separate folder, out/, without touching the source.
  • The branch grows by one commit per release instead of being rewritten from scratch. Hostinger always gets an ordinary update, and the history says which source commit produced which version of the site.
  • If the site didn’t change, say after an edit to the README, no commit is made and Hostinger gets nothing.
  • concurrency queues two pushes made close together, so they don’t trip over each other on the deploy branch.

_redirects and _headers only matter on Cloudflare Pages and Netlify. On Hostinger, .htaccess does it all, so I drop them.

Connecting Hostinger

In Hostinger’s panel, under Advanced → Git, there’s Connect with GitHub. It installs Hostinger’s app on your GitHub account, and it works with private repositories too: no SSH key to copy.

Then choose:

  • the repository;
  • the deploy branch, not main;
  • the target folder, which is public_html by default.

From then on every push to the deploy branch goes live on its own. And since the Action updates the deploy branch, all you do is push to main.

The traps

Hidden files. .htaccess starts with a dot, so on a Mac it’s hidden, and in manual uploads it’s the first thing to get lost. I ran for almost three weeks without it, and with it went the redirects, the 404 page and the security headers. Git treats it like any other file. The Check that .htaccess is there step stops the release if it’s missing.

Folders that aren’t part of the site. My public_html also holds a subdomain’s folder. Hostinger’s documentation says a deploy replaces existing files, but doesn’t say what happens to the ones that aren’t in the branch. Before the first deploy I downloaded a copy of that folder. It survived, but the copy cost nothing.

Old files stay. A deploy adds and replaces, but it doesn’t delete. If you once uploaded something by hand that shouldn’t be there, the deploy won’t remove it: I still had two folders online from an old upload, and had to delete them by hand. Check public_html once and clean up.

The cache. Hostinger has a CDN in front of the site. If you still see the old version after a deploy, clear the cache from the panel.

How long it takes

From push to updated site takes about a minute: just under forty seconds for the build on GitHub, then a few seconds for Hostinger to pull the branch.

The same pattern works with any static site (Astro, Vite, Hugo, Eleventy) and any host that can deploy from a Git branch. Only the build command and the folder to publish change.