Skip to content

Commit

Permalink
Setup CI
Browse files Browse the repository at this point in the history
  • Loading branch information
almirsarajcic committed Apr 29, 2024
1 parent 378f30c commit 65def71
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
import_deps: [:phoenix],
inputs: ["*.{ex,exs}", "{config,lib,test}/**/*.{ex,exs}"]
inputs: ["*.{ex,exs}", "{.github,config,lib,test}/**/*.{ex,exs}"]
]
103 changes: 103 additions & 0 deletions .github/github_workflows.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
defmodule GithubWorkflows do
def get do
%{
"main.yml" => main_workflow()
}
end

defp main_workflow do
[
[
name: "Test Elixir release",
on: "push",
jobs: [
test_release: [
name: "Build and test release",
"runs-on": "ubuntu-latest",
env: [
FLY_APP_NAME: "testing_release",
FLY_PRIVATE_IP: "0:0:0:0:0:0:0:1",
PHX_HOST: "localhost",
SECRET_KEY_BASE: "8qdSYkvbFbYhUIKIPGsQOoOVdGUIzR+Sh56BJ0E+SU1xD4EsQMV5zCOSgRC5U8Rf"
],
steps: [
[
name: "Checkout",
uses: "actions/checkout@v3"
],
[
name: "Set up Docker Buildx",
uses: "docker/setup-buildx-action@v1"
],
[
name: "Cache Docker layers",
uses: "actions/cache@v3",
with: [
path: "/tmp/.buildx-cache",
key: "${{ runner.os }}-buildx-${{ github.sha }}",
"restore-keys": "${{ runner.os }}-buildx"
]
],
[
name: "Build image",
uses: "docker/build-push-action@v2",
with: [
context: ".",
builder: "${{ steps.buildx.outputs.name }}",
tags: "testing_release:latest",
load: true,
"build-args": "target=testing_release",
"cache-from": "type=local,src=/tmp/.buildx-cache",
"cache-to": "type=local,dest=/tmp/.buildx-cache-new,mode=max"
]
],
[
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
name: "Move cache",
run: "rm -rf /tmp/.buildx-cache\nmv /tmp/.buildx-cache-new /tmp/.buildx-cache"
],
[
name: "Create the container",
id: "create_container",
run:
"echo ::set-output name=container_id::$(docker create -p 4000:4000 -e FLY_APP_NAME=${{ env.FLY_APP_NAME }} -e FLY_PRIVATE_IP=${{ env.FLY_PRIVATE_IP }} -e PHX_HOST=${{ env.PHX_HOST }} -e SECRET_KEY_BASE=${{ env.SECRET_KEY_BASE }} testing_release | tail -1)"
],
[
name: "Start the container",
run: "docker start ${{ steps.create_container.outputs.container_id }}"
],
[
name: "Check HTTP status code",
uses: "nick-fields/retry@v2",
with: [
command:
"INPUT_SITES='[\"http://localhost:4000/api\"]' INPUT_EXPECTED='[200]' ./scripts/check_status_code.sh",
max_attempts: 3,
retry_wait_seconds: 5,
timeout_seconds: 1
]
],
[
name: "Write Docker logs to a file",
if: "failure() && steps.create_container.outcome == 'success'",
run:
"docker logs ${{ steps.create_container.outputs.container_id }} >> docker.log"
],
[
name: "Upload Docker log file",
if: "failure()",
uses: "actions/upload-artifact@v3",
with: [
name: "docker.log",
path: "docker.log"
]
]
]
]
]
]
]
end
end
55 changes: 55 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: Test Elixir release
on: push
jobs:
test_release:
name: Build and test release
runs-on: ubuntu-latest
env:
FLY_APP_NAME: testing_release
FLY_PRIVATE_IP: 0:0:0:0:0:0:0:1
PHX_HOST: localhost
SECRET_KEY_BASE: "8qdSYkvbFbYhUIKIPGsQOoOVdGUIzR+Sh56BJ0E+SU1xD4EsQMV5zCOSgRC5U8Rf"
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: ${{ runner.os }}-buildx
- name: Build image
uses: docker/build-push-action@v2
with:
context: .
builder: ${{ steps.buildx.outputs.name }}
tags: testing_release:latest
load: true
build-args: target=testing_release
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
- name: Move cache
run: "rm -rf /tmp/.buildx-cache\nmv /tmp/.buildx-cache-new /tmp/.buildx-cache"
- name: Create the container
id: create_container
run: "echo ::set-output name=container_id::$(docker create -p 4000:4000 -e FLY_APP_NAME=${{ env.FLY_APP_NAME }} -e FLY_PRIVATE_IP=${{ env.FLY_PRIVATE_IP }} -e PHX_HOST=${{ env.PHX_HOST }} -e SECRET_KEY_BASE=${{ env.SECRET_KEY_BASE }} testing_release | tail -1)"
- name: Start the container
run: docker start ${{ steps.create_container.outputs.container_id }}
- name: Check HTTP status code
uses: nick-fields/retry@v2
with:
command: "INPUT_SITES='[\"http://localhost:4000/api\"]' INPUT_EXPECTED='[200]' ./scripts/check_status_code.sh"
max_attempts: 3
retry_wait_seconds: 5
timeout_seconds: 1
- name: Write Docker logs to a file
if: failure() && steps.create_container.outcome == 'success'
run: docker logs ${{ steps.create_container.outputs.container_id }} >> docker.log
- name: Upload Docker log file
if: failure()
uses: actions/upload-artifact@v3
with:
name: docker.log
path: docker.log
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ defmodule TestingRelease.MixProject do
{:telemetry_poller, "~> 1.0"},
{:jason, "~> 1.2"},
{:dns_cluster, "~> 0.1.1"},
{:bandit, "~> 1.2"}
{:bandit, "~> 1.2"},
{:github_workflows_generator, "~> 0.1", only: :dev, runtime: false}
]
end

