|
| 1 | +node { |
| 2 | + properties([ |
| 3 | + [$class: 'BuildDiscarderProperty', |
| 4 | + strategy: [$class: 'LogRotator', |
| 5 | + artifactDaysToKeepStr: '', |
| 6 | + artifactNumToKeepStr: '', |
| 7 | + daysToKeepStr: '', |
| 8 | + numToKeepStr: '5'] |
| 9 | + ], |
| 10 | + pipelineTriggers([ |
| 11 | + [$class: 'GenericTrigger', |
| 12 | + genericVariables: [ |
| 13 | + [expressionType: 'JSONPath', key: 'repository', value: '$.repository'], |
| 14 | + [expressionType: 'JSONPath', key: 'organization', value: '$.organization'], |
| 15 | + [expressionType: 'JSONPath', key: 'sender', value: '$.sender'], |
| 16 | + [expressionType: 'JSONPath', key: 'ref_type', value: '$.ref_type'], |
| 17 | + [expressionType: 'JSONPath', key: 'master_branch', value: '$.master_branch'], |
| 18 | + [expressionType: 'JSONPath', key: 'branch_name', value: 'ref'] |
| 19 | + ], |
| 20 | + regexpFilterText: '', |
| 21 | + regexpFilterExpression: '' |
| 22 | + ] |
| 23 | + ]) |
| 24 | + ]) |
| 25 | + // Define the payload to send to GitHub. |
| 26 | + // This is JSON |
| 27 | + def githubPayload = """{ |
| 28 | + "required_status_checks": { |
| 29 | + "strict": true, |
| 30 | + "contexts": [ |
| 31 | + "continuous-integration/jenkins/branch" |
| 32 | + ] |
| 33 | + }, |
| 34 | + "enforce_admins": true, |
| 35 | + "required_pull_request_reviews": { |
| 36 | + "dismissal_restrictions": { |
| 37 | + "users": [ |
| 38 | + "hollywood", |
| 39 | + "primetheus" |
| 40 | + ], |
| 41 | + "teams": [ |
| 42 | + "test-team" |
| 43 | + ] |
| 44 | + }, |
| 45 | + "dismiss_stale_reviews": true, |
| 46 | + "require_code_owner_reviews": true |
| 47 | + }, |
| 48 | + "restrictions": { |
| 49 | + "users": [ |
| 50 | + "hollywood", |
| 51 | + "primetheus" |
| 52 | + ], |
| 53 | + "teams": [ |
| 54 | + "test-team" |
| 55 | + ] |
| 56 | + } |
| 57 | + }""" |
| 58 | + |
| 59 | + stage("Protect Master Branch") { |
| 60 | + if(env.branch_name && "${branch_name}" == "${master_branch}") { |
| 61 | + // The credentialsId should match yours, not this demonstration |
| 62 | + withCredentials([string(credentialsId: '1cf07897-ad01-4e59-9075-617ea40cf111', variable: 'githubToken')]) { |
| 63 | + httpRequest( |
| 64 | + contentType: 'APPLICATION_JSON', |
| 65 | + consoleLogResponseBody: true, |
| 66 | + customHeaders: [ |
| 67 | + [maskValue: true, name: 'Authorization', value: "token ${githubToken}"], |
| 68 | + [name: 'Accept', value: 'application/vnd.github.loki-preview']], |
| 69 | + httpMode: 'PUT', |
| 70 | + ignoreSslErrors: true, |
| 71 | + requestBody: githubPayload, |
| 72 | + responseHandle: 'NONE', |
| 73 | + url: "${repository_url}/branches/${repository_default_branch}/protection") |
| 74 | + } |
| 75 | + } else { |
| 76 | + sh(name: "Skip", script: 'echo "Move along, nothing to see here"') |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments