forked from snowflakedb/snowflake-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
152 lines (143 loc) · 5.9 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import groovy.json.JsonOutput
pipeline {
// TODO Please migrate this to C7 as sfc-dev2 servers do not support c6 nodes
agent { label 'regular-memory-node' }
options { timestamps() }
environment {
COMMIT_SHA_LONG = sh(returnStdout: true, script: "echo \$(git rev-parse " + "HEAD)").trim()
SEMGREP_DEPLOYMENT_ID = 1
INPUT_PUBLISHURL = "https://semgrep.snowflake.com"
// environment variables for semgrep_agent (for findings / analytics page)
// remove .git at the end
SEMGREP_REPO_URL = env.GIT_URL.replaceFirst(/^(.*).git$/,'$1')
SEMGREP_BRANCH = "${CHANGE_BRANCH}"
SEMGREP_JOB_URL = "${BUILD_URL}"
// remove SCM URL + .git at the end
SEMGREP_REPO_NAME = env.GIT_URL.replaceFirst(/^https:\/\/github.com\/(.*).git$/, '$1')
SEMGREP_COMMIT = "${GIT_COMMIT}"
SEMGREP_PR_ID = "${env.CHANGE_ID}"
BASELINE_BRANCH = "${env.CHANGE_TARGET}"
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Semgrep_agent') {
agent {
docker {
label 'parallelizable-c7'
image 'nexus.int.snowflakecomputing.com:8087/returntocorp/semgrep-agent:v1'
args '-u root'
}
}
when {
expression { env.CHANGE_ID && env.BRANCH_NAME.startsWith("PR-") }
}
steps{
wrap([$class: 'MaskPasswordsBuildWrapper']) {
withCredentials([
[$class: 'UsernamePasswordMultiBinding', credentialsId:
'b4f59663-ae0a-4384-9fdc-c7f2fe1c4fca', usernameVariable:
'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD'],
string(credentialsId:'SEMGREP_APP_TOKEN', variable: 'SEMGREP_APP_TOKEN'),
]) {
script {
try {
sh 'export SEMGREP_DIR=semgrep-scan-$(pwd | rev | cut -d \'/\' -f1 | rev) && mkdir -p ../$SEMGREP_DIR && cp -R . ../$SEMGREP_DIR && cd ../$SEMGREP_DIR && git fetch https://$GIT_USERNAME:[email protected]/$SEMGREP_REPO_NAME.git $BASELINE_BRANCH:refs/remotes/origin/$BASELINE_BRANCH && python -m semgrep_agent --baseline-ref $(git merge-base origin/$BASELINE_BRANCH HEAD) --publish-token $SEMGREP_APP_TOKEN --publish-deployment $SEMGREP_DEPLOYMENT_ID && cd ../ && rm -r $SEMGREP_DIR'
wgetUpdateGithub('success', 'semgrep', "${BUILD_URL}", '123')
} catch (err) {
wgetUpdateGithub('failure', 'semgrep', "${BUILD_URL}", '123')
}
}
}
}
}
}
}
}
timestamps {
// TODO Please migrate this to C7 as sfc-dev2 servers do not support c6 nodes
node('regular-memory-node') {
stage('checkout') {
scmInfo = checkout scm
println("${scmInfo}")
env.GIT_BRANCH = scmInfo.GIT_BRANCH
}
stage('Build') {
sh '''\
|export JAVA_HOME=/usr/java/latest
|export PATH=$JAVA_HOME/bin:$PATH
|export GIT_BRANCH=${GIT_BRANCH}
|$WORKSPACE/ci/build.sh
'''.stripMargin()
}
params = [
string(name: 'client_git_branch', value: scmInfo.GIT_BRANCH),
string(name: 'client_git_commit', value: scmInfo.GIT_COMMIT),
string(name: 'branch', value: 'main'),
string(name: 'TARGET_DOCKER_TEST_IMAGE', value: 'jdbc-centos6-default'),
string(name: 'parent_job', value: env.JOB_NAME),
string(name: 'parent_build_number', value: env.BUILD_NUMBER),
string(name: 'timeout_value', value: '420'),
string(name: 'PR_Key', value: scmInfo.GIT_BRANCH.substring(3))
]
stage('Test') {
try {
def commit_hash = "main" // default which we want to override
def bptp_tag = "bptp-built"
def response = authenticatedGithubCall("https://api.github.com/repos/snowflakedb/snowflake/git/ref/tags/${bptp_tag}")
commit_hash = response.object.sha
// Append the bptp-built commit sha to params
params += [string(name: 'svn_revision', value: commit_hash)]
} catch(Exception e) {
println("Exception computing commit hash from: ${response}")
}
parallel (
'Test JDBC 1': { build job: 'RT-LanguageJDBC1-PC',parameters: params
},
'Test JDBC 2': { build job: 'RT-LanguageJDBC2-PC',parameters: params
},
'Test JDBC 3': { build job: 'RT-LanguageJDBC3-PC',parameters: params
},
'Test JDBC 4': { build job: 'RT-LanguageJDBC4-PC',parameters: params
},
'CodeCoverage JDBC': { build job: 'RT-LanguageJDBC-CodeCoverage-PC',parameters: params
}
)
}
}
}
// Generic github API function
// url : API url to call get command
def authenticatedGithubCall(url) {
withCredentials([
usernamePassword(credentialsId: 'jenkins-snowflakedb-github-app',
usernameVariable: 'GITHUB_USER',
passwordVariable: 'GITHUB_TOKEN'),
]) {
try {
def encodedAuth = Base64.getEncoder().encodeToString(
"${GITHUB_USER}:${GITHUB_TOKEN}".getBytes(java.nio.charset.StandardCharsets.UTF_8)
)
def authHeaderValue = "Basic ${encodedAuth}"
def connection = new URL(url).openConnection()
connection.setRequestProperty("Authorization", authHeaderValue)
if (connection.getResponseCode() >= 300) {
println("ERROR: Status fetch from ${url} returned ${connection.getResponseCode()}")
println(connection.getErrorStream().getText())
return null
}
return new groovy.json.JsonSlurperClassic().parseText(connection.getInputStream().getText())
} catch(Exception e) {
println("Exception fetching ${url}: ${e}")
return null
}
}
}
def wgetUpdateGithub(String state, String folder, String targetUrl, String seconds) {
def ghURL = "https://api.github.com/repos/snowflakedb/snowflake-jdbc/statuses/$COMMIT_SHA_LONG"
def data = JsonOutput.toJson([state: "${state}", context: "jenkins/${folder}",target_url: "${targetUrl}"])
sh "wget ${ghURL} --spider -q --header='Authorization: token $GIT_PASSWORD' --post-data='${data}'"
}