Sobes.tech
Middle+

How would you set up dynamic feature-stands in GitLab? The stand is deployed on each push to a branch and removed after merge or after a certain time.

sobes.tech AI

Answer from AI

To set up dynamic feature environments in GitLab, you can use GitLab CI/CD with dynamic environment creation and automatic deletion.

  1. Configure a job in .gitlab-ci.yml that runs on each push to the desired branch and deploys the environment (for example, in Kubernetes or Docker).
  2. Use the environment parameter with a dynamic name, such as based on the branch or commit name.
  3. Set auto_stop_in to automatically delete the environment after a specified time.
  4. Add a job to delete the environment after merge, using rules with when: manual or when: on_success and a merge event check.

Example configuration:

stages:
  - deploy
  - cleanup

deploy_feature:
  stage: deploy
  script:
    - ./deploy.sh $CI_COMMIT_REF_NAME
  environment:
    name: feature/$CI_COMMIT_REF_NAME
    url: https://$CI_COMMIT_REF_NAME.example.com
    auto_stop_in: 1 day
  only:
    - branches

cleanup_feature:
  stage: cleanup
  script:
    - ./cleanup.sh $CI_COMMIT_REF_NAME
  when: manual
  environment:
    name: feature/$CI_COMMIT_REF_NAME
    action: stop
  only:
    - master

Thus, each push creates a separate environment that will automatically be deleted after a day or manually after merging.