Skip to content

Commit b690647

Browse files
authored
[Recognizer] Setup CD (#29)
1 parent b6396d4 commit b690647

File tree

14 files changed

+156
-12
lines changed

14 files changed

+156
-12
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
on:
2+
workflow_call:
3+
inputs:
4+
app-name:
5+
required: true
6+
type: string
7+
8+
env:
9+
REGISTRY: ghcr.io
10+
11+
jobs:
12+
build-publish-image:
13+
name: "Build and publish image"
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout the code
17+
uses: actions/checkout@v4
18+
with:
19+
sparse-checkout: ${{ inputs.app-name }}
20+
21+
- name: Set up Docker Buildx
22+
uses: docker/setup-buildx-action@v2
23+
24+
- name: Login to Container Registry
25+
uses: docker/login-action@v2
26+
with:
27+
registry: ${{ env.REGISTRY }}
28+
username: ${{ github.actor }}
29+
password: ${{ secrets.GITHUB_TOKEN }}
30+
31+
- name: Extract metadata (tags, labels) for Docker
32+
id: meta
33+
uses: docker/metadata-action@v4
34+
with:
35+
images: ${{ env.REGISTRY }}/${{ github.repository }}/${{ inputs.app-name }}
36+
tags: type=match,pattern=${{ inputs.app-name }}-v(.*),group=1
37+
38+
- name: Build and push Docker image
39+
uses: docker/build-push-action@v4
40+
with:
41+
context: ./${{ inputs.app-name }}
42+
platforms: linux/amd64
43+
push: true
44+
tags: ${{ steps.meta.outputs.tags }}
45+
cache-from: type=gha
46+
cache-to: type=gha,mode=max
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
on:
2+
workflow_call:
3+
inputs:
4+
app-name:
5+
required: true
6+
type: string
7+
secrets:
8+
ssh-host:
9+
required: true
10+
ssh-username:
11+
required: true
12+
ssh-priv-key:
13+
required: true
14+
secret-key-base:
15+
required: true
16+
phx-host:
17+
required: true
18+
ice-port-range:
19+
required: true
20+
21+
jobs:
22+
deploy-image:
23+
name: Deploy image
24+
runs-on: ubuntu-latest
25+
steps:
26+
- name: Run docker via remote SSH
27+
uses: appleboy/ssh-action@v1.0.3
28+
with:
29+
host: ${{ secrets.ssh-host }}
30+
username: ${{ secrets.ssh-username }}
31+
key: ${{ secrets.ssh-priv-key }}
32+
script: |
33+
export TAG=${{ github.ref_name }}
34+
export TAG=${TAG#*-v}
35+
docker stop ${{ inputs.app-name }}
36+
docker rm ${{ inputs.app-name }}
37+
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}
38+
docker image prune --all --force
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Recognizer CD
2+
3+
on:
4+
push:
5+
tags:
6+
- "recognizer-v*.*.*"
7+
8+
permissions:
9+
contents: read
10+
packages: write
11+
12+
jobs:
13+
build-publish-recognizer-image:
14+
name: "Build and publish Recognizer image"
15+
uses: ./.github/workflows/__CD__build-publish-image.yml
16+
with:
17+
app-name: recognizer
18+
deploy-recognizer:
19+
name: "Deploy Recognizer image"
20+
needs: build-publish-recognizer-image
21+
uses: ./.github/workflows/__CD__deploy-image.yml
22+
with:
23+
app-name: recognizer
24+
secrets:
25+
ssh-host: ${{ secrets.RECOGNIZER_SSH_HOST }}
26+
ssh-username: ${{ secrets.RECOGNIZER_SSH_USERNAME }}
27+
ssh-priv-key: ${{ secrets.RECOGNIZER_SSH_PRIV_KEY }}
28+
secret-key-base: ${{ secrets.RECOGNIZER_SECRET_KEY_BASE }}
29+
phx-host: ${{ secrets.RECOGNIZER_PHX_HOST }}
30+
ice-port-range: ${{ secrets.RECOGNIZER_ICE_PORT_RANGE }}

.github/workflows/ci.yml renamed to .github/workflows/__CI__all.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ permissions:
88
jobs:
99
build_check_recognizer:
1010
name: Recognizer CI
11-
uses: ./.github/workflows/build-check-app.yml
11+
uses: ./.github/workflows/__CI__build-check-app.yml
1212
with:
1313
workdir: recognizer
1414
with-ffmpeg: true
1515

1616
build_check_broadcaster:
1717
name: Broadcaster CI
18-
uses: ./.github/workflows/build-check-app.yml
18+
uses: ./.github/workflows/__CI__build-check-app.yml
1919
with:
2020
workdir: broadcaster
2121

2222
build_check_nexus:
2323
name: Nexus CI
24-
uses: ./.github/workflows/build-check-app.yml
24+
uses: ./.github/workflows/__CI__build-check-app.yml
2525
with:
2626
workdir: nexus

.github/workflows/build-check-app.yml renamed to .github/workflows/__CI__build-check-app.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ jobs:
3939
with:
4040
node-version: 22
4141

42+
- name: Install libsrtp2
43+
working-directory: .
44+
run: |
45+
sudo apt-get update
46+
sudo apt-get install -y libsrtp2-dev
47+
4248
- name: Install FFmpeg development libraries
4349
if: ${{ inputs.with-ffmpeg }}
4450
working-directory: .

recognizer/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20231009-slim - for the release image
1111
# - https://pkgs.org/ - resource for finding needed packages
1212
# - Ex: hexpm/elixir:1.16.0-erlang-26.2.1-debian-bullseye-20231009-slim
13-
1413
ARG ELIXIR_VERSION=1.17.2
1514
ARG OTP_VERSION=27.0.1
1615
ARG DEBIAN_VERSION=bookworm-20240701-slim

recognizer/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Now you can visit [`localhost:5002`](http://localhost:5002) from your browser.
1313

1414
You can also run Recognizer using Docker.
1515

16-
Build an image:
16+
Build an image (or use `ghcr.io/elixir-webrtc/apps/recognizer:latest`):
1717

1818
```
1919
docker build -t recognizer .

recognizer/config/runtime.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,26 @@ import Config
1616
#
1717
# Alternatively, you can use `mix phx.gen.release` to generate a `bin/server`
1818
# script that automatically sets the env var above.
19+
20+
read_ice_port_range! = fn ->
21+
case System.get_env("ICE_PORT_RANGE") do
22+
nil ->
23+
[0]
24+
25+
raw_port_range ->
26+
case String.split(raw_port_range, "-", parts: 2) do
27+
[from, to] -> String.to_integer(from)..String.to_integer(to)
28+
_other -> raise "ICE_PORT_RANGE has to be in form of FROM-TO, passed: #{raw_port_range}"
29+
end
30+
end
31+
end
32+
1933
if System.get_env("PHX_SERVER") do
2034
config :recognizer, RecognizerWeb.Endpoint, server: true
2135
end
2236

37+
config :recognizer, ice_port_range: read_ice_port_range!.()
38+
2339
if config_env() == :prod do
2440
# The secret key base is used to sign/encrypt cookies and other secrets.
2541
# A default value is used in config/dev.exs and config/test.exs but you

recognizer/lib/recognizer/room.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,14 @@ defmodule Recognizer.Room do
5959
@impl true
6060
def handle_call({:connect, channel_pid}, _from, %{channel: nil} = state) do
6161
Process.monitor(channel_pid)
62-
{:ok, pc} = PeerConnection.start_link(video_codecs: @video_codecs)
62+
63+
ice_port_range = Application.fetch_env!(:recognizer, :ice_port_range)
64+
65+
{:ok, pc} =
66+
PeerConnection.start_link(
67+
video_codecs: @video_codecs,
68+
ice_port_range: ice_port_range
69+
)
6370

6471
state =
6572
state

recognizer/lib/recognizer_web/controllers/room_controller.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ defmodule RecognizerWeb.RoomController do
22
use RecognizerWeb, :controller
33

44
def room(conn, _params) do
5-
render(conn, :room)
5+
render(conn, :room, page_title: "Room")
66
end
77
end

0 commit comments

Comments
 (0)