Skip to content

Commit

Permalink
fix: simplify CI
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Sep 9, 2024
1 parent 6083645 commit e385429
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 106 deletions.
163 changes: 142 additions & 21 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: ci
env:
EARTHLY_TOKEN: ${{ secrets.EARTHLY_TOKEN }}
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
FORCE_COLOR: 1
REGISTRY: ghcr.io
IMAGE_NAME: peapescarte/pescarte-plataforma

on:
push:
Expand All @@ -14,35 +12,158 @@ on:
jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
otp: [26.2.5]
elixir: [1.16.2]
steps:
- uses: earthly/actions-setup@v1
- uses: actions/checkout@v4

- uses: erlef/setup-beam@v1
with:
version: v0.8.14
- uses: actions/checkout@v2
- name: Run build
run: earthly --ci --build-arg MIX_ENV=dev +ci
otp-version: ${{ matrix.otp }}
elixir-version: ${{ matrix.elixir }}
experimental-otp: true

- name: Cache Elixir deps
uses: actions/cache@v1
id: deps-cache
with:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}

- name: Cache Elixir _build
uses: actions/cache@v1
id: build-cache
with:
path: _build
key: ${{ runner.os }}-build-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
- name: Run compiler checks
run: mix clean && mix compile --force --warning-as-errors

- name: Run formatter check
run: mix format --check-formatted

- name: Run static analysis
run: mix credo --strict

test:
runs-on: ubuntu-latest
env:
SUPABASE_KEY: "super-ci-key"
SUPABASE_URL: "http://localhost:123"
DATABASE_USER: "peapescarte"
DATABASE_PASS: "peapescarte"
strategy:
matrix:
otp: [26.2.5]
elixir: [1.16.2]
services:
postgres:
image: postgres:15
ports:
- 5432:5432
env:
POSTGRES_USER: peapescarte
POSTGRES_PASSWORD: peapescarte
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4

- uses: erlef/setup-beam@v1
with:
otp-version: ${{ matrix.otp }}
elixir-version: ${{ matrix.elixir }}
experimental-otp: true

- name: Cache Elixir deps
uses: actions/cache@v1
id: deps-cache
with:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}

- name: Cache Elixir _build
uses: actions/cache@v1
id: build-cache
with:
path: _build
key: ${{ runner.os }}-build-${{ matrix.otp }}-${{ matrix.elixir }}-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.lock')) }}
- name: Run tests
run: mix test

build-dev:
runs-on: ubuntu-latest
needs: test
if: success() && github.ref == 'refs/heads/main'
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- uses: earthly/actions-setup@v1
- uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
version: v0.8.14
- uses: actions/checkout@v2
- name: Run build
run: earthly -P --ci --build-arg MIX_ENV=test +test
registry: ${{ env.REGISTRY }}
username: ${{ secrets.GHCR_USER }}
password: ${{ secrets.GHCR_TOKEN }}

- name: Build and push Docker image (DEV)
id: push
uses: docker/build-push-action@v6
with:
context: .
from: ./Dockerfile
push: true
tags: ${{ env.IMAGE_NAME }}:dev
build-args: MIX_ENV="dev"
target: builder

- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true

build-prod:
runs-on: ubuntu-latest
needs: test
if: success() && github.ref == 'refs/heads/main'
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- uses: earthly/actions-setup@v1
- uses: actions/checkout@v4

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
version: v0.8.14
- uses: actions/checkout@v2
- name: Docker Login
run: echo "$DOCKERHUB_TOKEN" | docker login --username "$DOCKERHUB_USERNAME" --password-stdin
- name: Run build
run: earthly -P --ci --push --build-arg GITHUB_REPO=${{ github.repository }} +docker
registry: ${{ env.REGISTRY }}
username: ${{ secrets.GHCR_USER }}
password: ${{ secrets.GHCR_TOKEN }}

- name: Build and push Docker image (PROD)
id: push
uses: docker/build-push-action@v6
with:
context: .
from: ./Dockerfile
push: true
tags: ${{ env.IMAGE_NAME }}:prod

- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ARG ELIXIR_VERSION=1.16.0
ARG OTP_VERSION=26.1.2
ARG ALPINE_VERSION=3.18.4
ARG MIX_ENV=prod

ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-alpine-${ALPINE_VERSION}"
ARG RUNNER_IMAGE="alpine:${ALPINE_VERSION}"
Expand All @@ -17,9 +18,6 @@ RUN apk add --no-cache build-base gcc curl git wget nodejs npm
RUN mix local.hex --force && \
mix local.rebar --force

# set build ENV
ENV MIX_ENV="prod"

# install mix dependencies
COPY mix.exs mix.lock ./
RUN mix deps.get --only $MIX_ENV
Expand Down
69 changes: 0 additions & 69 deletions Earthfile

This file was deleted.

13 changes: 0 additions & 13 deletions docker-compose.ci.yml

This file was deleted.

0 comments on commit e385429

Please sign in to comment.