dood/.gitlab-ci.yml
2025-12-15 09:32:41 -08:00

128 lines
3.8 KiB
YAML

stages:
- configure
- build
- test
- package
# Set variables used for builds in CI, should probably set this in the build container if possible
variables:
AF_PATH: /usr/local
# Use Docker-outside-of-Docker (DooD) by mounting the host's Docker socket
DOCKER_HOST: unix:///var/run/docker.sock
# Disable TLS as we're using the local socket
DOCKER_TLS_CERTDIR: ""
DOCKER_AUTH_CONFIG: '{"auths":{"gl.whitefoxdefense.com:5050":{"auth":"Z2l0bGFiK2RlcGxveS10b2tlbi04OmdsZHQtSnNiVFFoeXhpUWNxcHFXMndfYjY="}}}'
LD_LIBRARY_PATH: /usr/local/lib
# Experimental for the EKS runner
# DOCKER_HOST: "tcp://docker:2375"
# Dynamically pull the build container image name from the devcontainer.json file.
# This acts as a single-source-of-truth to keep local and CI builds unified.
configure:
stage: configure
image: alpine:latest
before_script:
- apk add --no-cache jq
script:
- BUILD_IMAGE=$(grep '"image":' .devcontainer/devcontainer.json | cut -d '"' -f 4)
- printf 'BUILD_IMAGE=%s' "$BUILD_IMAGE" > build_image.env
- ls -la build_image.env
- cat build_image.env
artifacts:
reports:
dotenv: build_image.env
tags:
- test-ci-cd
# Build the Rust application using Nix
build:
stage: build
image: nixos/nix:latest
before_script:
# Enable flakes and nix-command
- mkdir -p ~/.config/nix
- echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf
script:
# Generate Cargo.lock if it doesn't exist
- nix develop --command cargo generate-lockfile || true
# Build the Rust application
- nix build .#app
# Copy the result for artifacts
- mkdir -p build-output
- cp -rL result/* build-output/ || cp result build-output/hello-world
artifacts:
paths:
- build-output/
- Cargo.lock
expire_in: 1 hour
tags:
- test-ci-cd
# Test the application
test:
stage: test
image: nixos/nix:latest
before_script:
- mkdir -p ~/.config/nix
- echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf
script:
# Run the application in a Nix shell with all dependencies
- nix develop --command cargo test
# You could also run the binary here if needed
# - nix run .#app
dependencies:
- build
tags:
- test-ci-cd
# Build Docker image using Nix and load it into Docker (DooD pattern)
build-docker-image:
stage: package
image: nixos/nix:latest
before_script:
# Install Docker CLI in the Nix container
- nix-env -iA nixpkgs.docker
# Enable flakes
- mkdir -p ~/.config/nix
- echo "experimental-features = nix-command flakes" >> ~/.config/nix/nix.conf
script:
# Build the Docker image using Nix
- nix build .#docker
# Load the image into Docker daemon (running on host via socket)
- docker load < result
# Tag the image
- docker tag hello-world:latest hello-world:${CI_COMMIT_SHORT_SHA}
# Test run the container
- docker run --rm hello-world:latest
# Optional: Push to registry if configured
# - docker tag hello-world:latest ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHORT_SHA}
# - docker push ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHORT_SHA}
dependencies:
- build
tags:
- test-ci-cd
# This job requires a GitLab runner with Docker socket access
# The runner should have /var/run/docker.sock mounted
# Alternative: Build using Docker directly (DooD)
build-docker-traditional:
stage: package
image: docker:latest
services: [] # No dind service
variables:
DOCKER_HOST: unix:///var/run/docker.sock
before_script:
# Verify Docker access
- docker info
script:
# Build the Docker image
- docker build -t hello-world:traditional-${CI_COMMIT_SHORT_SHA} .
# Test run
- docker run --rm hello-world:traditional-${CI_COMMIT_SHORT_SHA}
dependencies:
- build
tags:
- test-ci-cd
only:
- branches
when: manual