Deployment an elixir project with using TravisCI

deployment
#1

I have published an elixir project with using Travis CI.
I would like to share some tips & thoughts that I was getting through this experience. I will be happy if it helps you.

.travis.yml

Here is an .travis.yml in an elixir project. You have to set HEX_ENCRYPTED_KEY and HEX_PASSPHRASE to environment variables in TravisCI.

  • Every time I push a commit
    • Execute mix credo as linter
    • Execute mix dialyzer as statical type checker
    • Execute mix test
  • Every time I push a tag
    • First, same as pushing a commit. When it pass the checkings, it will deploy project to hex.pm and its document to hexdocs.pm
    • No write key and passphrase to logs in TravisCI
  • To build faster
language: elixir
sudo: false
otp_release:
  - 19.3
elixir:
  - 1.4.2
env:
  global:
    - HEX_USERNAME=niku
    # Follow other language's environment
    # e.g.) `RACK_ENV=test` has been setted as Default Environment Variables
    # https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables
    - MIX_ENV=test
cache:
  directories:
    - _build
    - deps
script:
  - mix credo --strict
  # https://github.com/jeremyjh/dialyxir#command-line-options
  # > exit immediately with same exit status as dialyzer. useful for CI
  - mix dialyzer --halt-exit-status
  - mix test
deploy:
   # https://docs.travis-ci.com/user/deployment/script/
   # > `script` must be a scalar pointing to an executable file or command.
   provider: script
   # http://yaml.org/spec/1.2/spec.html#id2779048
   # `>-` indicates the line folding.
   script: >-
     mix deps.get &&
     mix hex.config username "$HEX_USERNAME" &&
     (mix hex.config encrypted_key "$HEX_ENCRYPTED_KEY" > /dev/null 2>&1) &&
     (echo "$HEX_PASSPHRASE"\\nY | mix hex.publish) &&
     mix clean &&
     mix deps.clean --all
   on:
    tags: true

tips & thoughts

A package with a specific version in hex.pm could not be updated except for one hour since its creation. I didn’t realize it.

A ruby project on TravisCI, RAILS_ENV=test and RACK_ENV=test are available to all builds. To get a similar behavior at an elixir project on TravisCI, you should set MIX_ENV=test to the global environment variable.

You can write only a scalar variable a deploy - script section. You have to use multiline syntax for yaml if you want to write scripts which have multiple inline lines. When you use >, it doesn’t work well because it includes the last line feed ( \n ). Instead, you have to use >- to write it which doesn’t include the last line feed ( \n ).
Related YAML spec:
8.1.1.2. Block Chomping Indicator,
8.1.3. Folded Style

For security reasons, you mustn’t write any encrypted_key to the CI log which anyone can read. Hoyouver mix hex.config key value writes its value to STDOUT. So, I have redirected the result of the command to /dev/null.

mix hex.publish requires both an input passphrase and to confirm Code of Conduct. It means you need key input twice. First, It needs the input passphrase. Second, It needs to type Y. So I have to have handled them with STDIN like this: echo "$HEX_PASSPHRASE"\\nY | mix hex.publish.

You need execution mix clean && mix deps.clean --all because of TravisCI executes git stash after a script evaluates. You will get an error like: Could not restore untracked files from stash if you don’t clean up.

3 Likes

#2

Nice work! I’m tempted to try this out on my projects, though I’m hesitant to have things auto published - that said, I’ve probably made more mistakes during publishing by hand than I would if I got this set up right. Have you considered modifying this to maybe only do this on a tag? Have you looked into GitHub releases at all? It’d be cool to automatically create a release from a tag and set the description to be some subset of a CHANGELOG file showing what was modified.

Thanks for sharing!

1 Like

#3

I tend to have a ‘release’ branch that I merge in to to do an automated release.

0 Likes

#4

Any new revelations on this? I tried the above and it works, but was wondering if a better way has come along

0 Likes