From 4a618de0ac9f0dad54e9b6da445d73924710ca56 Mon Sep 17 00:00:00 2001 From: Nicolas Vuillamy Date: Thu, 25 Feb 2021 23:21:16 +0100 Subject: [PATCH] Override git branch config (#35) * Connected App profile assistance * Reuse msTeamsWebhookUrl during sfdx:org:configure:monitoring prompts * allow to override CONFIG_BRANCH to get forced .sfdx.hardis.BRANCH.yml --- CHANGELOG.md | 11 +- Dockerfile | 1 + defaults/ci/.gitlab-ci-config.yml | 3 + defaults/ci/.gitlab-ci.yml | 200 ++++++++++++++++++ .../hardis/org/configure/monitoring.ts | 5 +- src/config/index.ts | 2 +- 6 files changed, 218 insertions(+), 4 deletions(-) create mode 100644 defaults/ci/.gitlab-ci-config.yml create mode 100644 defaults/ci/.gitlab-ci.yml diff --git a/CHANGELOG.md b/CHANGELOG.md index c73a9b0dd..e002939ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,12 @@ ## [insiders] (master) -Note: Can be used with `sfdx plugins:install sfdx-hardis@beta` and docker image +Note: Can be used with `sfdx plugins:install sfdx-hardis@beta` and docker image `hardisgroupcom/sfdx-hardis@beta` + +## [1.3.4] 2021-02-25 + +- Reuse msTeamsWebhookUrl during sfdx:org:configure:monitoring prompts +- Allow to override CONFIG_BRANCH to get forced .sfdx.hardis.BRANCH.yml ## [1.3.3] 2021-02-24 @@ -12,6 +17,10 @@ Note: Can be used with `sfdx plugins:install sfdx-hardis@beta` and docker image - Guide user to assign rights to Connected App in **sfdx:org:configure:monitoring** +## [1.3.2] 2021-02-24 + +- Guide user to assign rights to Connected App in **sfdx:org:configure:monitoring** + ## [1.3.1] 2021-02-24 - Manage git clone & push for **sfdx:org:configure:monitoring** diff --git a/Dockerfile b/Dockerfile index f29c55664..8b531d76c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,7 @@ RUN apk add --update --no-cache \ npm # Add node packages to path # +# hadolint ignore=DL3044 ENV PATH="/node_modules/.bin:${PATH}" ARG SFDX_HARDIS_VERSION=latest diff --git a/defaults/ci/.gitlab-ci-config.yml b/defaults/ci/.gitlab-ci-config.yml new file mode 100644 index 000000000..1065cc6eb --- /dev/null +++ b/defaults/ci/.gitlab-ci-config.yml @@ -0,0 +1,3 @@ +# Vous pouvez personnaliser l'éxécution de votre pipeline ici +variables: + DEPLOY_BRANCHES: /(developpement|integration|recette|preprod|master)/ # Regex contenant la liste des branches où un push va déclencher un déploiement diff --git a/defaults/ci/.gitlab-ci.yml b/defaults/ci/.gitlab-ci.yml new file mode 100644 index 000000000..4fb076e3b --- /dev/null +++ b/defaults/ci/.gitlab-ci.yml @@ -0,0 +1,200 @@ +--- +################################################################## +# Pipeline de test et déploiement d'un projet DX géré en CI/CD # +# NE PAS MODIFIER !! # +################################################################## + +# Ce pipeline est mis à jour automatiquement par sfdx-hardis +# Vous ne pouvez donc pas le modifier sous peine de voir ces modifications ultérieurement écrasées +# Cependant, vous pouvez configurer des comportements dynamiques en modifiant le fichier ".gitlab-ci-config.yml" +# .gitlab-ci-config.yml ne sera jamais écrasé par sfdx-hardis +include: + - local: ".gitlab-ci-config.yml" + +# Liste ordonnée des étapes du pipeline +stages: + - build # Controle de qualité des sources et création d'une scratch org CI + - test # Tests effectués sur la scratch org effectuée lors de l'étape précédente + - clean # Suppression de la scratch org CI + - check_deploy # Simulation de déploiement du contenu de la branche vers l'org (Sandbox ou autre) associée + - deploy # Déploiement du contenu de la branche vers l'org (Sandbox ou autre) associée + +# On execute les jobs sur l'image hardisgroupcom/sfdx-hardis qui contient les applications necessaires +# Version latest recommandée, cependant beta et alpha peuvent être utilisées pour les tests +image: hardisgroupcom/sfdx-hardis:alpha + +# Variables globales aux jobs +variables: + FORCE_COLOR: '1' + +# Décommenter pour utiliser un des 2 trucs commencés en dessous :) +#default: + +# décommenter le jour ou on a un vrai cache pour les runners gitlab concurrents +# En attendant on utilise les artifacts, même si c est pas joli joli... +# cache: +# key: toto #${CI_COMMIT_REF_SLUG}_CACHE_SFDX_HARDIS +# paths: +# - .cache/sfdx-hardis/.sfdx +# - .sfdx/ +# - config/user/ +# policy: pull + +# Décommenter pour tester une version de sfdx-hardis en cours de dev +# before_script: +# - apk add tree +# - npm install -g typescript +# - git clone --branch NOM_DE_LA_BRANCHE_GIT https://github.com/hardisgroupcom/sfdx-hardis.git +# - cd sfdx-hardis +# - yarn +# - tsc +# - sfdx plugins:link +# - cd .. + +# Controle de la qualité des sources avec Mega-Linter, qui contient sfdx-scanner (apex PMD), +# mais aussi d'autres linters et détecteurs de mauvaises pratiques +check_quality: + stage: build + tags: + - ubuntu + only: + - pushes + - merge_requests + - web + image: nvuillam/mega-linter-salesforce:latest + script: [ "true" ] + variables: + # All available variables are described in documentation + # https://nvuillam.github.io/mega-linter/configuration/ + DEFAULT_WORKSPACE: $CI_PROJECT_DIR + DEFAULT_BRANCH: master + # ADD YOUR CUSTOM ENV VARIABLES HERE TO OVERRIDE VALUES OF .mega-linter.yml AT THE ROOT OF YOUR REPOSITORY + artifacts: + when: always + paths: + - report + expire_in: 1 week + +# Check du déploiement dans l'org cible de la merge request +check_deploy_to_target_branch_org: + stage: build + tags: + - ubuntu + only: + refs: + - merge_requests + - web + variables: + CONFIG_BRANCH: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME + ORG_ALIAS: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME + script: + - '[ -z "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ] && exit 0;' # Skip this job if it is launched from web UI and we are not in merge request context + - sfdx hardis:auth:login + - sfdx hardis:project:deploy:sources:dx --check + +# Création d'une scratch org, pour: +# - vérifier que les métadatas se déploient bien +# - exécuter les classes de test Apex +# - executer les tests automatisés +create_scratch_org: + stage: build + tags: + - ubuntu + only: + - merge_requests + - web + script: + - sfdx hardis:auth:login --devhub + - sfdx hardis:scratch:create + artifacts: + when: always + expire_in: 60 minutes + paths: + - .cache/sfdx-hardis/.sfdx + - .sfdx + - config/user + +# Execution des classes de test Apex dans la scratch org créée par le job +test_apex: + stage: test + tags: + - ubuntu + only: + - merge_requests + - web + needs: + - job: create_scratch_org + artifacts: true + script: + - sfdx hardis:auth:login --scratchorg || true + - sfdx hardis:org:test:apex + artifacts: + when: always + paths: + - hardis-report + expire_in: 1 week + +# Execution des tests automatisés +#test_automation: +# stage: test +# tags: +# - ubuntu +# only: +# - merge_requests +# - web +# needs: +# - job: create_scratch_org +# artifacts: true +# script: +# - sfdx hardis:auth:login --scratchorg +# - echo "Automated tests not implemented yet" + +# Suppression de la scratch org de test temporaire +clean: + stage: clean + tags: + - ubuntu + when: on_success + only: + - merge_requests + - web + needs: + - job: create_scratch_org + - job: test_apex + script: + - sfdx hardis:auth:login --devhub + - sfdx hardis:auth:login --scratchorg || true + - sfdx force:org:delete --noprompt || true + +# Simulation de déploiement vers l'org Salesforce cible +# Se déclenche en cas de planification ou lancement manuel +check_deploy_to_current_branch_org: + stage: deploy + tags: + - ubuntu + only: + - pushes + - schedules + - web + except: + variables: + - $CI_COMMIT_REF_NAME !~ $DEPLOY_BRANCHES + script: + - sfdx hardis:auth:login + - sfdx hardis:project:deploy:sources:dx --check + +# Déploiement vers l'org Salesforce cible +# Se déclenche en cas de push vers une des branches décrites dans la regex DEPLOY_BRANCHES de .gitlab-ci-config.yml +deploy_to_org: + stage: deploy + tags: + - ubuntu + only: + - pushes + - web + except: + variables: + - $CI_COMMIT_REF_NAME !~ $DEPLOY_BRANCHES + script: + - sfdx hardis:auth:login + - sfdx hardis:project:deploy:sources:dx diff --git a/src/commands/hardis/org/configure/monitoring.ts b/src/commands/hardis/org/configure/monitoring.ts index 243cd73f7..6380e8553 100644 --- a/src/commands/hardis/org/configure/monitoring.ts +++ b/src/commands/hardis/org/configure/monitoring.ts @@ -100,6 +100,7 @@ export default class OrgConfigureMonitoring extends SfdxCommand { { type: 'text', name: 'teamsHook', + initial: config.msTeamsWebhookUrl, message: c.cyanBright('If you want notifications of updates in orgs in a Microsoft Teams channel:\n- Create the WebHook: https://docs.microsoft.com/fr-fr/microsoftteams/platform/webhooks-and-connectors/how-to/add-incoming-webhook#add-an-incoming-webhook-to-a-teams-channel\n- paste the hook Url here\nIf you do not want Ms Team notifications, just leave empty and hit ENTER') } ]); @@ -114,7 +115,7 @@ export default class OrgConfigureMonitoring extends SfdxCommand { // Generate SSL certificate (requires openssl to be installed on computer) await generateSSLCertificate(branchName, './.ssh', this); - uxLog(this, 'You can customize monitoring updating .gitlab-ci-config.yml'); + uxLog(this, c.italic('You can customize monitoring by updating .gitlab-ci-config.yml')); // Confirm & push on server const confirmPush = await prompts({ @@ -130,7 +131,7 @@ export default class OrgConfigureMonitoring extends SfdxCommand { } else { uxLog(this, c.yellow('Please manually git add, commit and push to the remote repository :)')); } - uxLog(this, c.greenBright('You may schedule monitoring to be automatically run every day. To do that, go in Project -> CI -> Schedules -> New schedule')); + uxLog(this, c.greenBright(`You may schedule monitoring to be automatically run every day. To do that, go in ${c.bold('Project -> CI -> Schedules -> New schedule')}`)); // Return an object to be displayed with --json return { outputString: 'Configured branch for authentication' }; } diff --git a/src/config/index.ts b/src/config/index.ts index a5129b885..75eab8384 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -37,7 +37,7 @@ async function getBranchConfigFiles() { if (!isGitRepo()) { return []; } - const gitBranchFormatted = await getCurrentGitBranch({ formatted: true }); + const gitBranchFormatted = process.env.CONFIG_BRANCH || (await getCurrentGitBranch({ formatted: true })); const branchConfigFiles = [ `config/branches/.${moduleName}.${gitBranchFormatted}.yaml`, `config/branches/.${moduleName}.${gitBranchFormatted}.yml`