Packaging and CI
Packaging is the last step before a program leaves the machine it was written on, and continuous integration is the set of gates that runs on every change. For an Elixir project the gates are the formatter, Credo, the test suite and the build of the release; each one is a command, and a pipeline runs them in that order. This lesson runs the same commands on a generated project.
The gates
A gate is a command whose non-zero exit status stops the pipeline. The four commands of an Elixir project cover the four questions: is the code formatted, is it readable, does it behave, and does it build. The project is generated first:
mix new ci_demo
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/ci_demo.ex
* creating test
* creating test/test_helper.exs
* creating test/ci_demo_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd ci_demo
mix test
Run "mix help" for more commands.
cd ci_demo
mix format --check-formatted
The formatter gate printed nothing and exited 0. A file that the formatter would rewrite fails here, and the formatting lesson shows the diff it prints.
credo --strict lib/ci_demo.ex
2 mods/funs, found no issues.
The test gate
A test suite that fails stops the pipeline before anything is packaged. The generated project has one test and one doctest, and the seed and the time change between runs:
mix test
Compiling 1 file (.ex)
Generated ci_demo app
Running ExUnit with seed: 638128, max_cases: 8
..
Finished in 0.05 seconds (0.00s async, 0.05s sync)
1 doctest, 1 test, 0 failures
The release gate
The last gate builds the artifact that ships. MIX_ENV=prod mix release compiles in the production environment and writes the folder a release is copied from; the full output is in the releases lesson:
MIX_ENV=prod mix release
Compiling 1 file (.ex)
Generated ci_demo app
* assembling ci_demo-0.1.0 on MIX_ENV=prod
* skipping runtime configuration (config/runtime.exs not found)
Release created at _build/prod/rel/ci_demo
The pipeline
The four commands are the body of a CI job. A configuration file writes them in order and stops at the first non-zero status, which is the default of a shell with set -e or of a CI step:
mix format --check-formatted && credo --strict && mix test && MIX_ENV=prod mix release
The line is shown as a command but not run: the four commands were run one by one above, and the joined line is the shape a pipeline file uses. A real job installs the toolchain first, and the dependencies with mix deps.get when the project has any; the dependencies lesson marks that download as not run in this environment. The next page names the projects a reader can build on top of this course.
The commands on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), and Credo 1.7.19, on Linux x86_64 in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which command checks the formatting without rewriting a file?
Answer
mix format --check-formatted
The check mode is read-only and exits non-zero when a file would change.
-
What does a gate need to stop a pipeline?
Answer
A non-zero exit status
The pipeline reads the exit status, not the text of the report.
-
Which command builds the artifact a release is copied from?
Answer
MIX_ENV=prod mix release
The release task builds the production artifact.
-
What does
credo --strictadd to a pipeline?Answer
A check of the shape and readability of the code
Credo is the static analysis gate; it does not format or test.
-
What does
mix format --check-formattedprint when every file is formatted?Answer
Nothing, and it exits 0
A clean check is silent and returns success.
-
A test run ends with
1 doctest, 1 test, 0 failures. What does that mean?Answer
One documentation example and one test passed
The counts name the doctests, the ordinary tests and the failures.
-
Which command runs the generated tests of a new project?
Answer
mix test
mix test starts ExUnit and runs the test folder.
-
Why does the pipeline run the gates in order?
Answer
A cheap failure stops the job before the slow build
Format and Credo are fast; the test and the release are slower and run only if the cheap gates pass.
-
What does
MIX_ENV=prodchange aboutmix release?Answer
It builds the production environment into _build/prod
The environment selects the build folder and the production configuration.
-
Which file of the project decides what
mix formattouches?Answer
.formatter.exs
The inputs list of the formatter file is the set of files.
Автор статьи: Arthur Isaev