Skip to content

Commit bd93fa8

Browse files
authored
Merge pull request #193 from giusdp/main
Refactor release build and merge dockerfiles
2 parents 421157e + aa9adaf commit bd93fa8

File tree

11 files changed

+79
-97
lines changed

11 files changed

+79
-97
lines changed

.github/workflows/image-release.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,18 @@ jobs:
5151
uses: docker/[email protected]
5252
with:
5353
context: .
54-
file: Dockerfile.core
54+
file: Dockerfile
5555
push: true
5656
tags: ghcr.io/funlessdev/core:latest,ghcr.io/funlessdev/core:${{ env.VERSION }}
5757
platforms: linux/amd64
58+
build-args: COMPONENT=core,MIX_ENV=prod
5859

5960
- name: Build and push Worker
6061
uses: docker/[email protected]
6162
with:
6263
context: .
63-
file: Dockerfile.worker
64+
file: Dockerfile
6465
push: true
6566
tags: ghcr.io/funlessdev/worker:latest,ghcr.io/funlessdev/worker:${{ env.VERSION }}
6667
platforms: linux/amd64
68+
build-args: COMPONENT=worker,MIX_ENV=prod

Dockerfile.core renamed to Dockerfile

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
# This should match the version of Alpine that the `elixir:1.14-alpine` image uses
1717
ARG ALPINE_VERSION=3.17
1818
ARG SECRET_KEY_BASE
19+
ARG COMPONENT=core
20+
ARG MIX_ENV=prod
1921

2022
FROM elixir:1.14-alpine AS builder
2123

22-
ARG MIX_ENV=prod
24+
ARG MIX_ENV
25+
ARG COMPONENT
2326

2427
ENV MIX_ENV=${MIX_ENV} \
2528
SECRET_KEY_BASE=${SECRET_KEY_BASE}
@@ -37,14 +40,16 @@ RUN apk update && \
3740
# This copies our app source code into the build container
3841
COPY . .
3942

40-
RUN mix deps.get --only $MIX_ENV
43+
RUN mix deps.get --only "${MIX_ENV}"
4144
RUN mix compile
42-
RUN mix release core
45+
RUN echo "Building FunLess ${COMPONENT} in env ${MIX_ENV}"
46+
RUN MIX_ENV=${MIX_ENV} mix release ${COMPONENT}
4347

4448
# From this line onwards, we're in a new image, which will be the image used in production
4549
FROM alpine:${ALPINE_VERSION}
4650

47-
ARG MIX_ENV=prod
51+
ARG MIX_ENV
52+
ARG COMPONENT
4853
ARG SECRET_KEY_BASE
4954
ARG PORT=4000
5055
ARG NODE_IP=""
@@ -58,6 +63,7 @@ RUN apk update && \
5863
ENV REPLACE_OS_VARS=true \
5964
USER=funless \
6065
MIX_ENV=${MIX_ENV} \
66+
COMPONENT=${COMPONENT}\
6167
SECRET_KEY_BASE=${SECRET_KEY_BASE} \
6268
PORT=${PORT} \
6369
NODE_IP=${NODE_IP} \
@@ -70,19 +76,15 @@ ENV REPLACE_OS_VARS=true \
7076

7177

7278
WORKDIR "/home/${USER}/app"
79+
7380
# Creates an unprivileged user to be used exclusively to run the Phoenix app
7481
RUN addgroup -g 1000 -S "${USER}" && adduser -s /bin/sh -u 1000 -G "${USER}" \
7582
-h "/home/${USER}" -D "${USER}" && su "${USER}"
7683

7784
# Everything from this line onwards will run in the context of the unprivileged user.
7885
USER "${USER}"
7986

80-
COPY --chown="${USER}":"${USER}" --from=builder /opt/app/_build/${MIX_ENV}/rel/core/ .
87+
COPY --chown="${USER}":"${USER}" --from=builder /opt/app/_build/${MIX_ENV}/rel/${COMPONENT}/ .
8188

