Skip to content

Commit 3c855a3

Browse files
author
Steven Chen
committed
feat: support to build more gems
1 parent 72fd501 commit 3c855a3

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

Dockerfile

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
FROM centos:centos6
22

33

4-
ADD ./Gemfile /opt/build/
4+
ADD ./Gemfile /opt/build/
5+
ADD ./build.sh /opt/build/
56

67
RUN \
78
yum update -y && \
@@ -17,8 +18,9 @@ RUN \
1718
RUN \
1819
gem update --system && \
1920
cd /opt/build && \
20-
bundle install --path vendor/bundle && \
21-
cd vendor/bundle/ruby && \
22-
tar zcvf nokogiri-1.8.2.tar.gz 2.2.0/extensions/x86_64-linux/2.2.0-static/nokogiri-1.8.2 && \
23-
mv nokogiri-1.8.2.tar.gz /opt/build && \
24-
cd -
21+
/bin/bash build.sh
22+
# bundle install --path vendor/bundle && \
23+
# cd vendor/bundle/ruby && \
24+
# tar zcvf nokogiri-1.8.2.tar.gz 2.2.0/extensions/x86_64-linux/2.2.0-static/nokogiri-1.8.2 && \
25+
# mv nokogiri-1.8.2.tar.gz /opt/build && \
26+
# cd -

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
ruby '2.2.2'
22
source 'https://rubygems.org'
33

4+
# Add you gems here
45
gem 'nokogiri', '1.8.2'
6+
gem 'json', '1.8.2'
7+

README.md

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
# Ruby Gem native extensions
1+
# Building Ruby gem native extensions
22

3-
Some Ruby Gems depend on C/C++ native extensions which makes it platform dependent. `nokogiri` will
4-
be one of these. [Traveling Ruby](https://github.com/phusion/traveling-ruby) provides a solution to
5-
include native extensions for several specific platforms, however, the extensions provided by
6-
Traveling Ruby is quite outdated and it is quite non-trivial to build its packages. I encountered
7-
failures with `yum update` in the `chroot` environment every now and then. Thus, I would rather
8-
build it on my own.
3+
Some Ruby gems depend on C/C++ native extensions which are platform dependent. `nokogiri` is one of
4+
these. Fortunately, [Traveling Ruby](https://github.com/phusion/traveling-ruby) provides a solution
5+
to include native extensions for several specific platforms. However, those extensions provided by
6+
Traveling Ruby are quite obsolete and it is quite non-trivial to build those packages using
7+
Traveling Ruby. I encountered failures with `yum update` in the `chroot` environment every now and
8+
then. Thus, I would rather build it on my own.
99

10-
As a workaround, we can build specific library in `centos:centos6` container and copy over the
11-
extensions. Note that the final artifact of the extension should contain the following (use
12-
`nokogiri` as an example), which can be found at
13-
`ruby/2.2.0/extensions/x86_64-linux/2.2.0-static/xxxx` in your bundle `--path`:
10+
As a workaround, we can build specific library in `centos:centos6` container and copy those
11+
extensions after the build. Note that the final artifact of an extension should contain the
12+
following (use `nokogiri` as an example), which can be found at your bundle `--path`:
1413

1514
```
1615
2.2.0
@@ -33,7 +32,13 @@ tar zcvf nokogiri-1.8.2.tar.gz 2.2.0
3332
```
3433

3534
Alternatively, the Dockerfile provided in the directory will generate `nokogiri-1.8.2.tar.gz` and
36-
store it into `/opt/build`. And we can use this tarball as an upgrade for what `Traveling Ruby`
37-
provides.
35+
store it into `/opt/build/target`. And we can use this tarball as an upgrade for what `Traveling Ruby`
36+
provides. To build native extensions for more gems, add those gem into `Gemfile`, and run the
37+
following:
38+
39+
```
40+
$ docker build -t rubygem-native-builder .
41+
$ docker cp rubygem-native-builder:/opt/build/target /path/to/host/directory
42+
```
3843

3944
Have fun!

build.sh

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
set -ex
4+
5+
EXT_DIR=2.2.0/extensions/x86_64-linux/2.2.0-static
6+
TARGET_DIR=/opt/build/target
7+
BUNDLE_INSTALL_DIR=/opt/build/vendor/bundle
8+
9+
mkdir -p ${TARGET_DIR}
10+
bundle install --path ${BUNDLE_INSTALL_DIR}
11+
cd ${BUNDLE_INSTALL_DIR}/ruby
12+
13+
GEMS=$(ls "${EXT_DIR}")
14+
15+
for gem in $GEMS; do
16+
tar zcvf ${gem}.tar.gz ${EXT_DIR}/${gem}
17+
mv ${gem}.tar.gz ${TARGET_DIR}
18+
done

0 commit comments

Comments
 (0)