Sobes.tech
Middle

GitLab CI/CD: three repositories (Front, Back, DB). A developer created a branch in the front — how to automatically deploy a feature stand with all three services after merging, and delete the stand?

sobes.tech AI

Answer from AI

To implement automatic feature-stand deployment with three services (Front, Back, DB) in GitLab CI, you can use the following approach:

  1. Monorepo or multi-project integration:

    • Configure .gitlab-ci.yml in each repository (Front, Back, DB) to trigger pipelines in other repositories.
  2. Creating dynamic environments:

    • When a branch is created in the front repository, a pipeline is triggered that:
      • Clones or builds all three services (e.g., via Docker Compose or Kubernetes).
      • Creates a unique environment (e.g., feature/branch-name).
      • Deploys all services with configuration for this environment.
  3. Removing the environment after merge:

    • Set up a GitLab rule that, upon merging the branch into the main branch (e.g., master/main), triggers a pipeline to delete the corresponding feature environment.
  4. Example of a simplified .gitlab-ci.yml for Front:

stages:
  - deploy
  - cleanup

deploy_feature:
  stage: deploy
  script:
    - echo "Deploying feature environment for $CI_COMMIT_REF_NAME"
    - docker-compose -f docker-compose.feature.yml up -d
  environment:
    name: feature/$CI_COMMIT_REF_NAME
    url: http://feature-$CI_COMMIT_REF_NAME.example.com
  only:
    - branches
  except:
    - main

cleanup_feature:
  stage: cleanup
  script:
    - echo "Cleaning up feature environment for $CI_COMMIT_REF_NAME"
    - docker-compose -f docker-compose.feature.yml down
  environment:
    name: feature/$CI_COMMIT_REF_NAME
    action: stop
  only:
    - main
  when: manual
  1. Cross-repository trigger:
    • You can use GitLab Triggers or Multi-project pipelines to trigger build and deployment of back-end and database when deploying the front-end.

Thus, when a branch is created in the front repository, the full stack is automatically deployed, and after merging, it is removed.