Write me a complete gitlab.ci example
- User
Write me a complete gitlab.ci example
- Assistant
Write me a complete gitlab.ci example
Certainly! Below is a complete example of a .gitlab-ci.yml file for GitLab CI/CD. This example demonstrates how to set up a basic pipeline with stages such as build, test, and deploy. It includes various features like artifacts, caching, environment variables, and conditional job execution.
# .gitlab-ci.yml
# Define the order of stages that jobs will run in
stages:
- build
- test
- deploy
# Global variables accessible to all jobs
variables:
# Customize the default Docker image
DOCKER_DRIVER: overlay2
APP_NAME: my-awesome-app
APP_ENV: production
# Define default image for all jobs
default:
image: alpine:latest
# Job: Build the application
build_job:
stage: build
image: node:14-alpine # Use an appropriate Docker image
script:
- echo "Building the application..."
- npm install
- npm run build
artifacts:
paths:
- dist/
expire_in: 1 week
cache:
paths:
- node_modules/
only:
- branches # Run on all branches except those listed in 'except'
# Job: Run unit tests
test_job:
stage: test
image: node:14-alpine
script:
- echo "Running tests..."
- npm install
- npm test
coverage: /All files[^|]*\|[^|]*\s+([\d\.]+)%/
dependencies:
- build_job
cache:
paths:
- node_modules/
only:
- branches
# Job: Deploy to production
deploy_job:
stage: deploy
image: alpine:latest
script:
- echo "Deploying to production..."
- ./deploy.sh # Custom deployment script
environment:
name: production
url: https://www.example.com
when: manual # Allows manual triggering
only:
- main # Only deploy from the 'main' branch
# Job: Notify team
notify_job:
stage: deploy
image: alpine:latest
script:
- echo "Notifying the team..."
- ./notify.sh # Custom notification script
when: on_success
needs:
- deploy_job
only:
- main
# Include external templates (optional)
include:
- template: Jobs/Build.gitlab-ci.yml
npm install and npm run build).dist/ directory, which contains the build output.node_modules/ to speed up subsequent jobs.npm test.build_job and can use its artifacts.node_modules/.deploy.sh).manual, so it requires manual triggering via the GitLab UI.main branch.notify.sh) to inform the team after deployment.on_success of deploy_job.deploy_job and should wait for it to complete.main branch..gitlab-ci.yml templates, which is useful for reusing common configurations.Artifacts are files generated by a job and uploaded to GitLab. In the build_job, the dist/ directory is stored as an artifact, which can be downloaded or passed to other jobs.
Caching directories like node_modules/ can significantly speed up jobs by not requiring dependencies to be re-downloaded every time.
The environment keyword in deploy_job allows GitLab to track deployments. You can view active environments and deployments under the Environments section in GitLab.
on_success, on_failure, manual, or always).The needs keyword defines job dependencies, allowing jobs to run concurrently when possible but ensuring they wait for necessary upstream jobs.
node:14-alpine and alpine:latest with images suitable for your project (e.g., Python, Java, Go).script section of each job with commands specific to building, testing, and deploying your application.variables or use GitLab's CI/CD Variables in the project settings for sensitive data.when: manual for deployment jobs to have control over when code is deployed to production.If your project builds Docker images, you might need to use Docker-in-Docker (DinD).
docker_build_job:
stage: build
image: docker:20.10.16
services:
- docker:20.10.16-dind
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_TLS_CERTDIR: ""
script:
- echo "Building Docker image..."
- docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
- docker build -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA" .
- docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
only:
- main
Feel free to adjust this example to suit your project's specific needs. The .gitlab-ci.yml file is highly flexible, allowing you to create complex pipelines that fit your development workflow.
If you have any questions or need further assistance customizing your GitLab CI/CD pipeline, please let me know!
Not yet included in a published release.