Skip to content

GitLab CI Examples

Pipeline configurations for GitLab CI/CD.

Basic Pipeline

yaml
# .gitlab-ci.yml
stages:
  - build
  - test
  - deploy

variables:
  DOCKER_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA

build:
  stage: build
  image: docker:24
  services:
    - docker:24-dind
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build -t $DOCKER_IMAGE .
    - docker push $DOCKER_IMAGE
  only:
    - main
    - merge_requests

test:
  stage: test
  image: $DOCKER_IMAGE
  script:
    - npm test
  only:
    - main
    - merge_requests

deploy:
  stage: deploy
  image: curlimages/curl:latest
  script:
    - |
      curl -X POST "$OPSATLAS_URL/api/v1/deployments" \
        -H "Authorization: Bearer $OPSATLAS_TOKEN" \
        -H "Content-Type: application/json" \
        -d "{
          \"service\": \"$CI_PROJECT_NAME\",
          \"version\": \"$CI_COMMIT_SHA\",
          \"strategy\": \"rolling\"
        }"
  only:
    - main
  environment:
    name: production
    url: https://app.example.com

Multi-Environment

yaml
stages:
  - build
  - deploy:staging
  - deploy:production

.deploy_template: &deploy
  image: curlimages/curl:latest
  script:
    - |
      curl -X POST "$OPSATLAS_URL/api/v1/deployments" \
        -H "Authorization: Bearer $OPSATLAS_TOKEN" \
        -H "Content-Type: application/json" \
        -d "{
          \"service\": \"$CI_PROJECT_NAME\",
          \"version\": \"$CI_COMMIT_SHA\",
          \"environment\": \"$ENVIRONMENT\"
        }"

deploy:staging:
  <<: *deploy
  stage: deploy:staging
  variables:
    ENVIRONMENT: staging
  only:
    - main
  environment:
    name: staging
    url: https://staging.example.com

deploy:production:
  <<: *deploy
  stage: deploy:production
  variables:
    ENVIRONMENT: production
  only:
    - main
  when: manual
  environment:
    name: production
    url: https://app.example.com

Variables

Add these CI/CD variables in GitLab:

VariableProtectedMasked
OPSATLAS_URLYesNo
OPSATLAS_TOKENYesYes

Released under the MIT License.