- Move MIX_ENV and DATABASE_URL to job-level env vars - Removes need to set them on individual steps - Ensures all database operations use correct hostname (postgres) - Fixes 'connection refused' errors from hardcoded localhost in test.exs
157 lines
4.7 KiB
YAML
157 lines
4.7 KiB
YAML
name: Build
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- main
|
|
|
|
env:
|
|
DOCKER_BUILDKIT: 1
|
|
DOCKER_TLS_CERTDIR: ""
|
|
|
|
concurrency:
|
|
group: ${{ github.workflow }}-${{ github.ref }}
|
|
cancel-in-progress: true
|
|
|
|
jobs:
|
|
test:
|
|
runs-on: ubuntu-22.04
|
|
env:
|
|
MIX_ENV: test
|
|
DATABASE_URL: postgresql://postgres:postgres@postgres:5432/towerops_test
|
|
services:
|
|
postgres:
|
|
image: timescale/timescaledb:latest-pg17
|
|
env:
|
|
POSTGRES_USER: postgres
|
|
POSTGRES_PASSWORD: postgres
|
|
POSTGRES_DB: towerops_test
|
|
options: >-
|
|
--health-cmd pg_isready
|
|
--health-interval 10s
|
|
--health-timeout 5s
|
|
--health-retries 5
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Elixir
|
|
uses: https://github.com/erlef/setup-beam@v1
|
|
with:
|
|
otp-version: '28.3'
|
|
elixir-version: '1.19.5'
|
|
version-type: strict
|
|
|
|
- name: Cache system dependencies
|
|
uses: actions/cache@v4
|
|
id: system-deps-cache
|
|
with:
|
|
path: /var/cache/apt
|
|
key: ${{ runner.os }}-apt-${{ hashFiles('.forgejo/workflows/build.yaml') }}
|
|
restore-keys: ${{ runner.os }}-apt-
|
|
|
|
- name: Install system dependencies
|
|
run: |
|
|
sudo apt-get update
|
|
sudo apt-get install -y libsnmp-dev snmp-mibs-downloader postgresql-client
|
|
|
|
- name: Cache Mix dependencies
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: deps
|
|
key: ${{ runner.os }}-mix-deps-${{ hashFiles('**/mix.lock') }}-elixir-1.19.5-otp-28.3
|
|
restore-keys: |
|
|
${{ runner.os }}-mix-deps-${{ hashFiles('**/mix.lock') }}-
|
|
${{ runner.os }}-mix-deps-
|
|
|
|
- name: Cache compiled build
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: _build
|
|
key: ${{ runner.os }}-mix-build-${{ hashFiles('**/mix.lock') }}-${{ hashFiles('lib/**/*.ex') }}-elixir-1.19.5-otp-28.3
|
|
restore-keys: |
|
|
${{ runner.os }}-mix-build-${{ hashFiles('**/mix.lock') }}-
|
|
${{ runner.os }}-mix-build-
|
|
|
|
- name: Install dependencies
|
|
run: mix deps.get
|
|
|
|
- name: Wait for PostgreSQL
|
|
run: |
|
|
until pg_isready -h postgres -p 5432 -U postgres; do
|
|
echo "Waiting for PostgreSQL..."
|
|
sleep 2
|
|
done
|
|
echo "PostgreSQL is ready!"
|
|
|
|
- name: Setup database
|
|
run: mix ecto.create
|
|
|
|
- name: Check code formatting
|
|
run: mix format --check-formatted
|
|
|
|
- name: Compile with warnings as errors
|
|
run: mix compile --warnings-as-errors
|
|
|
|
- name: Run Credo
|
|
run: mix credo --strict
|
|
|
|
- name: Run tests
|
|
run: mix test --warnings-as-errors
|
|
|
|
build:
|
|
runs-on: ubuntu-22.04
|
|
needs: test
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v3
|
|
with:
|
|
buildkitd-config-inline: |
|
|
[registry."docker.io"]
|
|
mirrors = ["docker-mirror.mcintire.me"]
|
|
|
|
- name: Log in to container registry
|
|
uses: docker/login-action@v3
|
|
with:
|
|
registry: ${{ secrets.REGISTRY_URL }}
|
|
username: ${{ secrets.REGISTRY_USER }}
|
|
password: ${{ secrets.REGISTRY_PASSWORD }}
|
|
|
|
- name: Extract metadata
|
|
id: meta
|
|
run: |
|
|
TIMESTAMP=$(date +%s)
|
|
SHORT_SHA=$(echo "${{ github.sha }}" | cut -c1-7)
|
|
IMAGE=${{ secrets.REGISTRY_URL }}/${{ github.repository }}
|
|
echo "image=${IMAGE}" >> $GITHUB_OUTPUT
|
|
echo "tag=${IMAGE}:main-${TIMESTAMP}-${SHORT_SHA}" >> $GITHUB_OUTPUT
|
|
echo "latest_tag=${IMAGE}:latest" >> $GITHUB_OUTPUT
|
|
echo "cache_tag=${IMAGE}:buildcache" >> $GITHUB_OUTPUT
|
|
|
|
- name: Build and push Docker image
|
|
uses: docker/build-push-action@v6
|
|
with:
|
|
context: .
|
|
push: true
|
|
tags: |
|
|
${{ steps.meta.outputs.tag }}
|
|
${{ steps.meta.outputs.latest_tag }}
|
|
cache-from: type=registry,ref=${{ steps.meta.outputs.cache_tag }}
|
|
cache-to: type=registry,ref=${{ steps.meta.outputs.cache_tag }},mode=max
|
|
|
|
- name: Update deployment image tag
|
|
run: |
|
|
sed -i "s|image: git\.mcintire\.me/graham/towerops-web:.*|image: ${{ steps.meta.outputs.tag }}|g" k8s/deployment.yaml
|
|
git config user.email "ci@git.mcintire.me"
|
|
git config user.name "CI"
|
|
git add k8s/deployment.yaml
|
|
if ! git diff --cached --quiet; then
|
|
git commit -m "chore: update towerops-web image to ${{ steps.meta.outputs.tag }} [skip ci]"
|
|
# Pull with rebase to handle concurrent pushes
|
|
git pull --rebase origin main
|
|
git push origin HEAD:main
|
|
fi
|