Search code examples
gitlabjekyllgitlab-pages

HOSTED VS LOCAL: gitlab-ci.yml copies 'static' directory to 'public'. How to tell local command "bundle exec jekyll serve" to do that as well


I've got no website building experience. I've followed the instructions on gitlab to fork an example jekyll site (including removing the fork relationship and renaming the url to groupname). On gitlab, the .gitlab-ci.yml file contains:

pages:
stage: deploy
script:
- gem install jekyll
- jekyll build -d public/
- cp -r static/* public/
artifacts:
  paths:
  - public
only:
- pages

In the 'root' folder on gitlab, there's the index.html, directories (css, static), etc. You can see in the gitlab-ci.yml file, gitlab copies the contents of the static directory into the root folder (public). Although there's only the files in static when looking at my project page (and not the copies).

On linux, I've installed and run jekyll, but it displays improperly. The "not found" errors on the command line all reference paths in the static directory (which has subdirectories for css, font-awesome, fonts, img, and js).

I'm reluctant to mess with the file/directory locations locally because it may cause problems when I want to deploy the changes to gitlab. How do I emulate locally (with the "bundle exec jekyll serve" command) what gitlab is doing (copying static to the root directory)? Or is there another way to handle/fix this?

Edit: I'm also seeing a 'Configuration file: none' message when running the serve command. Although there is a _config.yml file, I think it may need some configuration. And on a different gitlab/jekyll theme there are some instructions for configuration. I'll have to look into this further.

Edit after solved:

Based on Ross's suggestion, I updated the line in the .gitlab-ci.yml file from 'jekyll build -d public/' to 'bundle exec jekyll build -d public'. For this file, I see that others have dropped the line 'gem install jekyll', but I still have it in and it works for now. Although I wonder if I should change it to 'bundle install' so that gitlab/local jekyll versions are the same. The bundle command reads the Gem file, which mine specifies the jekyll version. I'm not sure if that will tell gitlab to use a specific version of jekyll or if they just use their version regardless.

Then locally on linux I cd into the site directory with the .yml files that I downloaded from gitlab. Then ran

mkdir public
bundle install
bundle exec jekyll build -d public/
cp -r static/* public/
cd public/
jekyll serve

The served page is no longer broken. It now looks exactly as the actual gitlab.io page.


Solution

  • You'd have to run the same commands as GitLab to get the same result. Sounds like you are running this:

    bundle install
    bundle exec jekyll serve
    

    You've set GitLab to run this:

    gem install jekyll
    jekyll build -d public/
    cp -r static/* public/
    

    You could set the same destination with -d and run that cp command locally to get a similar result, but you'd have to do it each time jekyll builds.

    I'd suggest you continue to use the bundle commands, and set up GitLab to do the same. Currently you will run into versioning issues, since GitLab will install the latest Jekyll each build, and any plugins in the Gemfile are ignored.

    Jekyll copies static resources by default, so you could just rely on that rather than coping static/* to public, you'd need to update the paths. It's probably copying these already unless you've configured it to ignore the static folder with the exclude option. Hard to know without seeing the source files.

    Run jekyll from the same folder as the _config.yml if you are not overriding the source or config command line parameters to avoid the 'Configuration file: none' message.