19

I go into the page in Github (https://github.com/user/repo/settings/pages) and set a custom domain. It works great!

However, every time I deploy (using the gh-pages npm package) it resets the domain back to

 https://user.github.io/repo

This is incredibly frustrating... how can I tell it to use my custom domain permanently?

7 Answers 7

17

It looks like there are reports about this in the repo for gh-pages npm package. See #347, #236, #370, #213.

There is even a still opened merged pull-request that tackles the issue through documentation.

Basically, it says:

Modify the deployment line to your deploy script if you use custom domain. This will prevent deployment to remove the domain from settings in github.

echo 'your_cutom_domain.online' > ./build/CNAME && gh-pages -d build"

Edit: there are other options as well, some people directly change their deployment call and add a custom domain to their deployment scritpt:

var ghpages = require('gh-pages');
var fs = require('fs');

fs.writeFile('dist/CNAME', "your-custom-domain.com", function(err) {});
ghpages.publish('dist', function(err) {});

others just follow the advice for putting CNAME to your publishing folder.

Sign up to request clarification or add additional context in comments.

2 Comments

I got confused with the quotation mark around the domain name. Actually, it was not required.
"deploy": "echo domain.com > ./build/CNAME && gh-pages -d build"
3

For my React static site I have to add below scripts to the package.json to add the CNAME file with the custom domain to the build/ dir before each deployment.

"scripts": {
   "build": "react-scripts build",
   "add-domain": "echo \"myAwesomeDomain.org\" > build/CNAME",
   "deploy": "npm run add-domain && gh-pages -d build",
   "bd": "npm run build && npm run deploy",
},

1 Comment

I had to remove the quotes around my domain name for this to work correctly for me. Thanks for the scripts though. They were quite helpful
1

For those of you using the GitHub Action crazy-max/ghaction-github-pages there is a setting called fqdn that you're looking for. Set that with the custom domain and it stop breaking every deploy.

See documentation here.

1 Comment

Thanks, I just checked and the action I use - peaceiris/actions-gh-pages@v3 - has an analogous cname setting.
1

I was having the same problem for a gatsby site, I needed to install the gatsby-plugin-cname package and add the following to the gatsby-config.js

module.exports = {
  siteMetadata: {
    siteUrl: 'http://custom-domain.com/'
  },
  plugins: [
    'gatsby-plugin-cname'
  ],
}

https://www.gatsbyjs.com/plugins/gatsby-plugin-cname/

Comments

0

If you are using "npm run deploy" or any npm deployment scripts Add a CNAME.txt file in public your Directory In the 1st line enter your custom domain name in it. e.g. (acbd.com)

else Create and Add the same in you static Directory

Deploy and check

1 Comment

This is a simple solution, but it does work
0

I had the same problem, but in my case I'm using Material for MKDocs to deploy the website.

To solve this, I noticed that the CNAME file was not created (or copied) on the branch that the Github Pages was configured, just moving the CNAME file to the docs folder (in case of MKDocs), but there is the possibility to change the pipeline to create it on the correct branch.

Comments

0

With some frameworks (React, in my case), the contents of the public or static directory get copied to the root of the build directory. For me it was as simple as placing a CNAME file with my custom domain in that directory and it's there in the build.

echo "mydomain.com" > ./public/CNAME
npm run build
echo ./build/CNAME # outputs "mydomain.com"
gh-pages -d build

Commit, deploy

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.