Jekyll:在 gitlab-ci 管道中使用 PDFKit 生成文件
Jekyll : Generating file with PDFKit in a gitlab-ci pipeline
我正在尝试使用 jekyll 生成一个静态网站,包括为每篇文章生成一个 pdf 文件。
这是我的 jekyll 模块:
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
我的工作站上一切正常,所以我想我的管道有问题。
这是我的 .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
以及 registry.gitlab.com/thalion59fr/jekyll-ci:master 镜像的 Dockerfile:
(在this one的基础上,后面会做一个合适的带继承的Dockerfile)
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
感谢您的帮助!
大卫
我终于通过修改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 之后通过包管理器安装 wkhtmltopdf 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 build"之前使用gimli生成pdf文档。
然后我在帖子布局中使用一些液体标记:
<a href="/statics/{{page.path | split: '/' | last | split: '.' | first}}.pdf">download as pdf</a>
xvfb 在这里允许 wkhtmltopdf 无头执行,脚本来自这里:
https://gist.github.com/tyleramos/3744901
它不是很整洁,但现在就可以了。
我现在必须在 css 上工作,但使用 gimli 会很容易。
我正在尝试使用 jekyll 生成一个静态网站,包括为每篇文章生成一个 pdf 文件。
这是我的 jekyll 模块:
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
我的工作站上一切正常,所以我想我的管道有问题。
这是我的 .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
以及 registry.gitlab.com/thalion59fr/jekyll-ci:master 镜像的 Dockerfile: (在this one的基础上,后面会做一个合适的带继承的Dockerfile)
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
感谢您的帮助! 大卫
我终于通过修改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 之后通过包管理器安装 wkhtmltopdf 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 build"之前使用gimli生成pdf文档。 然后我在帖子布局中使用一些液体标记:
<a href="/statics/{{page.path | split: '/' | last | split: '.' | first}}.pdf">download as pdf</a>
xvfb 在这里允许 wkhtmltopdf 无头执行,脚本来自这里: https://gist.github.com/tyleramos/3744901
它不是很整洁,但现在就可以了。 我现在必须在 css 上工作,但使用 gimli 会很容易。