Skip to content

Instantly share code, notes, and snippets.

@unclebean
Created October 10, 2024 09:21
Show Gist options
  • Save unclebean/cb7bc17c61a415b437fb684f44c51058 to your computer and use it in GitHub Desktop.
Save unclebean/cb7bc17c61a415b437fb684f44c51058 to your computer and use it in GitHub Desktop.
ci/cd
stages:
- build
- deploy
# Variables for your project
variables:
IMAGE_NAME: "registry.example.com/your-project/your-image" # Docker image path
DOCKER_TAG: "${CI_COMMIT_SHA:0:8}" # Tag Docker image with commit SHA
# Build and push Docker image
build:
stage: build
image: docker:latest
services:
- docker:dind
before_script:
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
script:
- echo "Building Docker image with commit SHA..."
- docker build -t $IMAGE_NAME:$DOCKER_TAG .
- docker push $IMAGE_NAME:$DOCKER_TAG
only:
- main # This job will only run on the main branch
# Deploy Docker image to Kubernetes
deploy:
stage: deploy
image: lachlanevenson/k8s-kubectl:latest # Use kubectl for Kubernetes deployment
script:
- echo "Deploying to Kubernetes with commit SHA image..."
- kubectl set image deployment/my-app my-app=$IMAGE_NAME:$CI_COMMIT_SHA --record
only:
- main # This job will only run on the main branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment