oliveiragahenrique

oliveiragahenrique

Mix release - CI / CD Pipeline suggestions

I’m studying elixir for a while and never had a chance to use it in production on my day to day job.

Recently I’ve decided to put one of my phoenix side projects in “production”, by hosting a server a and config all the CI/CD on Github Actions to see how it works with elixir.

I’ve ended up with a pipeline such as this:

name: Server Deploy SDX
on:
  push:
    branches: ['sdx']
  pull_request:
    branches: ['sdx']

jobs:
  CI-CD:
    runs-on: ubuntu-latest
    env:
      MIX_ENV: test
      DATABASE_URL_SDX: ${{ secrets.DATABASE_URL_SDX }}
      SECRET_KEY_BASE_SDX: ${{ secrets.SECRET_KEY_BASE_SDX }}
      PORT_SDX: 7001

    services:
      db:
        image: postgres:12.8
        ports: ['5432:5432']
        env:
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - uses: actions/checkout@v2
      - name: Set up Elixir
        uses: erlef/setup-elixir@885971a72ed1f9240973bd92ab57af8c1aa68f24
        with:
          elixir-version: '1.12.2'
          otp-version: '24.0'

      - name: Restore Dependencies Cache
        uses: actions/cache@v2
        with:
          path: deps
          key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
          restore-keys: ${{ runner.os }}-mix-

      - name: Install dependencies
        run: mix deps.get

      - name: Run Tests
        run: mix test

      - name: Run Migrations SDX
        run: MIX_ENV=sdx mix ecto.migrate

      - name: Build SDX Release
        run: MIX_ENV=sdx mix release

      - name: Install SSH key
        uses: shimataro/ssh-key-action@v2
        with:
          key: ${{ secrets.APPLICATION_SERVER_SSH_KEY_SDX }}
          known_hosts: 'to be defined on next step'

      - name: Add Known Hosts
        run: ssh-keyscan -H ${{ secrets.APPLICATION_SERVER_HOST_SDX }} >> ~/.ssh/known_hosts

      - name: Rename Old Build Folder
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.APPLICATION_SERVER_HOST_SDX }}
          username: ${{ secrets.APPLICATION_SERVER_SSH_USER_SDX }}
          key: ${{ secrets.APPLICATION_SERVER_SSH_KEY_SDX }}
          script_stop: true
          script: mv _build _build_old
      
      - name: Stop Old Application
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.APPLICATION_SERVER_HOST_SDX }}
          username: ${{ secrets.APPLICATION_SERVER_SSH_USER_SDX }}
          key: ${{ secrets.APPLICATION_SERVER_SSH_KEY_SDX }}
          script_stop: true
          script: _build_old/sdx/rel/alfred_server/bin/alfred_server stop

      - name: Deploy Release with RSYNC 
        run: rsync -avz ./_build ${{ secrets.APPLICATION_SERVER_SSH_USER_SDX }}@${{ secrets.APPLICATION_SERVER_HOST_SDX }}:~

      - name: Start Application
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.APPLICATION_SERVER_HOST_SDX }}
          username: ${{ secrets.APPLICATION_SERVER_SSH_USER_SDX }}
          key: ${{ secrets.APPLICATION_SERVER_SSH_KEY_SDX }}
          script_stop: true
          script: echo ${{ secrets.APPLICATION_SERVER_USER_PWD_SDX }} | sudo -S _build/sdx/rel/alfred_server/bin/alfred_server daemon

      - name: Remove Old Build Folder
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.APPLICATION_SERVER_HOST_SDX }}
          username: ${{ secrets.APPLICATION_SERVER_SSH_USER_SDX }}
          key: ${{ secrets.APPLICATION_SERVER_SSH_KEY_SDX }}
          script_stop: true
          script: echo ${{ secrets.APPLICATION_SERVER_USER_PWD_SDX }} | sudo -S rm -rf ./_build_old

I’m not experienced with Github Actions stuff also, so I’m happy to hear some suggestions about it too (for instance how to parallelize mix test, build and migrations without having to setup elixir for every job! Did not figure it out yet). :smiley:

My questions are:

1 - Between the steps Rename Old Build Folder and Stop Old Application I have my application running but the original _build folder no longer exists. Can something goes wrong between these 2 steps? Should I stop before renaning? Nor renaning at all?

Something I’ve noticed is that if I run the server and rename the build folder, I can no long access it via the mix release remote command. I think it happens because the iex process cannot find the log files anymore but I’m not sure.

2 - Thinking about minimizing downtime, my first approach was to only run Stop Old Application just before Start Application, and this approach worked when there is no traffic in the application. But when the application has traffic for some reason the new release isn’t started and I don’t know why.

3 - How is your standard CI / CD pipeline for elixir projects? Are they similar to my approach? What we do similar? What we do different?

Thanks very much guys! :smiley:

Most Liked

cpgo

cpgo

I used to run a pipeline on circleci that ran the build and tests in parallel but only ran the deployment step if both the test and build steps passed. Its a good way to shave off a few minutes/seconds of deployment time.

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement