Prerequisites
Before configuring GitLab CI/CD on Debian, ensure you have:
Step 1: Install GitLab Runner (Self-Hosted Executor)
GitLab Runner is required to execute CI/CD jobs. On Debian, use the official repository for installation:
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt install gitlab-runner
sudo gitlab-runner register \
--url https://gitlab.com/ \ # Use your GitLab instance URL (e.g., https://gitlab.com/ or your self-hosted domain)
--registration-token YOUR_REGISTRATION_TOKEN \
--executor shell \ # Choose an executor (shell for local execution, docker for containerized jobs)
--description "Debian Runner" \
--tag-list "ci,linux" # Optional: Tags to filter jobs
sudo gitlab-runner status
The runner should show as “running”.Step 2: Create .gitlab-ci.yml in Your Project Root
This file defines your CI/CD pipeline’s structure (stages, jobs, and steps). Below is a basic template for a Debian-based project (e.g., a Python or Java app):
# Define pipeline stages (executed in order)
stages:
- build
- test
- deploy
# Variables (reusable across jobs)
variables:
PROJECT_NAME: "my-debian-app"
DOCKER_IMAGE: "my-registry/${PROJECT_NAME}:${CI_COMMIT_SHORT_SHA}" # Dynamic image tag (commit SHA)
# Build Job (compiles/ packages code)
build_job:
stage: build
script:
- echo "Building ${PROJECT_NAME}..."
- ./build_script.sh # Replace with your build command (e.g., mvn package, gradle build, or make)
artifacts: # Share build outputs with subsequent jobs
paths:
- target/*.jar # Example: Java JAR file
- dist/ # Example: Frontend build artifacts
expire_in: 1 hour # Auto-delete after 1 hour
# Test Job (runs automated tests)
test_job:
stage: test
script:
- echo "Running tests..."
- ./run_tests.sh # Replace with your test command (e.g., mvn test, pytest, or npm test)
needs: ["build_job"] # Only run after build_job succeeds
# Deploy Job (deploys to production)
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
- scp target/*.jar user@production-server:/opt/apps/ # Example: Copy JAR to remote server
- ssh user@production-server "systemctl restart my-app.service" # Restart service (if applicable)
only: ["main"] # Trigger only on pushes/merges to 'main' branch
needs: ["test_job"] # Only run after test_job succeeds
Key Notes:
build) must succeed before later stages (e.g., deploy) run.artifacts to pass files (e.g., build outputs) between jobs.tags in jobs to restrict execution to specific runners (e.g., tags: ["debian", "docker"]).only/except to control when jobs run (e.g., only: ["main"] for production deploys).Step 3: Configure SSH for Remote Deployment (Optional but Common)
If your deploy job interacts with a remote server (e.g., via scp/ssh), you need to securely store SSH credentials in GitLab CI/CD variables:
ssh-keygen -t rsa -b 4096 -C "gitlab-ci@debian-runner"
id_rsa/id_rsa.pub) in ~/.ssh/.id_rsa.pub and append it to ~/.ssh/authorized_keys on the production server.SSH_PRIVATE_KEYid_rsa (private key).File (optional but recommended for security)..gitlab-ci.yml for SSH Access:before_script of your deploy job (or a dedicated setup_ssh job):before_script:
- 'which ssh-agent || ( apt-get update -y && apt-get install -y openssh-client )' # Install ssh-agent if missing
- eval $(ssh-agent -s) # Start ssh-agent
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null # Add private key
- mkdir -p ~/.ssh # Create .ssh directory
- chmod 700 ~/.ssh # Set correct permissions
- ssh-keyscan production-server >> ~/.ssh/known_hosts # Trust the server
- chmod 644 ~/.ssh/known_hosts # Set correct permissions
Replace production-server with your server’s hostname/IP.Step 4: Trigger the CI/CD Pipeline
Once configured, your pipeline will trigger automatically when you:
git push origin main).To manually trigger (optional):
Monitor Pipeline Execution:
Step 5: Optimize and Secure Your Pipeline
.m2 repo, Node.js node_modules). Example:cache:
paths:
- .m2/repository/ # Maven cache
- node_modules/ # Node.js cache
key: ${CI_COMMIT_REF_SLUG} # Unique key per branch
docker and specify an image:build_job:
stage: build
image: maven:3.9-openjdk-17 # Use a Maven image from Docker Hub
script:
- mvn package
/etc/gitlab-runner/config.toml (for self-hosted runners).By following these steps, you’ll have a functional GitLab CI/CD pipeline on Debian that automates building, testing, and deploying your code. Adjust the .gitlab-ci.yml file to match your project’s language, framework, and deployment requirements.