Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.
24 changes: 21 additions & 3 deletions .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,25 @@ pipeline {
}
}
stage('Google Storage') {
steps {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run always

withGithubNotify(context: 'Google Storage', description: 'Google Storage step') {
dir("${BASE_DIR}"){
googleStorageUploadExt(bucket: "gs://${env.JOB_GCS_BUCKET}/${env.REPO}/commits/${env.GIT_BASE_COMMIT}", pattern: 'src/**/*.gdsl', sharedPublicly: false)
}
}
}
}
stage('Google Storage Windows') {
agent { label 'windows-2019-immutable' }
when {
branch 'master' // TODO: to be removed as soon as the step is available in the master branch
branch 'master'
}
steps {
withGithubNotify(context: 'Google Storage', description: 'Google Storage step') {
withGithubNotify(context: 'Google Storage Windows', description: 'Google Storage step') {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
googleStorageUploadExt(bucket: "gs://${env.JOB_GCS_BUCKET}/${env.REPO}/commits/${env.GIT_BASE_COMMIT}", pattern: 'target/**/*.xml', sharedPublicly: false)
googleStorageUploadExt(bucket: "gs://${env.JOB_GCS_BUCKET}/${env.REPO}/commits/${env.GIT_BASE_COMMIT}", pattern: 'resources/**/*.json', sharedPublicly: false)
}
}
}
Expand All @@ -135,9 +147,15 @@ pipeline {
validateDeclarativePipeline '.ci/Jenkinsfile'
validateDeclarativePipeline '.ci/buildBeatsDockerImages.groovy'
validateDeclarativePipeline '.ci/buildDockerImages.groovy'
validateDeclarativePipeline '.ci/chatops.groovy'
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated

validateDeclarativePipeline '.ci/buildDockerImages.groovy'
validateDeclarativePipeline '.ci/dockerImagesOpbeans.groovy'
validateDeclarativePipeline '.ci/jobdsl-generator.groovy'
validateDeclarativePipeline '.ci/licenseScan.groovy'
validateDeclarativePipeline '.ci/licenseScanGeneral.groovy'
validateDeclarativePipeline '.ci/schedule-daily.groovy'
validateDeclarativePipeline '.ci/schedule-weekly.groovy'
validateDeclarativePipeline '.ci/validateWorkersBeatsCi.groovy'
validateDeclarativePipeline 'resources/JenkinsfileTemplate.groovy'
}
}
Expand Down
1 change: 1 addition & 0 deletions src/test/groovy/ApmBasePipelineTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ class ApmBasePipelineTest extends DeclarativePipelineTest {
helper.registerAllowedMethod('string', [Map.class], { m -> return m })
helper.registerAllowedMethod('timeout', [Integer.class, Closure.class], null)
helper.registerAllowedMethod('unstash', [String.class], null)
helper.registerAllowedMethod('unzip', [Map.class], null)
helper.registerAllowedMethod('upstreamDevelopers', { "OK" })
helper.registerAllowedMethod('usernamePassword', [Map.class], { m ->
m.each{ k, v ->
Expand Down
12 changes: 5 additions & 7 deletions src/test/groovy/GoogleStorageUploadExtStepTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ class GoogleStorageUploadExtStepTests extends ApmBasePipelineTest {
@Test
void test_windows() throws Exception {
helper.registerAllowedMethod('isUnix', [], { false })
try {
script.call(bucket: 'gs://foo', pattern: 'file.txt')
} catch(e){
//NOOP
}
helper.registerAllowedMethod('gsutil', [Map.class], { return 'Operation completed over 1 objects.' })
def ret = script.call(bucket: 'gs://foo', pattern: 'file.txt')
printCallStack()
assertTrue(assertMethodCallContainsPattern('error', 'gsutil: windows is not supported yet.'))
assertJobStatusFailure()
assertFalse(assertMethodCallContainsPattern('gsutil', '-a public-read'))
assertTrue(assertMethodCallContainsPattern('gsutil', 'file.txt gs://foo'))
assertFalse(ret.isEmpty())
}

@Test
Expand Down
40 changes: 26 additions & 14 deletions src/test/groovy/GsutilStepTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@ class GsutilStepTests extends ApmBasePipelineTest {
script = loadScript('vars/gsutil.groovy')
}

@Test
void test_windows() throws Exception {
helper.registerAllowedMethod('isUnix', [], { false })
try {
script.call()
} catch(e){
//NOOP
}
printCallStack()
assertTrue(assertMethodCallContainsPattern('error', 'gsutil: windows is not supported yet.'))
assertJobStatusFailure()
}

@Test
void test_without_command() throws Exception {
try {
Expand Down Expand Up @@ -74,6 +61,7 @@ class GsutilStepTests extends ApmBasePipelineTest {
script.call(command: 'cp', credentialsId: 'foo')
printCallStack()
assertTrue(assertMethodCallContainsPattern('withCredentials', ''))
assertTrue(assertMethodCallContainsPattern('sh', 'gcloud auth activate-service-account --key-file ${FILE_CREDENTIAL}'))
assertTrue(assertMethodCallContainsPattern('sh', "gsutil cp"))
assertTrue(assertMethodCallContainsPattern('withEnv', 'PATH+GSUTIL'))
assertFalse(assertMethodCallContainsPattern('sh', "wget -q -O"))
Expand Down Expand Up @@ -102,7 +90,7 @@ class GsutilStepTests extends ApmBasePipelineTest {
script.call(command: 'cp', credentialsId: 'foo')
printCallStack()
assertTrue(assertMethodCallContainsPattern('withEnv', 'PATH+GSUTIL'))
assertTrue(assertMethodCallContainsPattern('sh', 'wget -q -O'))
assertTrue(assertMethodCallContainsPattern('sh', 'wget -q -O gsutil.tar.gz https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-319.0.0-linux-x86_64.tar.gz'))
assertJobStatusSuccess()
}

Expand Down Expand Up @@ -142,4 +130,28 @@ class GsutilStepTests extends ApmBasePipelineTest {
assertTrue(assertMethodCallContainsPattern('log', 'gsutil: set the gsutilLocation.'))
assertJobStatusSuccess()
}

@Test
void test_windows() throws Exception {
helper.registerAllowedMethod('isUnix', [], { false })
script.call(command: 'cp', credentialsId: 'foo')
printCallStack()
assertTrue(assertMethodCallContainsPattern('withCredentials', ''))
assertTrue(assertMethodCallContainsPattern('bat', 'gcloud auth activate-service-account --key-file %FILE_CREDENTIAL%'))
assertTrue(assertMethodCallContainsPattern('bat', "gsutil cp"))
assertTrue(assertMethodCallContainsPattern('withEnv', 'PATH+GSUTIL'))
assertFalse(assertMethodCallContainsPattern('bat', "wget -q -O"))
assertJobStatusSuccess()
}

@Test
void test_without_gh_installed_by_default_with_wget_in_windows() throws Exception {
helper.registerAllowedMethod('isInstalled', [Map.class], { m -> return m.tool.equals('wget') })
helper.registerAllowedMethod('isUnix', [], { false })
script.call(command: 'cp', credentialsId: 'foo')
printCallStack()
assertTrue(assertMethodCallContainsPattern('withEnv', 'PATH+GSUTIL'))
assertTrue(assertMethodCallContainsPattern('bat', 'wget -q -O gsutil.zip https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-319.0.0-windows-x86_64.zip'))
assertJobStatusSuccess()
}
}
26 changes: 26 additions & 0 deletions vars/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,32 @@ As long as we got some concurrency issues
googleStorageUpload(args)
```

## googleStorageUploadExt
Upload the given pattern files to the given bucket.

```
// Copy file.txt into the bucket
googleStorageUploadExt(pattern: 'file.txt', bucket: 'gs://bucket/folder/', credentialsId: 'foo', sharedPublicly: false)

```

* bucket: The Google Storage bucket format gs://bucket/folder/subfolder/. Mandatory
* credentialsId: The credentials to access the repo (repo permissions). Optional. Default to `JOB_GCS_CREDENTIALS`
* pattern: The file to pattern to search and copy. Mandatory.
* sharedPublicly: Whether to shared those objects publically. Optional. Default false.

## gsutil
Wrapper to interact with the gsutil command line. It returns the stdout output.

```
// Copy file.txt into the bucket
gsutil(command: 'cp file.txt gs://bucket/folder/', credentialsId: 'foo' ])

```

* command: The gsutil command to be executed. Mandatory
* credentialsId: The credentials to access the repo (repo permissions). Mandatory.

## httpRequest
Step to make HTTP request and get the result.
If the return code is >= 400, it would throw an error.
Expand Down
36 changes: 24 additions & 12 deletions vars/gsutil.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import groovy.transform.Field
@Field def gsUtilLocation = ''

def call(Map args = [:]) {
if(!isUnix()) {
error 'gsutil: windows is not supported yet.'
}
def command = args.containsKey('command') ? args.command : error('gsutil: command argument is required.')
def credentialsId = args.containsKey('credentialsId') ? args.credentialsId : error('gsutil: credentialsId argument is required.')

Expand All @@ -38,25 +35,40 @@ def call(Map args = [:]) {
downloadInstaller(gsUtilLocation)
}

withCredentials([file(credentialsId: credentialsId, variable: 'FILE_CREDENTIAL')]) {
sh(label: 'authenticate', script: 'gcloud auth activate-service-account --key-file ${FILE_CREDENTIAL}')
withCredentials([file(credentialsId: credentialsId, variable: 'FILE_CREDENTIAL')]) {
def credentialsVariable = isUnix() ? '${FILE_CREDENTIAL}' : '%FILE_CREDENTIAL%'
cmd(label: 'authenticate', script: 'gcloud auth activate-service-account --key-file ' + credentialsVariable)
Comment on lines +39 to +40
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid interpolation for any kind of credentials

}
return sh(label: "gsutil ${command}", script: "gsutil ${command}", returnStdout: true)
return cmd(label: "gsutil ${command}", script: "gsutil ${command}", returnStdout: true)
}
}

def downloadInstaller(where) {
def url = 'https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-319.0.0'
url = "${url}-linux-${is64() ? 'x86_64' : 'x86'}.tar.gz"
def tarball = 'gsutil.tar.gz'
def url = googleCloudSdkURL()
def tarball = "gsutil.${isUnix() ? 'tar.gz' : 'zip'}"
if(isInstalled(tool: 'wget', flag: '--version')) {
dir(where) {
retryWithSleep(retries: 3, seconds: 5, backoff: true) {
sh(label: 'download gsutil', script: "wget -q -O ${tarball} ${url}")
sh(label: 'untar gsutil', script: "tar -xpf ${tarball} --strip-components=1")
cmd(label: 'download gsutil', script: "wget -q -O ${tarball} ${url}")
uncompress(tarball)
}
}
} else {
log(level: 'WARN', text: 'gsutil: wget is not available. gsutil will not be installed then.')
}
}
}

def googleCloudSdkURL() {
def url = 'https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-319.0.0'
def os = isUnix() ? 'linux' : 'windows'
def ext = isUnix() ? 'tar.gz' : 'zip'
return "${url}-${os}-${is64() ? 'x86_64' : 'x86'}.${ext}"
}

def uncompress(tarball) {
if (isUnix()) {
sh(label: 'untar gsutil', script: "tar -xpf ${tarball} --strip-components=1")
} else {
unzip(quiet: true, zipFile: tarball)
}
}
2 changes: 0 additions & 2 deletions vars/gsutil.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ Wrapper to interact with the gsutil command line. It returns the stdout output.

* command: The gsutil command to be executed. Mandatory
* credentialsId: The credentials to access the repo (repo permissions). Mandatory.

_NOTE_: Windows is not supported yet.