82-
# # Usage:
83-
# # * build: sudo docker image build -t elixir/my_app .
84-
# # * shell: sudo docker container run --rm -it --entrypoint "" -p 127.0.0.1:4000:4000 elixir/my_app sh
85-
# # * run: sudo docker container run --rm -it -p 127.0.0.1:4000:4000 --name my_app elixir/my_app
86-
# # * exec: sudo docker container exec -it my_app sh
87-
# # * logs: sudo docker container logs --follow --tail 100 my_app
88-
CMD ["sh", "-c", "bin/migrate && bin/seed && bin/server"]
89+
RUN echo "Command: FunLess ${COMPONENT} in env ${MIX_ENV}"
90+
CMD ["sh", "-c", "bin/run"]

Dockerfile.worker

Lines changed: 0 additions & 72 deletions
This file was deleted.

Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717
SECRET_KEY_BASE ?= $(shell mix phx.gen.secret)
1818
## Compile core docker image
1919
build-core-image:
20-
docker build --build-arg SECRET_KEY_BASE=$(SECRET_KEY_BASE) --build-arg MIX_ENV=prod -t core -f ./Dockerfile.core .
20+
docker build \
21+
--build-arg SECRET_KEY_BASE=$(SECRET_KEY_BASE) \
22+
--build-arg MIX_ENV="prod" \
23+
--build-arg COMPONENT="core" \
24+
-t core .
2125

2226
## Compile worker docker image
2327
build-worker-image:
24-
docker build -t worker -f ./Dockerfile.worker .
28+
docker build --build-arg COMPONENT="worker" \
29+
--build-arg MIX_ENV="prod" -t worker .
2530

2631
## Run credo --strict
2732
credo:
@@ -35,7 +40,7 @@ dial:
3540
test:
3641
mix deps.get
3742
docker compose -f docker-compose.yml up --detach
38-
mix core.test
39-
mix worker.test
40-
mix core.integration_test
43+
mix core.utest
44+
mix worker.utest
45+
mix core.itest
4146
docker compose -f docker-compose.yml down

apps/core/mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule Core.MixProject do
1818
def project do
1919
[
2020
app: :core,
21-
version: "0.6.0",
21+
version: "0.8.0",
2222
build_path: "../../_build",
2323
config_path: "../../config/config.exs",
2424
deps_path: "../../deps",

apps/worker/mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ defmodule Worker.MixProject do
1818
def project do
1919
[
2020
app: :worker,
21-
version: "0.6.0",
21+
version: "0.8.0",
2222
build_path: "../../_build",
2323
config_path: "../../config/config.exs",
2424
deps_path: "../../deps",

rel/overlays/bin/migrate

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 Giuseppe De Palma, Matteo Trentin
1+
# Copyright 2023 Giuseppe De Palma, Matteo Trentin
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

rel/overlays/bin/run

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2023 Giuseppe De Palma, Matteo Trentin
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
#!/bin/sh
16+
cd -P -- "$(dirname -- "$0")"
17+
echo "Starting $COMPONENT in env $MIX_ENV"
18+
19+
if [ "$COMPONENT" = "core" ]; then
20+
sh -c ./migrate && ./seed && ./run_core start
21+
else
22+
sh -c ./run_worker start
23+
fi

rel/overlays/bin/server renamed to rel/overlays/bin/run_core

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2022 Giuseppe De Palma, Matteo Trentin
1+
# Copyright 2023 Giuseppe De Palma, Matteo Trentin
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

rel/overlays/bin/run_worker

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2023 Giuseppe De Palma, Matteo Trentin
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
#!/bin/sh
16+
cd -P -- "$(dirname -- "$0")"
17+
18+
if [[ -z "$NODE_IP" ]]; then
19+
DEPLOY_ENV=${DEPLOY_ENV} exec ./worker start
20+
else
21+
RELEASE_NODE=worker@${NODE_IP} DEPLOY_ENV=${DEPLOY_ENV} exec ./worker start
22+
fi

0 commit comments

Comments
 (0)