Optimize CI/CD: Implement Nix-based dev builds (#28) #17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Docker Build and Publish | |
on: | |
push: | |
branches: ["main"] | |
release: | |
types: [created] | |
env: | |
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }} | |
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} | |
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/hxckr-core | |
CARGO_TERM_COLOR: always | |
jobs: | |
build-and-push-dev: | |
if: github.event_name == 'push' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Install Nix | |
uses: cachix/install-nix-action@v20 | |
with: | |
nix_path: nixpkgs=channel:nixos-unstable | |
extra_nix_config: | | |
experimental-features = nix-command flakes | |
accept-flake-config = true | |
keep-outputs = true | |
keep-derivations = true | |
- name: Setup Nix caching | |
uses: actions/cache@v3 | |
with: | |
path: | | |
~/.cache/nix | |
key: ${{ runner.os }}-nix-${{ hashFiles('**/docker-image.nix', '**/Cargo.lock') }} | |
restore-keys: | | |
${{ runner.os }}-nix- | |
- name: Build with Nix | |
run: | | |
nix-build build-image.nix | |
docker load < result | |
- name: Verify image contents | |
run: | | |
docker run --rm hxckr-core:latest ls -l /app | |
docker run --rm hxckr-core:latest ls -l /app/migrations | |
docker run --rm hxckr-core:latest cat /app/entrypoint.sh | |
docker run --rm hxckr-core:latest which diesel | |
docker run --rm hxckr-core:latest diesel --version | |
- name: Login to DockerHub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ env.DOCKERHUB_USERNAME }} | |
password: ${{ env.DOCKERHUB_TOKEN }} | |
- name: Push Docker image | |
run: | | |
docker tag hxckr-core:latest ${{ env.IMAGE_NAME }}:dev | |
docker tag hxckr-core:latest ${{ env.IMAGE_NAME }}:${{ github.sha }} | |
docker push ${{ env.IMAGE_NAME }}:dev | |
docker push ${{ env.IMAGE_NAME }}:${{ github.sha }} | |
- name: Print image size and details | |
run: | | |
docker image ls ${{ env.IMAGE_NAME }}:dev | |
docker history ${{ env.IMAGE_NAME }}:dev | |
build-and-push-prod: | |
if: github.event_name == 'release' | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Setup Rust | |
uses: actions-rs/toolchain@v1 | |
with: | |
profile: minimal | |
toolchain: stable | |
- name: Build project | |
run: | | |
cargo install cargo-chef | |
cargo chef prepare --recipe-path recipe.json | |
cargo chef cook --release --recipe-path recipe.json | |
cargo build --release --all-features | |
- name: Login to DockerHub | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ env.DOCKERHUB_USERNAME }} | |
password: ${{ env.DOCKERHUB_TOKEN }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
file: Dockerfile | |
platforms: linux/amd64,linux/arm64 | |
push: true | |
tags: | | |
${{ env.IMAGE_NAME }}:latest | |
${{ env.IMAGE_NAME }}:${{ github.ref_name }} | |
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache | |
cache-to: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache,mode=max | |
- name: Run migrations | |
env: | |
DATABASE_URL: ${{ secrets.PRODUCTION_DATABASE_URL }} | |
run: | | |
docker run --rm \ | |
-e DATABASE_URL \ | |
${{ env.IMAGE_NAME }}:${{ github.ref_name }} \ | |
diesel migration run | |
- name: Print image size and details | |
run: | | |
docker image ls ${{ env.IMAGE_NAME }}:${{ github.ref_name }} | |
docker history ${{ env.IMAGE_NAME }}:${{ github.ref_name }} |