Earthfile 4.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
FROM ruby:2.6-slim-buster
WORKDIR /application

# `deps` installs all the system libraries required by the gems in order
# to install them all.
# Finally it creates a temporary image used as a base image later.
deps:
    RUN apt-get update \
        && apt-get install -y libpq-dev \
                              build-essential \
                              nodejs \
                              git
    SAVE IMAGE

# `gems` installs all the gems from the Gemfile.lock for a production usage.
# Finally it creates a temporary image used as a base image later.
gems:
    FROM +deps
    ENV BUNDLE_GEMFILE=/application/Gemfile
    ENV BUNDLE_JOBS=8
    ENV BUNDLE_PATH=/bundle
    COPY Gemfile* ./
    RUN gem install rubygems-update \
        && update_rubygems \
        && gem install bundler:1.16.0 \
        && bundle install --without development test
    SAVE IMAGE

# `assets` runs the Rails assets:precompile task and create an artifact from it
# used in the `prod` target.
assets:
    FROM +gems
    ENV RAILS_ENV production
34
35
36
37
38
39
40
41
42
43
44
45
    COPY app/assets /application/app/assets
    COPY app/models /application/app/models
    COPY app/sweepers /application/app/sweepers
    COPY config/environments/production.rb /application/config/environments/production.rb
    COPY config/initializers /application/config/initializers
    COPY config/application.rb /application/config/application.rb
    COPY config/boot.rb /application/config/boot.rb
    COPY config/database.yml /application/config/database.yml
    COPY config/environment.rb /application/config/environment.rb
    COPY config/redis.yml /application/config/redis.yml
    COPY vendor /application/vendor
    COPY Rakefile /application
46
47
48
49
50
51
52
53
54
55
    RUN bundle exec rake assets:precompile RAILS_ENV=production
    SAVE ARTIFACT public/assets assets

# `prod` builds the production image of the project which means precompiling
# the projet assets.
# Finally it creates the Docker image used in order to run the project.
prod:
    FROM +gems
    ENV RAILS_ENV production
    RUN useradd -ms /bin/bash brewformulas
56
57
58
59
60
61
62
63
64
65
66
67
    COPY app /application/app
    COPY bin /application/bin
    COPY config /application/config
    COPY db /application/db
    COPY lib /application/lib
    COPY public /application/public
    COPY vendor /application/vendor
    COPY config.ru /application
    COPY Gemfile* /application
    COPY package.json /application
    COPY Rakefile /application
    COPY VERSION /application
68
69
70
71
72
    COPY +assets/assets public/assets
    RUN chown -R brewformulas:users /application
    EXPOSE 3000
    ENTRYPOINT ["bundle", "exec"]
    CMD ["bin/rails server -b 0.0.0.0"]
73
    SAVE IMAGE registry.gitlab.com/zedtux/brewformulas.org:latest
74

75
# `ci` is based on the production image, in order to keep an environment as
76
77
78
79
# close as possible with production when running the tests.
# Nonetheless it install additionnal gems used to run the tests.
ci:
    FROM +prod
80

81
    ENV RAILS_ENV test
82
83
84
85
86
87

    COPY features /application/features
    COPY script /application/script
    COPY spec /application/spec
    COPY .coveralls.yml /application/.coveralls.yml

Guillaume Hain's avatar
Guillaume Hain committed
88
    RUN bundle config --delete without
89
90
91
92
    RUN bundle install --gemfile=/application/Gemfile \
                       --jobs=8 \
                       --path=/bundle \
                       --without development
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
    CMD ["rake"]
    SAVE IMAGE

integration-test:
    FROM docker:19.03.13-dind
    RUN apk --update --no-cache add docker-compose jq
    COPY docker-compose.yml ./
    WITH DOCKER
        DOCKER PULL postgres:9.6.2
        DOCKER PULL redis:3.2.8
        DOCKER PULL memcached:1.5.3-alpine
        DOCKER LOAD +ci zedtux/brewformulas.org:dev
        RUN docker-compose up -d \
            && POSTGRESQL_CONTAINER_IPADDR=$(docker inspect default_postgres_1 \
                                             | jq -r '.[0].NetworkSettings.Networks.default_default.IPAddress') \
            && for i in {1..30}; do nc -z $POSTGRESQL_CONTAINER_IPADDR 5432 && break; sleep 1; done; \
            docker run --network=host \
                       --env DATABASE_HOST=$POSTGRESQL_CONTAINER_IPADDR \
                       --env POSTGRESQL_USER=postgres \
                       --env RAILS_ENV=test \
                       zedtux/brewformulas.org:dev bash -c 'bundle exec rake db:setup \
                                                            && bundle exec rake' \
            && docker-compose down
    END
117
118
119
120
121
122
123
124
125
126
127
128
129
130

# `dev` is based on the production image, in order to keep an environmnet as
# close as possible with produciton and then installs all the other gems for
# developing the project.
# The final image is used by the local docker-compose.yml file.
dev:
    FROM +prod
    ENV RAILS_ENV development
    RUN bundle config --delete without
    RUN bundle install --gemfile=/application/Gemfile \
                       --jobs=8 \
                       --path=/bundle
    # Allow adding/removing/updating gems
    RUN chown -R brewformulas:users /bundle
131
    SAVE IMAGE registry.gitlab.com/zedtux/brewformulas.org:dev