Search code examples
rubypdf-generationwkhtmltopdfgitlab-cijekyll-extensions

Jekyll : Generating file with PDFKit in a gitlab-ci pipeline


I'm trying to generate a static website with jekyll, including the generation of a per article pdf file.

Here is my jekyll module :

require 'pdfkit'

module Jekyll

  Jekyll::Hooks.register :site, :post_write do |post|

      post.posts.docs.each do |post|
          filename = post.site.dest + post.id + ".pdf"
          dirname = File.dirname(filename)
          Dir.mkdir(dirname) unless File.exists?(dirname)

          kit = PDFKit.new(post.content, :page_size => 'Letter')
          kit.stylesheets << './css/bootstrap.min.css'
          kit.to_file(filename)
      end
  end

end

Everything is working properly on my workstation, so i suppose there is a problem with my pipeline.

Here is my .gitlab-ci.yml :

stages:
  - build
  - deploy

build:
  image: registry.gitlab.com/thalion59fr/jekyll-ci:master
  stage: build
  script:
  - bundle
  - gem list
  - bundle exec jekyll build --trace
  only:
  - master
  artifacts:
    paths:
    - _site/

deploy:
  image: registry.gitlab.com/thalion59fr/jekyll-ci:master
  stage: deploy
  script:
  - aero deploy --directory _site
  dependencies:
  - build

And the Dockerfile of the registry.gitlab.com/thalion59fr/jekyll-ci:master image : (based on this one, I will make a proper Dockerfile with inheritance later)

FROM alpine
MAINTAINER David Von Lehman <[email protected]>
ENV LANGUAGE=en_US
ENV LANG=en_US.UTF-8
ENV JEKYLL_ENV=development
ENV LC_ALL=en_US
RUN apk --update add zlib-dev build-base libxml2-dev \
  libxslt-dev readline-dev \
  libffi-dev ruby-dev yaml-dev zlib libxml2 \
  build-base ruby-io-console readline libxslt ruby yaml libffi nodejs ruby-irb \
  ruby-json ruby-rake ruby-rdoc git bash curl ttf-freefont fontconfig
RUN gem clean && gem install --no-ri --no-rdoc \
  bundler json:1.8.6 nokogiri:1.7.0.1 jekyll:3.4.0 wkhtmltopdf-installer jekyll-pdf
