Automatiseer GitLab CI-pijplijn met dynamische omgevingsconfiguraties
description
Met deze prompt kan de gebruiker een geavanceerde GitLab CI-pijplijn creëren die automatisch de juiste omgevingsinstellingen toepast op basis van de branch of deployment, wat handmatige fouten vermindert en de deployment workflow versnelt. Dit is effectiever dan statische configuraties die apart moeten worden beheerd en aangepast.
prompt
Help me een GitLab CI-pijplijn te ontwerpen die dynamisch omgevingsconfiguraties laadt en toepast afhankelijk van de branch of het doel van de deployment. Mijn huidige configuratie: <voer je huidige GitLab CI YAML-configuratie in>. Branch ...
try_prompt
generate_helper
disclaimerOnPageApi
tags
ai_answers
provider: openai
model: gpt-4.1-nano
Hier is een voorbeeld van een GitLab CI-configuratie die dynamisch omgevingsconfiguraties laadt en toepast afhankelijk van de branch. We gebruiken variabelen en conditionele regels om verschillende omgevingsinstellingen te activeren voor development, staging en productie.
```yaml
stages:
- build
- test
- deploy
variables:
# Standaardinstellingen
LOG_LEVEL: "minimaal"
DEBUG: "false"
build_job:
stage: build
script:
- echo "Building project..."
- echo "Branch: $CI_COMMIT_BRANCH"
- echo "Log level: $LOG_LEVEL"
- echo "Debug mode: $DEBUG"
rules:
- if: '$CI_COMMIT_BRANCH == "development"'
variables:
LOG_LEVEL: "debug"
DEBUG: "true"
- if: '$CI_COMMIT_BRANCH == "staging"'
variables:
LOG_LEVEL: "debug"
DEBUG: "true"
- if: '$CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "master"'
variables:
LOG_LEVEL: "minimaal"
DEBUG: "false"
```
**Uitleg:**
- De variabelen `LOG_LEVEL` en `DEBUG` worden standaard ingesteld, maar kunnen worden overschreven afhankelijk van de branch.
- Voor de `development` en `staging` branches worden debug logging en debug mode ingeschakeld.
- Voor de productiebranch (`main` of `master`) wordt minimale logging en uitgeschakeld debug mode toegepast.
- Je kunt deze regels verder uitbreiden naar behoefte, bijvoorbeeld door verschillende scripts of configuraties te laden afhankelijk van de branch.
Wil je dat deze configuratie ook automatisch andere omgevingsvariabelen laadt of specifieke configuratiebestanden kopieert, dan kunnen we dat nog verder uitbreiden.
provider: deepseek
model: deepseek-chat
Hier is een verbeterde GitLab CI-pijplijnconfiguratie die dynamisch omgevingsconfiguraties laadt op basis van branches:
```yaml
stages:
- build
- test
- deploy
# Omgevingsvariabelen en configuraties per branch
.variables_dev: &variables_dev
LOG_LEVEL: "debug"
ENVIRONMENT: "development"
DEPLOY_URL: "https://dev.example.com"
.variables_staging: &variables_staging
LOG_LEVEL: "debug"
ENVIRONMENT: "staging"
DEPLOY_URL: "https://staging.example.com"
.variables_prod: &variables_prod
LOG_LEVEL: "warning"
ENVIRONMENT: "production"
DEPLOY_URL: "https://example.com"
# Job templates
.build_template: &build_template
stage: build
script:
- echo "Building project voor $ENVIRONMENT..."
- echo "Log level: $LOG_LEVEL"
- echo "Deploy URL: $DEPLOY_URL"
- echo "Branch: $CI_COMMIT_REF_NAME"
- # Voeg hier je build commando's toe
.test_template: &test_template
stage: test
script:
- echo "Running tests voor $ENVIRONMENT..."
- echo "Test configuratie met log level: $LOG_LEVEL"
- # Voeg hier je test commando's toe
.deploy_template: &deploy_template
stage: deploy
script:
- echo "Deployen naar $ENVIRONMENT..."
- echo "Gebruikende configuratie:"
- echo " - Log level: $LOG_LEVEL"
- echo " - URL: $DEPLOY_URL"
- # Voeg hier je deploy commando's toe
# Development omgeving (develop branch)
build_dev:
<<: *build_template
variables:
<<: *variables_dev
only:
- develop
test_dev:
<<: *test_template
variables:
<<: *variables_dev
only:
- develop
deploy_dev:
<<: *deploy_template
variables:
<<: *variables_dev
environment:
name: development
url: https://dev.example.com
only:
- develop
# Staging omgeving (staging branch)
build_staging:
<<: *build_template
variables:
<<: *variables_staging
only:
- staging
test_staging:
<<: *test_template
variables:
<<: *variables_staging
only:
- staging
deploy_staging:
<<: *deploy_template
variables:
<<: *variables_staging
environment:
name: staging
url: https://staging.example.com
only:
- staging
# Productie omgeving (main branch)
build_prod:
<<: *build_template
variables:
<<: *variables_prod
only:
- main
test_prod:
<<: *test_template
variables:
<<: *variables_prod
only:
- main
deploy_prod:
<<: *deploy_template
variables:
<<: *variables_prod
environment:
name: production
url: https://example.com
only:
- main
when: manual # Handmatige goedkeuring voor productie
```
**Alternatieve geavanceerde aanpak met dynamische configuratie:**
```yaml
stages:
- build
- test
- deploy
# Dynamische configuratie loader
.setup_environment: &setup_environment |
case $CI_COMMIT_REF_NAME in
"develop")
export ENVIRONMENT="development"
export LOG_LEVEL="debug"
export DEPLOY_URL="https://dev.example.com"
;;
"staging")
export ENVIRONMENT="staging"
export LOG_LEVEL="debug"
export DEPLOY_URL="https://staging.example.com"
;;
"main")
export ENVIRONMENT="production"
export LOG_LEVEL="warning"
export DEPLOY_URL="https://example.com"
;;
*)
echo "Onbekende branch, gebruik default configuratie"
export ENVIRONMENT="unknown"
export LOG_LEVEL="info"
;;
esac
echo "Omgevingsconfiguratie geladen:"
echo "Environment: $ENVIRONMENT"
echo "Log Level: $LOG_LEVEL"
echo "Deploy URL: $DEPLOY_URL"
# Universele jobs die automatisch configureren
build_job:
stage: build
before_script:
- *setup_environment
script:
- echo "Building project voor $ENVIRONMENT..."
- echo "Configuratie: Log level = $LOG_LEVEL"
- # Build commando's hier
rules:
- if: $CI_COMMIT_BRANCH == "develop" || $CI_COMMIT_BRANCH == "staging" || $CI_COMMIT_BRANCH == "main"
test_job:
stage: test
before_script:
- *setup_environment
script:
- echo "Running tests voor $ENVIRONMENT..."
- echo "Testen met log level: $LOG_LEVEL"
- # Test commando's hier
rules:
- if: $CI_COMMIT_BRANCH == "develop" || $CI_COMMIT_BRANCH == "staging" || $CI_COMMIT_BRANCH == "main"
deploy_job:
stage: deploy
before_script:
- *setup_environment
script:
- echo "Deployen naar $ENVIRONMENT..."
- echo "Gebruikende configuratie:"
- echo " - Log level: $LOG_LEVEL"
- echo " - URL: $DEPLOY_URL"
- # Deploy commando's hier
environment:
name: $ENVIRONMENT
url: $DEPLOY_URL
rules:
- if: $CI_COMMIT_BRANCH == "develop"
- if: $CI_COMMIT_BRANCH == "staging"
- if: $CI_COMMIT_BRANCH == "main"
when: manual # Handmatige deploy voor productie
```
**Belangrijkste kenmerken:**
1. **Dynamische configuratie**: Variabelen worden automatisch ingesteld op basis van de branch
2. **Log level configuratie**: Debug logging voor development/staging, minimale logging voor productie
3. **Omgevingsspecifieke URLs**: Elke omgeving heeft zijn eigen deploy URL
4. **Veiligheid**: Productie deployment vereist handmatige goedkeuring
5. **Schaalbaar**: Eenvoudig nieuwe branches/omgevingen toevoegen
Kies de eerste aanpak voor expliciete controle per branch, of de tweede voor een meer dynamische aanpak die automatisch configureert op basis van de branch naam.