oliveiragahenrique

oliveiragahenrique

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:

Showing Posts 3 to 1

hudsonbay

hudsonbay

BTW, if you want to save deployment time on the testing part you can use this API Reference — husky v1.0.3. This won’t let you push to GitHub unless all test have passed successfully. Maybe by using that you can skip doing the same test again on GitHub Actions. I don’t know if this is a good practice or not but it’s an idea :light_bulb:

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.

hudsonbay

hudsonbay

Maybe I’m wrong on this. But you don’t want to parallelize the Elixir test. The thing is that when you are in the testing step, a new independent VM is gonna be created for that on the Microsoft servers (I guess) although you can have your own servers if you want → your project is gonna be setup → the tests are gonna run. If the tests fail then you won’t want to continue with the next step, which is the deployment part. So it’s a secuencial job, (only if the former is successful then you will run the latter), you don’t want it parallelized. I hope it makes sense. Maybe I’m wrong with this, though

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews