This repository has been archived by the owner on Oct 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
ci: stash/unstash steps with Google Cloud Storage #531
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7cf84b0
ci: support stash/unstash with GCS
v1v dce5627
Merge branch 'master' into feature/stashv2
v1v eb8fd0f
ci: simplify tar when running in .
v1v 939f5b8
fix the right order
v1v ccbef8b
test: add ITs and test-infra
v1v 6c61380
Merge branch 'feature/stashv2' of github.com:v1v/apm-pipeline-library…
v1v 06d853f
fix: . is not required when extracting
v1v ce01608
Merge branch 'master' into feature/stashv2
v1v 7ea9d7a
Merge remote-tracking branch 'upstream/master' into feature/stashv2
v1v 84734aa
docs: update main readme and further configuration
v1v 6a119da
Apply suggestions from code review
v1v File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import org.junit.Before | ||
import org.junit.Test | ||
import static org.junit.Assert.assertFalse | ||
import static org.junit.Assert.assertNull | ||
import static org.junit.Assert.assertTrue | ||
|
||
class StashV2StepTests extends ApmBasePipelineTest { | ||
String scriptName = 'vars/stashV2.groovy' | ||
|
||
@Override | ||
@Before | ||
void setUp() throws Exception { | ||
super.setUp() | ||
} | ||
|
||
@Test | ||
void test_without_name_param() throws Exception { | ||
def script = loadScript(scriptName) | ||
try { | ||
script.call() | ||
} catch(e) { | ||
//NOOP | ||
} | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('error', 'stashV2: name param is required.')) | ||
assertJobStatusFailure() | ||
} | ||
|
||
@Test | ||
void test_without_bucket_param_without_env_variable() throws Exception { | ||
def script = loadScript(scriptName) | ||
try { | ||
script.call(name: 'source') | ||
} catch(e) { | ||
//NOOP | ||
} | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('error', 'stashV2: bucket param is required or JOB_GCS_BUCKET env variable.')) | ||
assertJobStatusFailure() | ||
} | ||
|
||
@Test | ||
void test_without_credentialsId_param_without_env_variable() throws Exception { | ||
def script = loadScript(scriptName) | ||
try { | ||
script.call(name: 'source', bucket: 'foo') | ||
} catch(e) { | ||
//NOOP | ||
} | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('error', 'stashV2: credentialsId param is required or JOB_GCS_CREDENTIALS env variable.')) | ||
assertJobStatusFailure() | ||
} | ||
|
||
@Test | ||
void test_bucket_precedence() throws Exception { | ||
def script = loadScript(scriptName) | ||
env.JOB_GCS_BUCKET = 'bar' | ||
script.call(name: 'source', bucket: 'foo', credentialsId: 'secret') | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('log', 'stashV2: JOB_GCS_BUCKET is set. bucket param got precedency instead.')) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'tar --exclude=source.tgz -czf source.tgz .')) | ||
assertTrue(assertMethodCallContainsPattern('googleStorageUpload', 'bucket=gs://foo')) | ||
assertTrue(assertMethodCallContainsPattern('googleStorageUpload', 'credentialsId=secret')) | ||
assertTrue(assertMethodCallContainsPattern('googleStorageUpload', 'pattern=source.tgz')) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'rm source.tgz')) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test | ||
void test_credentialsId_precedence() throws Exception { | ||
def script = loadScript(scriptName) | ||
env.JOB_GCS_CREDENTIALS = 'bar' | ||
script.call(name: 'source', bucket: 'foo', credentialsId: 'my-super-credentials') | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('log', 'stashV2: JOB_GCS_CREDENTIALS is set. credentialsId param got precedency instead.')) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'tar --exclude=source.tgz -czf source.tgz .')) | ||
assertTrue(assertMethodCallContainsPattern('googleStorageUpload', 'bucket=gs://foo')) | ||
assertTrue(assertMethodCallContainsPattern('googleStorageUpload', 'credentialsId=my-super-credentials')) | ||
assertTrue(assertMethodCallContainsPattern('googleStorageUpload', 'pattern=source.tgz')) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'rm source.tgz')) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test | ||
void test_linux_with_parameters() throws Exception { | ||
def script = loadScript(scriptName) | ||
def bucketUri = script.call(name: 'source', bucket: 'foo', credentialsId: 'secret') | ||
printCallStack() | ||
assertFalse(assertMethodCallContainsPattern('log', 'got precedency instead.')) | ||
assertNull(assertMethodCall('bat')) | ||
assertTrue(assertMethodCallContainsPattern('writeFile', 'file=source.tgz')) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'tar --exclude=source.tgz -czf source.tgz .')) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'rm source.tgz')) | ||
assertTrue(bucketUri.contains('source/source.tgz')) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test | ||
void test_windows_with_parameters() throws Exception { | ||
def script = loadScript(scriptName) | ||
helper.registerAllowedMethod('isUnix', [], { false }) | ||
script.call(name: 'source', bucket: 'foo', credentialsId: 'secret') | ||
printCallStack() | ||
assertFalse(assertMethodCallContainsPattern('log', 'got precedency instead.')) | ||
assertNull(assertMethodCall('sh')) | ||
assertTrue(assertMethodCallContainsPattern('writeFile', 'file=source.tgz')) | ||
assertTrue(assertMethodCallContainsPattern('bat', 'tar --exclude=source.tgz -czf source.tgz .')) | ||
assertTrue(assertMethodCallContainsPattern('bat', 'del source.tgz')) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test | ||
void test_missing_build_context() throws Exception { | ||
def script = loadScript(scriptName) | ||
env.remove('BUILD_ID') | ||
try { | ||
script.call(name: 'source', bucket: 'foo', credentialsId: 'secret') | ||
} catch(e) { | ||
//NOOP | ||
} | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('error', 'BUILD_ID and JOB_NAME environment variables are required.')) | ||
assertJobStatusFailure() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import org.junit.Before | ||
import org.junit.Test | ||
import static org.junit.Assert.assertFalse | ||
import static org.junit.Assert.assertNull | ||
import static org.junit.Assert.assertTrue | ||
|
||
class UnstashV2StepTests extends ApmBasePipelineTest { | ||
String scriptName = 'vars/unstashV2.groovy' | ||
|
||
@Override | ||
@Before | ||
void setUp() throws Exception { | ||
super.setUp() | ||
} | ||
|
||
@Test | ||
void test_without_name_param() throws Exception { | ||
def script = loadScript(scriptName) | ||
try { | ||
script.call() | ||
} catch(e) { | ||
//NOOP | ||
} | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('error', 'unstashV2: name param is required.')) | ||
assertJobStatusFailure() | ||
} | ||
|
||
@Test | ||
void test_without_bucket_param_without_env_variable() throws Exception { | ||
def script = loadScript(scriptName) | ||
try { | ||
script.call(name: 'source') | ||
} catch(e) { | ||
//NOOP | ||
} | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('error', 'unstashV2: bucket param is required or JOB_GCS_BUCKET env variable.')) | ||
assertJobStatusFailure() | ||
} | ||
|
||
@Test | ||
void test_without_credentialsId_param_without_env_variable() throws Exception { | ||
def script = loadScript(scriptName) | ||
try { | ||
script.call(name: 'source', bucket: 'foo') | ||
} catch(e) { | ||
//NOOP | ||
} | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('error', 'unstashV2: credentialsId param is required or JOB_GCS_CREDENTIALS env variable.')) | ||
assertJobStatusFailure() | ||
} | ||
|
||
@Test | ||
void test_bucket_precedence() throws Exception { | ||
def script = loadScript(scriptName) | ||
env.JOB_GCS_BUCKET = 'bar' | ||
script.call(name: 'source', bucket: 'foo', credentialsId: 'secret') | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('log', 'unstashV2: JOB_GCS_BUCKET is set. bucket param got precedency instead.')) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'tar -xpf source.tgz')) | ||
assertTrue(assertMethodCallContainsPattern('googleStorageDownload', 'bucketUri=gs://foo')) | ||
assertTrue(assertMethodCallContainsPattern('googleStorageDownload', 'credentialsId=secret')) | ||
assertTrue(assertMethodCallContainsPattern('googleStorageDownload', 'source/source.tgz')) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'rm source.tgz')) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test | ||
void test_credentialsId_precedence() throws Exception { | ||
def script = loadScript(scriptName) | ||
env.JOB_GCS_CREDENTIALS = 'bar' | ||
script.call(name: 'source', bucket: 'foo', credentialsId: 'my-super-credentials') | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('log', 'unstashV2: JOB_GCS_CREDENTIALS is set. credentialsId param got precedency instead.')) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'tar -xpf source.tgz')) | ||
assertTrue(assertMethodCallContainsPattern('googleStorageDownload', 'bucketUri=gs://foo')) | ||
assertTrue(assertMethodCallContainsPattern('googleStorageDownload', 'credentialsId=my-super-credentials')) | ||
assertTrue(assertMethodCallContainsPattern('googleStorageDownload', 'source/source.tgz')) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'rm source.tgz')) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test | ||
void test_linux_with_parameters() throws Exception { | ||
def script = loadScript(scriptName) | ||
script.call(name: 'source', bucket: 'foo', credentialsId: 'secret') | ||
printCallStack() | ||
assertFalse(assertMethodCallContainsPattern('log', 'got precedency instead.')) | ||
assertNull(assertMethodCall('bat')) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'tar -xpf source.tgz')) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'rm source.tgz')) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test | ||
void test_windows_with_parameters() throws Exception { | ||
def script = loadScript(scriptName) | ||
helper.registerAllowedMethod('isUnix', [], { false }) | ||
script.call(name: 'source', bucket: 'foo', credentialsId: 'secret') | ||
printCallStack() | ||
assertFalse(assertMethodCallContainsPattern('log', 'got precedency instead.')) | ||
assertNull(assertMethodCall('sh')) | ||
assertTrue(assertMethodCallContainsPattern('bat', 'tar -xpf source.tgz')) | ||
assertTrue(assertMethodCallContainsPattern('bat', 'del source.tgz')) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test | ||
void test_missing_build_context() throws Exception { | ||
def script = loadScript(scriptName) | ||
env.remove('BUILD_ID') | ||
try { | ||
script.call(name: 'source', bucket: 'foo', credentialsId: 'secret') | ||
} catch(e) { | ||
//NOOP | ||
} | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('error', 'BUILD_ID and JOB_NAME environment variables are required.')) | ||
assertJobStatusFailure() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
NAME = 'it/stashV2' | ||
DSL = '''pipeline { | ||
agent any | ||
stages { | ||
stage('stashV2') { | ||
steps { | ||
// Credentials for the Google Storage are file based, so it's not | ||
// fully automated yet, for such you need to create a new credentials | ||
// manually | ||
input 'Please create Google Service account from private key http://localhost:18080/credentials/store/system/domain/_/newCredentials' | ||
gitCheckout(basedir: 'sub-folder', branch: 'master', | ||
credentialsId: '2a9602aa-ab9f-4e52-baf3-b71ca88469c7-UserAndToken', | ||
repo: 'https://github.com/octocat/Hello-World.git') | ||
withEnv(["JOB_GCS_BUCKET=beats-ci-temp", "JOB_GCS_CREDENTIALS=beats-ci-gcs-plugin"]){ | ||
stashV2(name: 'source') | ||
} | ||
stashV2(name: 'foo', bucket: 'beats-ci-temp', credentialsId: 'beats-ci-gcs-plugin') | ||
deleteDir() | ||
} | ||
} | ||
stage('unstashV2'){ | ||
steps { | ||
dir('source') { | ||
withEnv(['JOB_GCS_BUCKET=beats-ci-temp', 'JOB_GCS_CREDENTIALS=beats-ci-gcs-plugin']){ | ||
unstashV2(name: 'source') | ||
} | ||
sh 'ls -ltra sub-folder' | ||
} | ||
dir('foo') { | ||
unstashV2(name: 'foo', bucket: 'beats-ci-temp', credentialsId: 'beats-ci-gcs-plugin') | ||
sh 'ls -ltra sub-folder' | ||
} | ||
} | ||
} | ||
} | ||
}''' | ||
|
||
pipelineJob(NAME) { | ||
definition { | ||
cps { | ||
script(DSL.stripIndent()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix an issue