60 lines
2 KiB
YAML
60 lines
2 KiB
YAML
stages:
|
|
- build
|
|
- deploy
|
|
|
|
variables:
|
|
DOCKER_BUILDKIT: 1
|
|
CI_BUILDX_ARCHS: "linux/amd64"
|
|
|
|
build:
|
|
stage: build
|
|
image: docker:24
|
|
services:
|
|
- docker:24-dind
|
|
before_script:
|
|
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
|
|
script:
|
|
# Pull latest image for layer caching (ignore failures if not exists)
|
|
- docker pull $CI_REGISTRY_IMAGE:latest || true
|
|
# Build with cache-from for faster builds and inline cache for future builds
|
|
- docker build
|
|
--cache-from $CI_REGISTRY_IMAGE:latest
|
|
--build-arg BUILDKIT_INLINE_CACHE=1
|
|
-f k8s/Dockerfile
|
|
-t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
|
|
-t $CI_REGISTRY_IMAGE:latest
|
|
.
|
|
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
|
|
- docker push $CI_REGISTRY_IMAGE:latest
|
|
rules:
|
|
- if: $CI_COMMIT_BRANCH == "main"
|
|
|
|
deploy:
|
|
stage: deploy
|
|
needs: ["build"]
|
|
image:
|
|
name: bitnami/kubectl:latest
|
|
entrypoint: ['']
|
|
script:
|
|
- kubectl config get-contexts
|
|
- kubectl config use-context graham/towerops:towerops
|
|
# Run database migrations before deploying new version
|
|
- |
|
|
# Create unique job name using commit SHA (truncated to fit k8s naming limits)
|
|
JOB_NAME="towerops-migrate-${CI_COMMIT_SHORT_SHA}"
|
|
# Generate job manifest with correct image tag and unique name
|
|
sed -e "s|IMAGE_TAG|${CI_COMMIT_SHA}|g" \
|
|
-e "s|towerops-migrate-TIMESTAMP|${JOB_NAME}|g" \
|
|
k8s/migrate-job.yaml | kubectl apply -f -
|
|
# Wait for migration to complete (max 5 minutes)
|
|
kubectl wait --for=condition=complete --timeout=5m job/${JOB_NAME} -n towerops
|
|
echo "Database migration completed successfully"
|
|
# Deploy new version
|
|
- kubectl set image deployment/towerops towerops=$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA -n towerops
|
|
- kubectl rollout status deployment/towerops -n towerops --timeout=5m
|
|
environment:
|
|
name: production
|
|
kubernetes:
|
|
namespace: towerops
|
|
rules:
|
|
- if: $CI_COMMIT_BRANCH == "main"
|