Jekyll:在gitlab-ci管道中使用PDFKit生成文件 [英] Jekyll : Generating file with PDFKit in a gitlab-ci pipeline

查看:129
本文介绍了Jekyll:在gitlab-ci管道中使用PDFKit生成文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jekyll生成一个静态网站,包括生成每篇文章pdf文件.

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

这是我的jekyll模块:

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.

这是我的.gitlab-ci.yml:

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

以及注册表的Dockerfile.gitlab.com/thalion59fr/jekyll-ci:master映像: (基于这一个,我稍后将制作一个具有继承性的正确Dockerfile)

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 <david@aerobatic.com>
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 aerobatic-cli@1.0.33

最后,错误消息:

$ 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

感谢您的帮助! 大卫

推荐答案

我终于通过修改建筑物图像解决了这个问题:

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

FROM aerobatic/jekyll:0.0.2
MAINTAINER David Vergison <david.vergison@gmail.com>
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/* \

然后在gimli gem之后通过软件包管理器安装wkhtmltopdf:

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

如您所见,我放弃使用jekyll来生成pdf.我在"jekyll构建"之前使用gimli生成pdf文档. 然后在帖子布局中使用一些液体标记:

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在这里允许wkhtmltopdf无头执行,并且脚本comme从这里开始: https://gist.github.com/tyleramos/3744901

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

这不是真的很整洁,但现在可以了. 我现在必须处理CSS,但是使用gimli会很容易.

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.

这篇关于Jekyll:在gitlab-ci管道中使用PDFKit生成文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