Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(script): Add docker script for container compilation #36

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/core/structure/root.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Structure.Root do
create: %{
"{app_snake}/mix.exs" => @base <> "mix.exs",
"{app_snake}/Dockerfile-build" => @base <> "Dockerfile-build",
"{app_snake}/sh_build.sh" => @base <> "sh_build.sh",
"{app_snake}/config/config.exs" => @base <> "config.exs",
"{app_snake}/config/dev.exs" => @base <> "dev.exs",
"{app_snake}/config/test.exs" => @base <> "test.exs",
Expand Down
4 changes: 2 additions & 2 deletions lib/mix/tasks/ca.release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* _build/release/test-junit-report.xml
* _build/release/artifact/<app-name>.tar.gz

You can skip some task with --skip-test or --skip-release options:
You can skip some task with --skiptest or --skipreleasee options:
$ mix ca.release --skiptest
$ mix ca.release --skiprelease

Expand Down Expand Up @@ -118,7 +118,7 @@
Mix.shell().info([:green, "* Artifact generated"])
end

defp run_in_container(skip_release, skip_test) do

Check warning on line 121 in lib/mix/tasks/ca.release.ex

View workflow job for this annotation

GitHub Actions / build

variable "skip_release" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 121 in lib/mix/tasks/ca.release.ex

View workflow job for this annotation

GitHub Actions / build

variable "skip_test" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 121 in lib/mix/tasks/ca.release.ex

View workflow job for this annotation

GitHub Actions / build

variable "skip_release" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 121 in lib/mix/tasks/ca.release.ex

View workflow job for this annotation

GitHub Actions / build

variable "skip_test" is unused (if the variable is not meant to be used, prefix it with an underscore)
Mix.shell().info([:green, "* Generating release artifact inside a container"])
container_tool = Application.get_env(:elixir_structure_manager, :container_tool, "docker")

Expand All @@ -139,7 +139,7 @@
transformations: [
{:cmd, "rm -rf _build/release"},
{:cmd,
"#{container_tool} build --build-arg IMAGE=#{container_base_image} --build-arg SKIP_RELEASE=#{skip_release} --build-arg SKIP_TEST=#{skip_test} -t {app_snake} -f #{container_file} ."},
"#{container_tool} build --build-arg IMAGE=#{container_base_image} -t {app_snake} -f #{container_file} ."},
{:cmd, "#{container_tool} stop {app_snake} || true"},
{:cmd, "#{container_tool} rm {app_snake} || true"},
{:cmd, "#{container_tool} run -d --name {app_snake} {app_snake}"},
Expand Down
4 changes: 1 addition & 3 deletions priv/templates/structure/Dockerfile-build
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
ARG IMAGE=elixir:1.16.2-otp-26-alpine
ARG SKIP_RELEASE=false
ARG SKIP_TEST=false

FROM $IMAGE
RUN apk add build-base git
Expand All @@ -16,7 +14,7 @@ RUN mix deps.get \
# Ensure use of .dockerignore file to avoid copying unnecessary files
COPY . .
# To generate compiled application and run static code analysis
RUN mix ca.release --skip-release=$SKIP_RELEASE --skip-test=$SKIP_TEST
RUN mix ca.release

CMD while true; do \
echo "This is an infinite loop until files are copied and container destroyed."; \
Expand Down
15 changes: 15 additions & 0 deletions priv/templates/structure/sh_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
set -e
BASE_IMAGE=$1
APP_NAME={app_snake}
mkdir -p _build
rm -rf _build/release
docker build --build-arg IMAGE=$BASE_IMAGE -t $APP_NAME -f Dockerfile-build .
docker stop $APP_NAME || true
docker rm $APP_NAME || true
docker run -d --name $APP_NAME $APP_NAME
docker cp $APP_NAME:/app/_build/release _build/release
docker stop $APP_NAME || true
docker rm $APP_NAME || true
docker rmi $APP_NAME --force
cp -r deployment _build/release/artifact
ls -lR _build/release
Loading