Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying ignored dist folder to GitHub Pages

I have created a Git repo for a JavaScript library. The repo also contains a demo website that uses the bundled library, which is generated in a dist/ folder. I would now like to deploy that website to GitHub Pages.

However, I have the dist/ folder in my .gitignore and would prefer it to remain ignored.

Is there a way to automatically generate the gh-pages branch, which should include dist/, but keep it off the master branch?

like image 591
Baleb Avatar asked Feb 02 '18 18:02

Baleb


People also ask

How do I deploy a folder in GitHub?

In the command line, navigate to the root directory of your project. Initialize the local directory as a Git repository. To create a repository for your project on GitHub, use the gh repo create subcommand. When prompted, select Push an existing local repository to GitHub and enter the desired name for your repository.

Should I commit dist folder?

The dist directory should not be committed to your version control system and should be ignored through your . gitignore as it will be generated automatically every time you run nuxt generate .

Can I deploy on GitHub Pages?

On GitHub, navigate to your site's repository. Under your repository name, click Settings. In the "Code and automation" section of the sidebar, click Pages. Under "Build and deployment", under "Source", select Deploy from a branch.


Video Answer


2 Answers

Commit this script and call it after having built your dist:

#!/bin/sh

git commit -am "Save uncommited changes (WIP)"
git branch --delete --force gh-pages
git checkout --orphan gh-pages
git add -f dist
git commit -m "Rebuild GitHub pages"
git filter-branch -f --prune-empty --subdirectory-filter dist && git push -f origin gh-pages && git checkout -

I use it for that ;-)

like image 74
Philippe Avatar answered Oct 22 '22 21:10

Philippe


To summarize your context:

  • your current branch is master
  • you have a dist/ folder that is ignored and untracked
  • it contains the sources of a website that can be statically served
  • you'd like to publish this dist/ folder on GitHub Pages using a gh-pages branch.

The simplest way to do this seems to follow the workflow suggested in this gist as well as in the documentation of Yeoman.

But note that this relies on the advanced command git subtree and requires that the dist/ folder is not ignored and tracked on master as well.

You could then follow the doc regarding the configuration of GH Pages' publishing source.

Does this address your question or do you really want to have the two branches separated and no dist/* file in the master branch?

In the latter case, the problem is more complicated (because of the untracked or ignored files in the work tree that can hinder the process...) but you may want to have a look at this project, which is an alternative to the git subtree-based solution, and is based on a Bash script.

(Note: I have not tested this "git-directory-deploy" solution; it was suggested in the gist I mentioned above.)

like image 39
ErikMD Avatar answered Oct 22 '22 19:10

ErikMD