Skip to content

Commit

Permalink
[Recognizer] Setup Recognizer CD
Browse files Browse the repository at this point in the history
  • Loading branch information
mickel8 committed Jul 18, 2024
1 parent 395f5dc commit f052232
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 6 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/__CD__build-publish-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
on:
workflow_call:
inputs:
app-name:
required: true
type: string

env:
REGISTRY: ghcr.io

jobs:
build-publish-image:
name: "Build and publish image"
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v4
with:
sparse-checkout: ${{ inputs.app-name }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to Container Registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ github.repository }}/${{ inputs.app-name }}
tags: type=match,pattern=${{ inputs.app-name }}-v(.*),group=1

- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: ./${{ inputs.app-name }}
platforms: linux/amd64
push: true
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max
43 changes: 43 additions & 0 deletions .github/workflows/__CD__deploy-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
on:
workflow_call:
inputs:
app-name:
required: true
type: string
secrets:
ssh-host:
required: true
ssh-username:
required: true
ssh-priv-key:
required: true
secret-key-base:
required: true
phx-host:
required: true
ice-port-range:
required: true

jobs:
deploy-image:
name: Deploy image
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v4
with:
sparse-checkout: ${{ inputs.app-name }}

- name: Run docker via remote SSH
uses: appleboy/[email protected]
with:
host: ${{ secrets.ssh-host }}
username: ${{ secrets.ssh-username }}
key: ${{ secrets.ssh-priv-key }}
script: |
export TAG=${{ github.ref_name }}
export TAG=${TAG#*-v}
docker stop ${{ inputs.app-name }}
docker rm ${{ inputs.app-name }}
docker run -d --restart unless-stopped --name ${{ inputs.app-name }} -e SECRET_KEY_BASE=${{ secrets.secret-key-base }} -e PHX_HOST=${{ secrets.phx-host }} -e ICE_PORT_RANGE=${{ secrets.ice-port-range }} --network host ghcr.io/elixir-webrtc/apps/${{ inputs.app-name }}:${TAG}
docker image prune --all --force
30 changes: 30 additions & 0 deletions .github/workflows/__CD__recognizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Recognizer CD

on:
push:
tags:
- "recognizer-v*.*.*"

permissions:
contents: read
packages: write

jobs:
build-publish-recognizer-image:
name: "Build and publish Recognizer image"
uses: ./.github/workflows/__CD__build-publish-image.yml
with:
app-name: recognizer
deploy-recognizer:
name: "Deploy Recognizer image"
needs: build-publish-recognizer-image
uses: ./.github/workflows/__CD__deploy-image.yml
with:
app-name: recognizer
secrets:
ssh-host: ${{ secrets.RECOGNIZER_SSH_HOST }}
ssh-username: ${{ secrets.RECOGNIZER_SSH_USERNAME }}
ssh-priv-key: ${{ secrets.RECOGNIZER_SSH_PRIV_KEY }}
secret-key-base: ${{ secrets.RECOGNIZER_SECRET_KEY_BASE }}
phx-host: ${{ secrets.RECOGNIZER_PHX_HOST }}
ice-port-range: ${{ secrets.RECOGNIZER_ICE_PORT_RANGE }}
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml → .github/workflows/__CI__all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ permissions:
jobs:
build_check_recognizer:
name: Recognizer CI
uses: ./.github/workflows/build-check-app.yml
uses: ./.github/workflows/__CI__build-check-app.yml
with:
workdir: recognizer
with-ffmpeg: true

build_check_broadcaster:
name: Broadcaster CI
uses: ./.github/workflows/build-check-app.yml
uses: ./.github/workflows/__CI__build-check-app.yml
with:
workdir: broadcaster

build_check_nexus:
name: Nexus CI
uses: ./.github/workflows/build-check-app.yml
uses: ./.github/workflows/__CI__build-check-app.yml
with:
workdir: nexus
File renamed without changes.
16 changes: 16 additions & 0 deletions recognizer/config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,26 @@ import Config
#
# Alternatively, you can use `mix phx.gen.release` to generate a `bin/server`
# script that automatically sets the env var above.

read_ice_port_range! = fn ->
case System.get_env("ICE_PORT_RANGE") do
nil ->
[0]

raw_port_range ->
case String.split(raw_port_range, "-", parts: 2) do
[from, to] -> String.to_integer(from)..String.to_integer(to)
_other -> raise "ICE_PORT_RANGE has to be in form of FROM-TO, passed: #{raw_port_range}"
end
end
end

if System.get_env("PHX_SERVER") do
config :recognizer, RecognizerWeb.Endpoint, server: true
end

config :recognizer, ice_port_range: read_ice_port_range!.()

if config_env() == :prod do
# The secret key base is used to sign/encrypt cookies and other secrets.
# A default value is used in config/dev.exs and config/test.exs but you
Expand Down
9 changes: 8 additions & 1 deletion recognizer/lib/recognizer/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ defmodule Recognizer.Room do
@impl true
def handle_call({:connect, channel_pid}, _from, %{channel: nil} = state) do
Process.monitor(channel_pid)
{:ok, pc} = PeerConnection.start_link(video_codecs: @video_codecs)

ice_port_range = Application.fetch_env!(:recognizer, :ice_port_range)

{:ok, pc} =
PeerConnection.start_link(
video_codecs: @video_codecs,
ice_port_range: ice_port_range
)

state =
state
Expand Down
3 changes: 2 additions & 1 deletion recognizer/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ defmodule Recognizer.MixProject do
{:jason, "~> 1.2"},
{:dns_cluster, "~> 0.1.1"},
{:plug_cowboy, "~> 2.5"},
{:ex_webrtc, "~> 0.3.0"},
# {:ex_webrtc, "~> 0.3.0"},
{:ex_webrtc, github: "elixir-webrtc/ex_webrtc", branch: "ice-port-range", override: true},
{:ex_webrtc_dashboard, "~> 0.3.0"},
{:xav, "~> 0.4.0"},
{:bumblebee, "~> 0.5.3"},
Expand Down
2 changes: 1 addition & 1 deletion recognizer/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"ex_sdp": {:hex, :ex_sdp, "0.17.0", "4c50e7814f01f149c0ccf258fba8428f8567dffecf1c416ec3f6aaaac607a161", [:mix], [{:bunch, "~> 1.3", [hex: :bunch, repo: "hexpm", optional: false]}, {:elixir_uuid, "~> 1.2", [hex: :elixir_uuid, repo: "hexpm", optional: false]}], "hexpm", "c7fe0625902be2a835b5fe6834a189f7db7639d2625c8e9d8b3564e6d704145f"},
"ex_stun": {:hex, :ex_stun, "0.2.0", "feb1fc7db0356406655b2a617805e6c712b93308c8ea2bf0ba1197b1f0866deb", [:mix], [], "hexpm", "1e01ba8290082ccbf37acaa5190d1f69b51edd6de2026a8d6d51368b29d115d0"},
"ex_turn": {:hex, :ex_turn, "0.1.0", "177405aadf3d754567d0d37cf881a83f9cacf8f45314d188633b04c4a9e7c1ec", [:mix], [{:ex_stun, "~> 0.2.0", [hex: :ex_stun, repo: "hexpm", optional: false]}], "hexpm", "d677737fb7d45274d5dac19fe3c26b9038b6effbc0a6b3e7417bccc76b6d1cd3"},
"ex_webrtc": {:hex, :ex_webrtc, "0.3.0", "283f5b31d539f65238596793aabcefe32d221618ceb751ae68951712a486cac2", [:mix], [{:crc, "~> 0.10", [hex: :crc, repo: "hexpm", optional: false]}, {:ex_dtls, "~> 0.15.0", [hex: :ex_dtls, repo: "hexpm", optional: false]}, {:ex_ice, "~> 0.7.0", [hex: :ex_ice, repo: "hexpm", optional: false]}, {:ex_libsrtp, "~> 0.7.1", [hex: :ex_libsrtp, repo: "hexpm", optional: false]}, {:ex_rtcp, "~> 0.4.0", [hex: :ex_rtcp, repo: "hexpm", optional: false]}, {:ex_rtp, "~> 0.4.0", [hex: :ex_rtp, repo: "hexpm", optional: false]}, {:ex_sdp, "~> 0.17.0", [hex: :ex_sdp, repo: "hexpm", optional: false]}], "hexpm", "a8a4f38cdcacae170615d6abb83d8c42220b6ac0133d84b900f4994d5eff7143"},
"ex_webrtc": {:git, "https://github.com/elixir-webrtc/ex_webrtc.git", "3d6edd7612f2d90258da4faf1ee397775048459d", [branch: "ice-port-range"]},
"ex_webrtc_dashboard": {:hex, :ex_webrtc_dashboard, "0.3.0", "f38cc5847a6e7617b950119c46dd090b420c0be90a22113f37e51cd2056a3c19", [:mix], [{:ex_webrtc, "~> 0.3.0", [hex: :ex_webrtc, repo: "hexpm", optional: false]}, {:phoenix_live_dashboard, "~> 0.8.3", [hex: :phoenix_live_dashboard, repo: "hexpm", optional: false]}], "hexpm", "73ce31ee429c05455090443eb4d573bef5c69fbc170fb83825587eafbb04881e"},
"exla": {:hex, :exla, "0.7.3", "51310270a0976974fc758f7b28ebd6ca8e099b3d6fc78b0d484c808e977cb914", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:nx, "~> 0.7.1", [hex: :nx, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:xla, "~> 0.6.0", [hex: :xla, repo: "hexpm", optional: false]}], "hexpm", "5b3d5741a24aada21d3b0feb4b99d1fc3c8457f995a63ea16684d8d5678b96ff"},
"expo": {:hex, :expo, "0.5.2", "beba786aab8e3c5431813d7a44b828e7b922bfa431d6bfbada0904535342efe2", [:mix], [], "hexpm", "8c9bfa06ca017c9cb4020fabe980bc7fdb1aaec059fd004c2ab3bff03b1c599c"},
Expand Down

0 comments on commit f052232

Please sign in to comment.