Expand Down
3 changes: 3 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
"bandit": {:hex, :bandit, "1.5.0", "3bc864a0da7f013ad3713a7f550c6a6ec0e19b8d8715ec678256a0dc197d5539", [:mix], [{:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:thousand_island, "~> 1.0", [hex: :thousand_island, repo: "hexpm", optional: false]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "92d18d9a7228a597e0d4661ef69a874ea82d63ff49c7d801a5c68cb18ebbbd72"},
"castore": {:hex, :castore, "1.0.7", "b651241514e5f6956028147fe6637f7ac13802537e895a724f90bf3e36ddd1dd", [:mix], [], "hexpm", "da7785a4b0d2a021cd1292a60875a784b6caef71e76bf4917bdee1f390455cf5"},
"dns_cluster": {:hex, :dns_cluster, "0.1.3", "0bc20a2c88ed6cc494f2964075c359f8c2d00e1bf25518a6a6c7fd277c9b0c66", [:mix], [], "hexpm", "46cb7c4a1b3e52c7ad4cbe33ca5079fbde4840dedeafca2baf77996c2da1bc33"},
"fast_yaml": {:hex, :fast_yaml, "1.0.36", "65413a34a570fd4e205a460ba602e4ee7a682f35c22d2e1c839025dbf515105c", [:rebar3], [{:p1_utils, "1.0.25", [hex: :p1_utils, repo: "hexpm", optional: false]}], "hexpm", "1abe8f758fc2a86b08edff80bbc687cfd41ebc1412cfec0ef4a0acfcd032052f"},
"github_workflows_generator": {:hex, :github_workflows_generator, "0.1.0", "446ee8f8d39d0e386705f35eded837e53d17b5e8cd056b97ad6cb37abad290f0", [:mix], [{:fast_yaml, "~> 1.0", [hex: :fast_yaml, repo: "hexpm", optional: false]}], "hexpm", "8523183e8bfd8c9c97e0df30f1f06e500ca06a34f763462f25e85404a4297bb1"},
"hpax": {:hex, :hpax, "0.1.2", "09a75600d9d8bbd064cdd741f21fc06fc1f4cf3d0fcc335e5aa19be1a7235c84", [:mix], [], "hexpm", "2c87843d5a23f5f16748ebe77969880e29809580efdaccd615cd3bed628a8c13"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
"p1_utils": {:hex, :p1_utils, "1.0.25", "2d39b5015a567bbd2cc7033eeb93a7c60d8c84efe1ef69a3473faa07fa268187", [:rebar3], [], "hexpm", "9219214428f2c6e5d3187ff8eb9a8783695c2427420be9a259840e07ada32847"},
"phoenix": {:hex, :phoenix, "1.7.12", "1cc589e0eab99f593a8aa38ec45f15d25297dd6187ee801c8de8947090b5a9d3", [:mix], [{:castore, ">= 0.0.0", [hex: :castore, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.7", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}, {:websock_adapter, "~> 0.5.3", [hex: :websock_adapter, repo: "hexpm", optional: false]}], "hexpm", "d646192fbade9f485b01bc9920c139bfdd19d0f8df3d73fd8eaf2dfbe0d2837c"},
"phoenix_html": {:hex, :phoenix_html, "4.1.1", "4c064fd3873d12ebb1388425a8f2a19348cef56e7289e1998e2d2fa758aa982e", [:mix], [], "hexpm", "f2f2df5a72bc9a2f510b21497fd7d2b86d932ec0598f0210fed4114adc546c6f"},
"phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.8.3", "7ff51c9b6609470f681fbea20578dede0e548302b0c8bdf338b5a753a4f045bf", [:mix], [{:ecto, "~> 3.6.2 or ~> 3.7", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_mysql_extras, "~> 0.5", [hex: :ecto_mysql_extras, repo: "hexpm", optional: true]}, {:ecto_psql_extras, "~> 0.7", [hex: :ecto_psql_extras, repo: "hexpm", optional: true]}, {:ecto_sqlite3_extras, "~> 1.1.7 or ~> 1.2.0", [hex: :ecto_sqlite3_extras, repo: "hexpm", optional: true]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.19 or ~> 1.0", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6 or ~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "f9470a0a8bae4f56430a23d42f977b5a6205fdba6559d76f932b876bfaec652d"},
Expand Down
4 changes: 3 additions & 1 deletion rel/env.sh.eex
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/bin/sh

# configure node for distributed erlang with IPV6 support
export ERL_AFLAGS="-proto_dist inet6_tcp"
if [[ -z "${FLY_PRIVATE_IP}" ]]; then
export ERL_AFLAGS="-proto_dist inet6_tcp"
fi
export ECTO_IPV6="true"
export DNS_CLUSTER_QUERY="${FLY_APP_NAME}.internal"
export RELEASE_DISTRIBUTION="name"
Expand Down
49 changes: 49 additions & 0 deletions scripts/check_status_code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

# export INPUT_SITES='["http://github.com", "https://education.github.com"]'
# export INPUT_EXPECTED='[301, 200]'
# https://github.com/lakuapik/gh-actions-http-status

LRED="\033[1;31m" # Light Red
LGREEN="\033[1;32m" # Light Green
NC='\033[0m' # No Color

text_success="(${LGREEN}Success${NC})"
text_failed="(${LRED}Failed${NC}) "

http_status() {
site_url=$1
expected_status=$2

actual_status=$(curl -o /dev/null -s -w "%{http_code}" $site_url)

if [ "${actual_status}" == "${expected_status}" ]; then
result=${text_success}
else
result=${text_failed}
final_result=${text_failed}
final_result_code=127

fi

echo -e "${result} HTTP Status for ${site_url} is ${actual_status}, expected ${expected_status}"
}

echo -e "\n\n==Starting gh-actions HTTP Status==\n"

final_result="${LGREEN}Success${NC}"
final_result_code=0

len=$(printenv INPUT_SITES | jq -r '. | length')

for ((i=0; i<len; i++))
do
site=$(printenv INPUT_SITES | jq -r '.['${i}']')
expected=$(printenv INPUT_EXPECTED | jq -r '.['${i}']')
http_status $site $expected
done

echo -e "\nHTTP Status result: ${final_result}"
echo -e "\n==END==\n\n"

exit ${final_result_code}

0 comments on commit 65def71

Please sign in to comment.