towerops-agent/.gitlab-ci.yml

132 lines
4 KiB
YAML

# GitLab CI/CD Configuration for Towerops Agent
# Builds and publishes Docker image to Docker Hub
stages:
- test
- build
- release
variables:
# Docker Hub Registry
REGISTRY: docker.io
IMAGE_NAME: gmcintire/towerops-agent
# Docker BuildKit for better caching
DOCKER_BUILDKIT: 1
# Disable TLS for buildx compatibility
DOCKER_TLS_CERTDIR: ""
# Test stage - compile and check code
test:
stage: test
image: rust:1.83-alpine
before_script:
- apk add --no-cache musl-dev protobuf-dev
- rustup component add rustfmt clippy
script:
- cargo check --release
- cargo fmt -- --check
- cargo clippy -- -D warnings
only:
- branches
- merge_requests
cache:
key: ${CI_COMMIT_REF_SLUG}-cargo
paths:
- target/
# Build Docker image for branches (test build)
build:test:
stage: build
image: docker:24-dind
services:
- docker:24-dind
before_script:
- docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_TOKEN
script:
- |
docker build \
--tag $REGISTRY/$IMAGE_NAME:$CI_COMMIT_REF_SLUG \
--tag $REGISTRY/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA \
.
- docker push $REGISTRY/$IMAGE_NAME:$CI_COMMIT_REF_SLUG
- docker push $REGISTRY/$IMAGE_NAME:$CI_COMMIT_SHORT_SHA
only:
- branches
except:
- main
- tags
# Build and push for main branch (amd64 only for speed)
# Tags with timestamp for Watchtower auto-updates
build:main:
stage: build
image: docker:24-dind
services:
- docker:24-dind
before_script:
- echo "$DOCKERHUB_TOKEN" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
- docker buildx create --driver docker-container --name builder --use || docker buildx use builder
- docker buildx inspect --bootstrap
- apk add --no-cache git grep sed
# Generate version from git describe (includes commit count since last tag)
- VERSION=$(git describe --tags --always --dirty=-modified | sed 's/^v//')
- TIMESTAMP=$(date +%Y%m%d-%H%M%S)
- echo "Building version $VERSION at $TIMESTAMP"
script:
- |
docker buildx build \
--platform linux/amd64 \
--cache-from type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache \
--cache-to type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache,mode=max \
--tag $REGISTRY/$IMAGE_NAME:latest \
--tag $REGISTRY/$IMAGE_NAME:main \
--tag $REGISTRY/$IMAGE_NAME:$VERSION \
--tag $REGISTRY/$IMAGE_NAME:main-$CI_COMMIT_SHORT_SHA \
--tag $REGISTRY/$IMAGE_NAME:main-$TIMESTAMP \
--push \
.
- echo "IMAGE_TAG=$VERSION" >> build.env
- echo "Built and pushed with tags:"
- echo " - latest (for production)"
- echo " - main (for Watchtower tracking)"
- echo " - $VERSION (git describe)"
- echo " - main-$CI_COMMIT_SHORT_SHA (commit)"
- echo " - main-$TIMESTAMP (timestamp)"
artifacts:
reports:
dotenv: build.env
only:
- main
# Release build for tags (multi-architecture with caching)
# Note: Multi-arch builds are slow (~15-20 min) due to QEMU emulation for ARM64
release:multiarch:
stage: release
image: docker:24-dind
services:
- docker:24-dind
before_script:
- echo "$DOCKERHUB_TOKEN" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
- docker buildx create --driver docker-container --name multiarch-builder --use || docker buildx use multiarch-builder
- docker buildx inspect --bootstrap
script:
- |
VERSION=${CI_COMMIT_TAG#v}
docker buildx build \
--platform linux/amd64,linux/arm64 \
--cache-from type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache \
--cache-to type=registry,ref=$REGISTRY/$IMAGE_NAME:buildcache,mode=max \
--tag $REGISTRY/$IMAGE_NAME:$VERSION \
--tag $REGISTRY/$IMAGE_NAME:$CI_COMMIT_TAG \
--tag $REGISTRY/$IMAGE_NAME:latest \
--push \
.
echo "Released version: $VERSION"
echo "IMAGE_TAG=$VERSION" >> release.env
artifacts:
reports:
dotenv: release.env
only:
- tags