RUN rm -rf /usr/lib/ruby/gems/*/cache/*.gem
# Install the aerobatic-cli globally
RUN npm install -g [email protected]

And finaly, the error message :

$ bundle exec jekyll build --trace
Configuration file: /builds/Thalion59fr/jekyll-wulin/_config.yml
            Source: /builds/Thalion59fr/jekyll-wulin
       Destination: /builds/Thalion59fr/jekyll-wulin/_site
 Incremental build: disabled. Enable with --incremental
      Generating... 
bundler: failed to load command: jekyll (/usr/bin/jekyll)
Errno::EPIPE: Broken pipe
  /usr/lib/ruby/gems/2.3.0/gems/pdfkit-0.8.2/lib/pdfkit/pdfkit.rb:64:in `write'
  /usr/lib/ruby/gems/2.3.0/gems/pdfkit-0.8.2/lib/pdfkit/pdfkit.rb:64:in `puts'
  /usr/lib/ruby/gems/2.3.0/gems/pdfkit-0.8.2/lib/pdfkit/pdfkit.rb:64:in `block in to_pdf'
  /usr/lib/ruby/gems/2.3.0/gems/pdfkit-0.8.2/lib/pdfkit/pdfkit.rb:63:in `popen'
  /usr/lib/ruby/gems/2.3.0/gems/pdfkit-0.8.2/lib/pdfkit/pdfkit.rb:63:in `to_pdf'
  /usr/lib/ruby/gems/2.3.0/gems/pdfkit-0.8.2/lib/pdfkit/pdfkit.rb:76:in `to_file'
  /builds/Thalion59fr/jekyll-wulin/_plugins/topdf.rb:31:in `block (2 levels) in <module:Jekyll>'
  /builds/Thalion59fr/jekyll-wulin/_plugins/topdf.rb:19:in `each'
  /builds/Thalion59fr/jekyll-wulin/_plugins/topdf.rb:19:in `block in <module:Jekyll>'
  /usr/lib/ruby/gems/2.3.0/gems/jekyll-3.3.1/lib/jekyll/hooks.rb:98:in `block in trigger'
  /usr/lib/ruby/gems/2.3.0/gems/jekyll-3.3.1/lib/jekyll/hooks.rb:97:in `each'
  /usr/lib/ruby/gems/2.3.0/gems/jekyll-3.3.1/lib/jekyll/hooks.rb:97:in `trigger'
  /usr/lib/ruby/gems/2.3.0/gems/jekyll-3.3.1/lib/jekyll/site.rb:211:in `write'
  /usr/lib/ruby/gems/2.3.0/gems/jekyll-3.3.1/lib/jekyll/site.rb:71:in `process'
  /usr/lib/ruby/gems/2.3.0/gems/jekyll-3.3.1/lib/jekyll/command.rb:26:in `process_site'
  /usr/lib/ruby/gems/2.3.0/gems/jekyll-3.3.1/lib/jekyll/commands/build.rb:63:in `build'
  /usr/lib/ruby/gems/2.3.0/gems/jekyll-3.3.1/lib/jekyll/commands/build.rb:34:in `process'
  /usr/lib/ruby/gems/2.3.0/gems/jekyll-3.3.1/lib/jekyll/commands/build.rb:16:in `block (2 levels) in init_with_program'
  /usr/lib/ruby/gems/2.3.0/gems/mercenary-0.3.6/lib/mercenary/command.rb:220:in `block in execute'
  /usr/lib/ruby/gems/2.3.0/gems/mercenary-0.3.6/lib/mercenary/command.rb:220:in `each'
  /usr/lib/ruby/gems/2.3.0/gems/mercenary-0.3.6/lib/mercenary/command.rb:220:in `execute'
  /usr/lib/ruby/gems/2.3.0/gems/mercenary-0.3.6/lib/mercenary/program.rb:42:in `go'
  /usr/lib/ruby/gems/2.3.0/gems/mercenary-0.3.6/lib/mercenary.rb:19:in `program'
  /usr/lib/ruby/gems/2.3.0/gems/jekyll-3.3.1/exe/jekyll:13:in `<top (required)>'
  /usr/bin/jekyll:22:in `load'
  /usr/bin/jekyll:22:in `<top (required)>'
ERROR: Job failed: exit code 1

Thanks for your help ! David


Solution

  • I finally solved the problem by modifying the building-image:

    FROM aerobatic/jekyll:0.0.2
    MAINTAINER David Vergison <[email protected]>
    ADD xvfb-run /usr/bin/ 
    RUN chmod +x /usr/bin/xvfb-run \
      && gem update --system \
      && gem install --no-ri --no-rdoc gimli \
      && apk --update add curl ttf-freefont fontconfig xvfb qt5-qtbase-dev dbus \
      && rm -rf /var/cache/apk/* \
    

    And by installing wkhtmltopdf via the packages manager AFTER the gimli gem :

    stages:
      - build
      - deploy
    build:
      image: registry.gitlab.com/thalion59fr/jekyll-ci:master
      stage: build
      script:
      - bundle install
      - apk add wkhtmltopdf --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted
      - mkdir statics
      - cd _posts/
      - /usr/bin/xvfb-run -- gimli -o ../statics/
      - cd ..
      - bundle exec jekyll build --trace
      only:
      - master
      artifacts:
        when: on_success
        expire_in: 3 days
        paths:
        - _site/
    deploy:
      image: aerobatic/jekyll
      stage: deploy
      script:
      - aero deploy --directory _site
      dependencies:
      - build
    

    As you can see, I gave up using jekyll to generate the pdf. I use gimli to generate pdf documents before the "jekyll build". Then I use some liquid markup in the posts layout :

    <a href="/statics/{{page.path | split: '/' | last | split: '.' | first}}.pdf">download as pdf</a>
    

    xvfb is here to allow wkhtmltopdf to be execute headless, and the script comme from here : https://gist.github.com/tyleramos/3744901

    It's not realy neat, but it will do for now. I now have to work on the css, but it's going to be easy with gimli.