|
| 1 | +// Jenkinsfile for gh-pr-and-build template |
| 2 | +// See: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/ |
| 3 | + |
| 4 | +def secrets = [ |
| 5 | + // params.VAULT_PATH_SVC_ACCOUNT_EPHEMERAL |
| 6 | + [path: 'insights-cicd/ephemeral-bot-svc-account', engineVersion: 1, secretValues: [ |
| 7 | + [envVar: 'OC_LOGIN_TOKEN_DEV', vaultKey: 'oc-login-token-dev'], |
| 8 | + [envVar: 'OC_LOGIN_SERVER_DEV', vaultKey: 'oc-login-server-dev']]], |
| 9 | + // params.VAULT_PATH_QUAY_PUSH |
| 10 | + [path: 'app-sre/quay/app-sre-push', engineVersion: 1, secretValues: [ |
| 11 | + [envVar: 'QUAY_USER', vaultKey: 'user'], |
| 12 | + [envVar: 'QUAY_TOKEN', vaultKey: 'token']]], |
| 13 | + // params.VAULT_PATH_RHR_PULL |
| 14 | + [path: 'insights-cicd/rh-registry-pull', engineVersion: 1, secretValues: [ |
| 15 | + [envVar: 'RH_REGISTRY_USER', vaultKey: 'user'], |
| 16 | + [envVar: 'RH_REGISTRY_TOKEN', vaultKey: 'token']]] |
| 17 | +] |
| 18 | + |
| 19 | +// params.VAULT_ADDRESS, params.VAULT_CREDS_ID |
| 20 | +def configuration = [vaultUrl: "https://vault.devshift.net", vaultCredentialId: 'vault-creds', engineVersion: 1] |
| 21 | + |
| 22 | +pipeline { |
| 23 | + // Agent configuration - defines where the pipeline runs |
| 24 | + agent { |
| 25 | + node { |
| 26 | + // Use spot instances for cost efficiency |
| 27 | + label 'rhel8-spot' |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + // Pipeline options |
| 32 | + options { |
| 33 | + // Add timestamps to console output |
| 34 | + timestamps() |
| 35 | + } |
| 36 | + |
| 37 | + stages { |
| 38 | + // Stage 1: PR Check - runs for pull requests only |
| 39 | + stage('PR Check') { |
| 40 | + when { |
| 41 | + // Only execute when building a pull request |
| 42 | + // Environment variables available: CHANGE_ID, CHANGE_AUTHOR, CHANGE_TARGET, etc. |
| 43 | + changeRequest() |
| 44 | + } |
| 45 | + steps { |
| 46 | + wrap([$class: 'VaultBuildWrapper', |
| 47 | + vaultSecrets: [ |
| 48 | + [ |
| 49 | + configuration: configuration, |
| 50 | + secretValues: secrets |
| 51 | + ] |
| 52 | + ] |
| 53 | + ]) { |
| 54 | + // Run PR validation script |
| 55 | + sh './pr_check.sh' |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Stage 2: Build - runs for main branch only |
| 61 | + stage('Build') { |
| 62 | + when { |
| 63 | + // Only execute when building the main branch |
| 64 | + branch 'main' |
| 65 | + } |
| 66 | + steps { |
| 67 | + // VaultBuildWrapper injects secrets as environment variables |
| 68 | + // Secrets are ONLY available in this stage, not in PR Check for security |
| 69 | + wrap([$class: 'VaultBuildWrapper', |
| 70 | + vaultSecrets: [ |
| 71 | + [ |
| 72 | + configuration: configuration, |
| 73 | + secretValues: secrets |
| 74 | + ] |
| 75 | + ] |
| 76 | + ]) { |
| 77 | + // Run build/deploy script with access to secrets |
| 78 | + sh './build_deploy.sh' |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Post-build actions |
| 85 | + post { |
| 86 | + always { |
| 87 | + // Clean workspace after every build to save disk space |
| 88 | + cleanWs() |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments