21

I want to use Github Pages to publish a production website (at, say, www.example.com) and also a staging version of the same site (at, say, staging.example.com).

I've set up a repo (e.g. my-jekyll-website) in Github for the production site, created a gh-pages branch, and set the CNAME file in the root level of the repo to www.example.com. Works great.

Now, what's the least painful way to also be able to push changes to staging.example.com?

One thing I've tried is setting up a second remote repo on Github (e.g. my-jekyll-website-staging), then adding this as a remote to my local git config. Works great, except that the CNAME file in the repo needs to be changed to staging.example.com for this to work. Which is no good, because then when I want to deploy to production, I'd need to change the CNAME file back to www.example.com. Not ideal.

I've looked at using git filter content drivers, but if I understand correctly, that's about modifying files locally, not getting different versions of a file pushed to different remotes.

Edit: I've also considered not using a custom domain for staging at all: i.e., just using username.github.io/my-jekyll-website-staging. Unfortunately, then my site-root-relative links (e.g. /images/foo.bar) break, because the baseurl is now different between production and staging.

5
  • 1
    Couldn't you just merge the staging git branch into the production branch and push? You could just keep the CNAMEs permanently pointing to their respective repos, but manipulate the repos separately. Commented Aug 10, 2015 at 3:20
  • @JonathanChan When I merge my local staging branch into my local gh-pages branch, the CNAME setting from the staging branch comes over too. If I then push gh-pages to my production remote, the CNAME will be wrong. I'm trying to avoid having to manually adjust the CNAME record when pushing different branches to different remotes.
    – Jacob
    Commented Aug 10, 2015 at 4:32
  • 2
    Do as @JonathanChan says, then hijack your CNAME file.
    – Shadowen
    Commented Aug 14, 2015 at 18:50
  • @Shadowen Perfect! "Hijack" is exactly what I was looking for. If you want to move your comment into a separate response, I'll mark it as the answer.
    – Jacob
    Commented Aug 16, 2015 at 12:32
  • 2
    I have an organisation repo. Is this still possible today?
    – tyteen4a03
    Commented Apr 19, 2016 at 1:48

3 Answers 3

6

Pulled from comments above:

[M]erge the staging git branch into the production branch and push[.] You could just keep the CNAMEs permanently pointing to their respective repos, but manipulate the repos separately.

Comment originally by Jonathan Chan

Do as @JonathanChan says: maintain two separate branches, then hijack your CNAME file in each one. This way local changes (to the CNAME) will not be committed to the repository.

3
  • 1
    Local versions of files are not relevant for Github Pages, only what is pushed to the repo. How does hijacking a file enable you to have different CNAME files in fully merged branches?
    – janpio
    Commented Sep 20, 2017 at 20:18
  • 1
    The key (or maybe "the compromise," I suppose) is that, in addition to two remote repos, you also need to maintain either separate local branches (one for staging and one for production) or separate local repositories (one for staging and one for production).
    – Jacob
    Commented Nov 27, 2017 at 21:48
  • What is extremely frustrating about this answer is that it does not answer the question, even though it does solve @Jacob's specific problem. This is because it requires the maintenance of local repositories as well. There does not seem to be any satisfactory answer to how to keep different CNAMEs without having to intervene manually between staging and production. Surely there is a way to do this? Eg, via front matter. Googling is fruitless. Commented Sep 13, 2023 at 3:24
0

I know that's an old post, but i've got tons of SO tabs open searching for a similar solution. None offered something I liked, so i ended up doing something else.

I'm using Hostinger VPS, and it has an option of Git deployment & Git web hooks. Create a GitHub Action workflow, and add a step to fire a curl command to Hostinger's web hook URL

1
  • To make this a better answer, you could perhaps explain what "Hosting VPS" is and what you are suggesting. Commented Sep 13, 2023 at 3:26
0

If you deploy using a GitHub Actions workflow, I believe you don't need CNAME files. See this: GitHub Pages documentation

If you are publishing from a custom GitHub Actions workflow, any CNAME file is ignored and is not required.

You just need to make sure that the URLs are correctly configured in the GitHub Pages settings of the repositories.

I spent an afternoon attempting to bypass the CNAME push for deployments from staging to production. Eventually, I discovered that removing the CNAME files while deploying through GitHub Actions resolves the issue effectively.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.