mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-03-25 21:39:13 +02:00
Create awsS3Upload (#3737)
* add Step awsS3Upload * fix JSON Read * fix groovy wrapper * change credentials to secret text * Change credentials type to token * add cleanup for environment variables * Add AwsS3UploadCommand to piper.go * add documentation of awsS3Upload * Fix JSON String Example in Documentation * add the Upload of whole directories * add Logging to awsS3Upload.go * imporve Logging * fix and improve unit tests * fix non-existing-filepaths-Bug * fix windows filepaths * remove ... from logging * change step description * fix PR Tests * remove redundant code * try to run tests sequentially * fix file.Close * executed go mod tidy * requested changes * fix comments and awsCredentials * Update documentation/docs/steps/awsS3Upload.md Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> * use temporary files for unit tests * executed go generate Co-authored-by: ffeldmann <f.feldmann@sap.com> Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com> Co-authored-by: Thorsten Duda <thorsten.duda@sap.com>
This commit is contained in:
parent
6070549704
commit
6714794066
142
cmd/awsS3Upload.go
Normal file
142
cmd/awsS3Upload.go
Normal file
@ -0,0 +1,142 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/log"
|
||||
"github.com/SAP/jenkins-library/pkg/telemetry"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
)
|
||||
|
||||
// S3PutObjectAPI defines the interface for the PutObject function.
|
||||
// We use this interface to test the function using a mocked service.
|
||||
type S3PutObjectAPI interface {
|
||||
PutObject(ctx context.Context,
|
||||
params *s3.PutObjectInput,
|
||||
optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
|
||||
}
|
||||
|
||||
// PutFile uploads a file to an AWS S3 bucket
|
||||
// The function needs a context (including the AWS Region) and a PutObjectInput for the service call
|
||||
// The return value is a PutObjectOutput with the result of the upload
|
||||
func PutFile(c context.Context, api S3PutObjectAPI, input *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
|
||||
return api.PutObject(c, input)
|
||||
}
|
||||
|
||||
// Struct to store the AWS credentials from a specified JSON string
|
||||
type awsCredentials struct {
|
||||
AwsAccessKeyID string `json:"access_key_id"`
|
||||
Bucket string `json:"bucket"`
|
||||
AwsSecretAccessKey string `json:"secret_access_key"`
|
||||
AwsRegion string `json:"region"`
|
||||
}
|
||||
|
||||
func awsS3Upload(configOptions awsS3UploadOptions, telemetryData *telemetry.CustomData) {
|
||||
// Prepare Credentials
|
||||
log.Entry().Infoln("Start reading AWS Credentials")
|
||||
var obj awsCredentials
|
||||
|
||||
err := json.Unmarshal([]byte(configOptions.JSONCredentialsAWS), &obj)
|
||||
if err != nil {
|
||||
log.Entry().
|
||||
WithError(err).
|
||||
Fatal("Could not read JSONCredentialsAWS")
|
||||
}
|
||||
|
||||
// Set environment variables which are needed to initialize S3 Client
|
||||
log.Entry().Infoln("Successfully read AWS Credentials. Setting up environment variables")
|
||||
awsRegionSet := setenvIfEmpty("AWS_REGION", obj.AwsRegion)
|
||||
awsAccessKeyIDSet := setenvIfEmpty("AWS_ACCESS_KEY_ID", obj.AwsAccessKeyID)
|
||||
awsSecretAccessKeySet := setenvIfEmpty("AWS_SECRET_ACCESS_KEY", obj.AwsSecretAccessKey)
|
||||
|
||||
defer removeEnvIfPreviouslySet("AWS_REGION", awsRegionSet)
|
||||
defer removeEnvIfPreviouslySet("AWS_ACCESS_KEY_ID", awsAccessKeyIDSet)
|
||||
defer removeEnvIfPreviouslySet("AWS_SECRET_ACCESS_KEY", awsSecretAccessKeySet)
|
||||
|
||||
// Initialize S3 Client
|
||||
log.Entry().Infoln("Loading Configuration for S3 Client")
|
||||
cfg, err := config.LoadDefaultConfig(context.TODO())
|
||||
if err != nil {
|
||||
log.Entry().
|
||||
WithError(err).
|
||||
Fatal("AWS Client Configuration failed")
|
||||
}
|
||||
client := s3.NewFromConfig(cfg)
|
||||
|
||||
err = runAwsS3Upload(&configOptions, client, obj.Bucket)
|
||||
if err != nil {
|
||||
log.Entry().WithError(err).Fatal("Step execution failed")
|
||||
}
|
||||
}
|
||||
|
||||
func runAwsS3Upload(configOptions *awsS3UploadOptions, client S3PutObjectAPI, bucket string) error {
|
||||
// Iterate through directories
|
||||
err := filepath.Walk(configOptions.FilePath, func(currentFilePath string, f os.FileInfo, err error) error {
|
||||
// Handle Failure to prevent panic (e.g. in case of an invalid filepath)
|
||||
if err != nil {
|
||||
log.Entry().WithError(err).Warnf("Failed to access path: '%v'", currentFilePath)
|
||||
return err
|
||||
}
|
||||
// Skip directories, only upload files
|
||||
if !f.IsDir() {
|
||||
log.Entry().Infof("Current target path is: '%v'", currentFilePath)
|
||||
|
||||
// Open File
|
||||
currentFile, e := os.Open(currentFilePath)
|
||||
if e != nil {
|
||||
log.Entry().WithError(e).Warnf("Could not open the file '%s'", currentFilePath)
|
||||
return e
|
||||
}
|
||||
defer currentFile.Close()
|
||||
|
||||
// AWS SDK needs UNIX file paths to automatically create directories
|
||||
key := filepath.ToSlash(currentFilePath)
|
||||
|
||||
// Intitialize S3 PutObjectInput
|
||||
inputObject := &s3.PutObjectInput{
|
||||
Bucket: &bucket,
|
||||
Key: &key,
|
||||
Body: currentFile,
|
||||
}
|
||||
|
||||
// Upload File
|
||||
log.Entry().Infof("Start upload of file '%v'", currentFilePath)
|
||||
_, e = PutFile(context.TODO(), client, inputObject)
|
||||
if e != nil {
|
||||
log.Entry().WithError(e).Warnf("There was an error during the upload of file '%v'", currentFilePath)
|
||||
return e
|
||||
}
|
||||
|
||||
log.Entry().Infof("Upload of file '%v' was successful!", currentFilePath)
|
||||
return e
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Entry().Infoln("Upload has successfully finished!")
|
||||
return err
|
||||
}
|
||||
|
||||
// Function to set environment variables if they are not already set
|
||||
func setenvIfEmpty(env, val string) bool {
|
||||
if len(os.Getenv(env)) == 0 {
|
||||
os.Setenv(env, val)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Function to remove environment variables if they are set
|
||||
func removeEnvIfPreviouslySet(env string, previouslySet bool) {
|
||||
if previouslySet {
|
||||
os.Setenv(env, "")
|
||||
}
|
||||
}
|
168
cmd/awsS3Upload_generated.go
Normal file
168
cmd/awsS3Upload_generated.go
Normal file
@ -0,0 +1,168 @@
|
||||
// Code generated by piper's step-generator. DO NOT EDIT.
|
||||
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/config"
|
||||
"github.com/SAP/jenkins-library/pkg/log"
|
||||
"github.com/SAP/jenkins-library/pkg/splunk"
|
||||
"github.com/SAP/jenkins-library/pkg/telemetry"
|
||||
"github.com/SAP/jenkins-library/pkg/validation"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type awsS3UploadOptions struct {
|
||||
JSONCredentialsAWS string `json:"jsonCredentialsAWS,omitempty"`
|
||||
FilePath string `json:"filePath,omitempty"`
|
||||
}
|
||||
|
||||
// AwsS3UploadCommand Uploads a specified file or directory into a given AWS S3 Bucket
|
||||
func AwsS3UploadCommand() *cobra.Command {
|
||||
const STEP_NAME = "awsS3Upload"
|
||||
|
||||
metadata := awsS3UploadMetadata()
|
||||
var stepConfig awsS3UploadOptions
|
||||
var startTime time.Time
|
||||
var logCollector *log.CollectorHook
|
||||
var splunkClient *splunk.Splunk
|
||||
telemetryClient := &telemetry.Telemetry{}
|
||||
|
||||
var createAwsS3UploadCmd = &cobra.Command{
|
||||
Use: STEP_NAME,
|
||||
Short: "Uploads a specified file or directory into a given AWS S3 Bucket",
|
||||
Long: `Uploads a specified file or directory as S3 Objects into a given AWS S3 Bucket.
|
||||
In case a file is uploaded that is already contained in the S3 bucket, it will be overwritten with the latest version.`,
|
||||
PreRunE: func(cmd *cobra.Command, _ []string) error {
|
||||
startTime = time.Now()
|
||||
log.SetStepName(STEP_NAME)
|
||||
log.SetVerbose(GeneralConfig.Verbose)
|
||||
|
||||
GeneralConfig.GitHubAccessTokens = ResolveAccessTokens(GeneralConfig.GitHubTokens)
|
||||
|
||||
path, _ := os.Getwd()
|
||||
fatalHook := &log.FatalHook{CorrelationID: GeneralConfig.CorrelationID, Path: path}
|
||||
log.RegisterHook(fatalHook)
|
||||
|
||||
err := PrepareConfig(cmd, &metadata, STEP_NAME, &stepConfig, config.OpenPiperFile)
|
||||
if err != nil {
|
||||
log.SetErrorCategory(log.ErrorConfiguration)
|
||||
return err
|
||||
}
|
||||
log.RegisterSecret(stepConfig.JSONCredentialsAWS)
|
||||
|
||||
if len(GeneralConfig.HookConfig.SentryConfig.Dsn) > 0 {
|
||||
sentryHook := log.NewSentryHook(GeneralConfig.HookConfig.SentryConfig.Dsn, GeneralConfig.CorrelationID)
|
||||
log.RegisterHook(&sentryHook)
|
||||
}
|
||||
|
||||
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
||||
splunkClient = &splunk.Splunk{}
|
||||
logCollector = &log.CollectorHook{CorrelationID: GeneralConfig.CorrelationID}
|
||||
log.RegisterHook(logCollector)
|
||||
}
|
||||
|
||||
validation, err := validation.New(validation.WithJSONNamesForStructFields(), validation.WithPredefinedErrorMessages())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = validation.ValidateStruct(stepConfig); err != nil {
|
||||
log.SetErrorCategory(log.ErrorConfiguration)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
Run: func(_ *cobra.Command, _ []string) {
|
||||
stepTelemetryData := telemetry.CustomData{}
|
||||
stepTelemetryData.ErrorCode = "1"
|
||||
handler := func() {
|
||||
config.RemoveVaultSecretFiles()
|
||||
stepTelemetryData.Duration = fmt.Sprintf("%v", time.Since(startTime).Milliseconds())
|
||||
stepTelemetryData.ErrorCategory = log.GetErrorCategory().String()
|
||||
stepTelemetryData.PiperCommitHash = GitCommit
|
||||
telemetryClient.SetData(&stepTelemetryData)
|
||||
telemetryClient.Send()
|
||||
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
||||
splunkClient.Send(telemetryClient.GetData(), logCollector)
|
||||
}
|
||||
}
|
||||
log.DeferExitHandler(handler)
|
||||
defer handler()
|
||||
telemetryClient.Initialize(GeneralConfig.NoTelemetry, STEP_NAME)
|
||||
if len(GeneralConfig.HookConfig.SplunkConfig.Dsn) > 0 {
|
||||
splunkClient.Initialize(GeneralConfig.CorrelationID,
|
||||
GeneralConfig.HookConfig.SplunkConfig.Dsn,
|
||||
GeneralConfig.HookConfig.SplunkConfig.Token,
|
||||
GeneralConfig.HookConfig.SplunkConfig.Index,
|
||||
GeneralConfig.HookConfig.SplunkConfig.SendLogs)
|
||||
}
|
||||
awsS3Upload(stepConfig, &stepTelemetryData)
|
||||
stepTelemetryData.ErrorCode = "0"
|
||||
log.Entry().Info("SUCCESS")
|
||||
},
|
||||
}
|
||||
|
||||
addAwsS3UploadFlags(createAwsS3UploadCmd, &stepConfig)
|
||||
return createAwsS3UploadCmd
|
||||
}
|
||||
|
||||
func addAwsS3UploadFlags(cmd *cobra.Command, stepConfig *awsS3UploadOptions) {
|
||||
cmd.Flags().StringVar(&stepConfig.JSONCredentialsAWS, "jsonCredentialsAWS", os.Getenv("PIPER_jsonCredentialsAWS"), "JSON String Credentials to access AWS S3 Bucket")
|
||||
cmd.Flags().StringVar(&stepConfig.FilePath, "filePath", os.Getenv("PIPER_filePath"), "Name/Path of the file which should be uploaded")
|
||||
|
||||
cmd.MarkFlagRequired("jsonCredentialsAWS")
|
||||
cmd.MarkFlagRequired("filePath")
|
||||
}
|
||||
|
||||
// retrieve step metadata
|
||||
func awsS3UploadMetadata() config.StepData {
|
||||
var theMetaData = config.StepData{
|
||||
Metadata: config.StepMetadata{
|
||||
Name: "awsS3Upload",
|
||||
Aliases: []config.Alias{},
|
||||
Description: "Uploads a specified file or directory into a given AWS S3 Bucket",
|
||||
},
|
||||
Spec: config.StepSpec{
|
||||
Inputs: config.StepInputs{
|
||||
Secrets: []config.StepSecrets{
|
||||
{Name: "awsCredentialsId", Description: "Jenkins 'Secret Text' credentials ID containing the JSON file to authenticate to the AWS S3 Bucket", Type: "jenkins"},
|
||||
},
|
||||
Parameters: []config.StepParameters{
|
||||
{
|
||||
Name: "jsonCredentialsAWS",
|
||||
ResourceRef: []config.ResourceReference{
|
||||
{
|
||||
Name: "awsCredentialsId",
|
||||
Type: "secret",
|
||||
},
|
||||
},
|
||||
Scope: []string{"PARAMETERS"},
|
||||
Type: "string",
|
||||
Mandatory: true,
|
||||
Aliases: []config.Alias{},
|
||||
Default: os.Getenv("PIPER_jsonCredentialsAWS"),
|
||||
},
|
||||
{
|
||||
Name: "filePath",
|
||||
ResourceRef: []config.ResourceReference{
|
||||
{
|
||||
Name: "commonPipelineEnvironment",
|
||||
Param: "mtarFilePath",
|
||||
},
|
||||
},
|
||||
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
|
||||
Type: "string",
|
||||
Mandatory: true,
|
||||
Aliases: []config.Alias{},
|
||||
Default: os.Getenv("PIPER_filePath"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
return theMetaData
|
||||
}
|
17
cmd/awsS3Upload_generated_test.go
Normal file
17
cmd/awsS3Upload_generated_test.go
Normal file
@ -0,0 +1,17 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAwsS3UploadCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
testCmd := AwsS3UploadCommand()
|
||||
|
||||
// only high level testing performed - details are tested in step generation procedure
|
||||
assert.Equal(t, "awsS3Upload", testCmd.Use, "command name incorrect")
|
||||
|
||||
}
|
111
cmd/awsS3Upload_test.go
Normal file
111
cmd/awsS3Upload_test.go
Normal file
@ -0,0 +1,111 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
)
|
||||
|
||||
type mockPutObjectAPI func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error)
|
||||
|
||||
func (m mockPutObjectAPI) PutObject(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
|
||||
return m(ctx, params, optFns...)
|
||||
}
|
||||
|
||||
func TestRunAwsS3Upload(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("happy path", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
// create temporary file
|
||||
f, err := os.CreateTemp("", "tmpfile-") // in Go version older than 1.17 you can use ioutil.TempFile
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
defer os.Remove(f.Name())
|
||||
data := []byte("test test test")
|
||||
if _, err := f.Write(data); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// initialization
|
||||
config := awsS3UploadOptions{
|
||||
FilePath: f.Name(),
|
||||
}
|
||||
client := mockS3Client
|
||||
// test
|
||||
err = runAwsS3Upload(&config, client(t, config.FilePath), "fooBucket")
|
||||
// assert
|
||||
assert.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("error path", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
// initialization
|
||||
config := awsS3UploadOptions{
|
||||
FilePath: "nonExistingFilepath",
|
||||
}
|
||||
client := mockS3Client
|
||||
// test
|
||||
err := runAwsS3Upload(&config, client(t, config.FilePath), "fooBucket")
|
||||
// assert
|
||||
_, ok := err.(*fs.PathError)
|
||||
assert.True(t, ok)
|
||||
})
|
||||
|
||||
t.Run("error bucket", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
// create temporary file
|
||||
f, err := os.CreateTemp("", "tmpfile-") // in Go version older than 1.17 you can use ioutil.TempFile
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
defer os.Remove(f.Name())
|
||||
data := []byte("test test test")
|
||||
if _, err := f.Write(data); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// initialization
|
||||
config := awsS3UploadOptions{
|
||||
FilePath: f.Name(),
|
||||
}
|
||||
client := mockS3Client
|
||||
// test
|
||||
err = runAwsS3Upload(&config, client(t, config.FilePath), "errorBucket")
|
||||
// assert
|
||||
assert.EqualError(t, err, "expect fooBucket, got errorBucket")
|
||||
})
|
||||
}
|
||||
|
||||
func mockS3Client(t *testing.T, fileName string) S3PutObjectAPI {
|
||||
return mockPutObjectAPI(func(ctx context.Context, params *s3.PutObjectInput, optFns ...func(*s3.Options)) (*s3.PutObjectOutput, error) {
|
||||
t.Helper()
|
||||
if params.Bucket == nil {
|
||||
return nil, fmt.Errorf("expect bucket to not be nil")
|
||||
}
|
||||
if e, a := "fooBucket", *params.Bucket; e != a {
|
||||
return nil, fmt.Errorf("expect %v, got %v", e, a)
|
||||
}
|
||||
if params.Key == nil {
|
||||
return nil, fmt.Errorf("expect key to not be nil")
|
||||
}
|
||||
if e, a := filepath.ToSlash(fileName), *params.Key; e != a {
|
||||
return nil, fmt.Errorf("expect %v, got %v", e, a)
|
||||
}
|
||||
if params.Body == nil {
|
||||
return nil, fmt.Errorf("expect Body / io.Reader not to be nil")
|
||||
}
|
||||
return &s3.PutObjectOutput{}, nil
|
||||
})
|
||||
}
|
@ -31,6 +31,7 @@ func GetAllStepMetadata() map[string]config.StepData {
|
||||
"apiProxyDownload": apiProxyDownloadMetadata(),
|
||||
"apiProxyUpload": apiProxyUploadMetadata(),
|
||||
"artifactPrepareVersion": artifactPrepareVersionMetadata(),
|
||||
"awsS3Upload": awsS3UploadMetadata(),
|
||||
"batsExecuteTests": batsExecuteTestsMetadata(),
|
||||
"checkmarxExecuteScan": checkmarxExecuteScanMetadata(),
|
||||
"cloudFoundryCreateService": cloudFoundryCreateServiceMetadata(),
|
||||
|
@ -184,6 +184,7 @@ func Execute() {
|
||||
rootCmd.AddCommand(GradleExecuteBuildCommand())
|
||||
rootCmd.AddCommand(ApiKeyValueMapUploadCommand())
|
||||
rootCmd.AddCommand(PythonBuildCommand())
|
||||
rootCmd.AddCommand(AwsS3UploadCommand())
|
||||
|
||||
addRootFlags(rootCmd)
|
||||
|
||||
|
48
documentation/docs/steps/awsS3Upload.md
Normal file
48
documentation/docs/steps/awsS3Upload.md
Normal file
@ -0,0 +1,48 @@
|
||||
# ${docGenStepName}
|
||||
|
||||
## ${docGenDescription}
|
||||
|
||||
## Prerequisites
|
||||
|
||||
* Before you can use the step awsS3Upload, you must have an Amazon account. See [How do I create and activate a new AWS account?](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/) for details.
|
||||
* You will need AWS access keys for your S3 Bucket. Access keys consist of an access key ID and secret access key, which are used to sign programmatic requests that you make to AWS. You can create them by using the AWS Management Console.
|
||||
* The access keys must allow the action "s3:PutObject" for the specified S3 Bucket
|
||||
|
||||
## Set up the AWS Credentials
|
||||
|
||||
To make your AWS credentials available to the jenkins library, store them as Jenkins credentials of type "Secret Text". The "Secret Text" must be in JSON format and contain the "access_key_id", "secret_access_key", "bucket" as well as the "region".
|
||||
|
||||
For Example:
|
||||
|
||||
```JSON
|
||||
{
|
||||
"access_key_id": "FJNAKNCLAVLRNBLAVVBK",
|
||||
"bucket": "vro-artloarj-ltnl-nnbv-ibnh-lbnlsnblltbn",
|
||||
"secret_access_key": "123467895896646438486316436kmdlcvreanvjk",
|
||||
"region": "eu-central-1"
|
||||
}
|
||||
```
|
||||
|
||||
If the JSON string contains additional information, this is not a problem. These are automatically detected and skipped.
|
||||
|
||||
## About Files/Directories to Upload
|
||||
|
||||
With the step awsS3Upload you can upload single files as well as whole directories into your S3 bucket. File formats do not matter and directory structures are preserved.
|
||||
|
||||
**Note:** File paths must be specified in UNIX format. So the used path separator must be "/".
|
||||
|
||||
## ${docGenParameters}
|
||||
|
||||
## ${docGenConfiguration}
|
||||
|
||||
## ${docJenkinsPluginDependencies}
|
||||
|
||||
## Example
|
||||
|
||||
```groovy
|
||||
awsS3Upload(
|
||||
script: this,
|
||||
awsCredentialsId: "AWS_Credentials",
|
||||
filePath: "test.txt"
|
||||
)
|
||||
```
|
@ -75,6 +75,7 @@ nav:
|
||||
- apiProviderUpload: steps/apiProviderUpload.md
|
||||
- apiProxyUpload: steps/apiProxyUpload.md
|
||||
- artifactPrepareVersion: steps/artifactPrepareVersion.md
|
||||
- awsS3Upload: steps/awsS3Upload.md
|
||||
- batsExecuteTests: steps/batsExecuteTests.md
|
||||
- buildExecute: steps/buildExecute.md
|
||||
- checkmarxExecuteScan: steps/checkmarxExecuteScan.md
|
||||
|
16
go.mod
16
go.mod
@ -7,6 +7,8 @@ require (
|
||||
github.com/Jeffail/gabs/v2 v2.6.1
|
||||
github.com/Masterminds/sprig v2.22.0+incompatible
|
||||
github.com/antchfx/htmlquery v1.2.4
|
||||
github.com/aws/aws-sdk-go-v2/config v1.15.3
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.26.3
|
||||
github.com/bmatcuk/doublestar v1.3.4
|
||||
github.com/bndr/gojenkins v1.1.1-0.20210520222939-90ed82bfdff6
|
||||
github.com/buildpacks/lifecycle v0.13.0
|
||||
@ -94,6 +96,20 @@ require (
|
||||
github.com/armon/go-radix v1.0.0 // indirect
|
||||
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect
|
||||
github.com/aws/aws-sdk-go v1.37.19 // indirect
|
||||
github.com/aws/aws-sdk-go-v2 v1.16.2 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.11.2 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.9 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.10 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.13.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.11.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.16.3 // indirect
|
||||
github.com/aws/smithy-go v1.11.2 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/bgentry/speakeasy v0.1.0 // indirect
|
||||
github.com/buildpacks/imgutil v0.0.0-20211001201950-cf7ae41c3771 // indirect
|
||||
|
39
go.sum
39
go.sum
@ -286,27 +286,52 @@ github.com/aws/aws-sdk-go v1.34.9/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU
|
||||
github.com/aws/aws-sdk-go v1.34.28/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48=
|
||||
github.com/aws/aws-sdk-go v1.37.19 h1:/xKHoSsYfH9qe16pJAHIjqTVpMM2DRSsEt8Ok1bzYiw=
|
||||
github.com/aws/aws-sdk-go v1.37.19/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
|
||||
github.com/aws/aws-sdk-go-v2 v1.8.0 h1:HcN6yDnHV9S7D69E7To0aUppJhiJNEzQSNcUxc7r3qo=
|
||||
github.com/aws/aws-sdk-go-v2 v1.8.0/go.mod h1:xEFuWz+3TYdlPRuo+CqATbeDWIWyaT5uAPwPaWtgse0=
|
||||
github.com/aws/aws-sdk-go-v2 v1.16.2 h1:fqlCk6Iy3bnCumtrLz9r3mJ/2gUT0pJ0wLFVIdWh+JA=
|
||||
github.com/aws/aws-sdk-go-v2 v1.16.2/go.mod h1:ytwTPBG6fXTZLxxeeCCWj2/EMYp/xDUgX+OET6TLNNU=
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.1 h1:SdK4Ppk5IzLs64ZMvr6MrSficMtjY2oS0WOORXTlxwU=
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.1/go.mod h1:n8Bs1ElDD2wJ9kCRTczA83gYbBmjSwZp3umc6zF4EeM=
|
||||
github.com/aws/aws-sdk-go-v2/config v1.6.0/go.mod h1:TNtBVmka80lRPk5+S9ZqVfFszOQAGJJ9KbT3EM3CHNU=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.3.2 h1:Uud/fZzm0lqqhE8kvXYJFAJ3PGnagKoUcvHq1hXfBZw=
|
||||
github.com/aws/aws-sdk-go-v2/config v1.15.3 h1:5AlQD0jhVXlGzwo+VORKiUuogkG7pQcLJNzIzK7eodw=
|
||||
github.com/aws/aws-sdk-go-v2/config v1.15.3/go.mod h1:9YL3v07Xc/ohTsxFXzan9ZpFpdTOFl4X65BAKYaz8jg=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.3.2/go.mod h1:PACKuTJdt6AlXvEq8rFI4eDmoqDFC5DpVKQbWysaDgM=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.11.2 h1:RQQ5fzclAKJyY5TvF+fkjJEwzK4hnxQCLOu5JXzDmQo=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.11.2/go.mod h1:j8YsY9TXTm31k4eFhspiQicfXPLZ0gYXA50i4gxPE8g=
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.4.0/go.mod h1:Mj/U8OpDbcVcoctrYwA2bak8k/HFPdcLzI/vaiXMwuM=
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.3 h1:LWPg5zjHV9oz/myQr4wMs0gi4CjnDN/ILmyZUFYXZsU=
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.12.3/go.mod h1:uk1vhHHERfSVCUnqSqz8O48LBYDSC+k6brng09jcMOk=
|
||||
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.4.0 h1:Iqp2aHeRF3kaaNuDS82bHBzER285NM6lLPAgsxHCR2A=
|
||||
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.4.0/go.mod h1:eHwXu2+uE/T6gpnYWwBwqoeqRf9IXyCcolyOWDRAErQ=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.9 h1:onz/VaaxZ7Z4V+WIN9Txly9XLTmoOh1oJ8XcAC3pako=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.9/go.mod h1:AnVH5pvai0pAF4lXRq0bmhbes1u9R8wTE+g+183bZNM=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.3 h1:9stUQR/u2KXU6HkFJYlqnZEjBnbgrVbG6I5HN09xZh0=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.3/go.mod h1:ssOhaLpRlh88H3UmEcsBoVKq309quMvm3Ds8e9d4eJM=
|
||||
github.com/aws/aws-sdk-go-v2/internal/ini v1.2.0/go.mod h1:Q5jATQc+f1MfZp3PDMhn6ry18hGvE0i8yvbXoKbnZaE=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.2.2 h1:YcGVEqLQGHDa81776C3daai6ZkkRGf/8RAQ07hV0QcU=
|
||||
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.10 h1:by9P+oy3P/CwggN4ClnW2D4oL91QV7pBzBICi1chZvQ=
|
||||
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.10/go.mod h1:8DcYQcz0+ZJaSxANlHIsbbi6S+zMwjwdDqwW3r9AzaE=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.2.2/go.mod h1:EASdTcM1lGhUe1/p4gkojHwlGJkeoRjjr1sRCzup3Is=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.2.2 h1:Xv1rGYgsRRn0xw9JFNnfpBMZam54PrWpC4rJOJ9koA8=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.1 h1:T4pFel53bkHjL2mMo+4DKE6r6AuoZnM0fg7k1/ratr4=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.1/go.mod h1:GeUru+8VzrTXV/83XyMJ80KpH8xO89VPoUileyNQ+tc=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.3 h1:I0dcwWitE752hVSMrsLCxqNQ+UdEp3nACx2bYNMQq+k=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.3/go.mod h1:Seb8KNmD6kVTjwRjVEgOT5hPin6sq+v4C2ycJQDwuH8=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.2.2/go.mod h1:NXmNI41bdEsJMrD0v9rUvbGCB5GwdBEpKvUvIY3vTFg=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.5.2 h1:ewIpdVz12MDinJJB/nu1uUiFIWFnvtd3iV7cEW7lR+M=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.3 h1:Gh1Gpyh01Yvn7ilO/b/hr01WgNpaszfbKMUgqM186xQ=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.3/go.mod h1:wlY6SVjuwvh3TVRpTqdy4I1JpBFLX4UGeKZdWntaocw=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.5.2/go.mod h1:QuL2Ym8BkrLmN4lUofXYq6000/i5jPjosCNK//t6gak=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.12.0 h1:cxZbzTYXgiQrZ6u2/RJZAkkgZssqYOdydvJPBgIHlsM=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.13.3 h1:BKjwCJPnANbkwQ8vzSbaZDKawwagDubrH/z/c0X+kbQ=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.13.3/go.mod h1:Bm/v2IaN6rZ+Op7zX+bOUMdL4fsrYZiD0dsjLhNKwZc=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.12.0/go.mod h1:6J++A5xpo7QDsIeSqPK4UHqMSyPOCopa+zKtqAMhqVQ=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.26.3 h1:rMPtwA7zzkSQZhhz9U3/SoIDz/NZ7Q+iRn4EIO8rSyU=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.26.3/go.mod h1:g1qvDuRsJY+XghsV6zg00Z4KJ7DtFFCx8fJD2a491Ak=
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.3.2/go.mod h1:J21I6kF+d/6XHVk7kp/cx9YVD2TMD2TbLwtRGVcinXo=
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.11.3 h1:frW4ikGcxfAEDfmQqWgMLp+F1n4nRo9sF39OcIb5BkQ=
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.11.3/go.mod h1:7UQ/e69kU7LDPtY40OyoHYgRmgfGM4mgsLYtcObdveU=
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.6.1/go.mod h1:hLZ/AnkIKHLuPGjEiyghNEdvJ2PP0MgOxcmv9EBJ4xs=
|
||||
github.com/aws/smithy-go v1.7.0 h1:+cLHMRrDZvQ4wk+KuQ9yH6eEg6KZEJ9RI2IkDqnygCg=
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.16.3 h1:cJGRyzCSVwZC7zZZ1xbx9m32UnrKydRYhOvcD1NYP9Q=
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.16.3/go.mod h1:bfBj0iVmsUyUg4weDB4NxktD9rDGeKSVWnjTnwbx9b8=
|
||||
github.com/aws/smithy-go v1.7.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E=
|
||||
github.com/aws/smithy-go v1.11.2 h1:eG/N+CcUMAvsdffgMvjMKwfyDzIkjM6pfxMJ8Mzc6mE=
|
||||
github.com/aws/smithy-go v1.11.2/go.mod h1:3xHYmszWVx2c0kIwQeEVf9uSm4fYZt67FBJnwub1bgM=
|
||||
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59/go.mod h1:q/89r3U2H7sSsE2t6Kca0lfwTK8JdoNGS/yzM/4iH5I=
|
||||
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
|
||||
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f/go.mod h1:AuiFmCCPBSrqvVMvuqFuk0qogytodnVFVSN5CeJB8Gc=
|
||||
|
34
resources/metadata/awsS3Upload.yaml
Normal file
34
resources/metadata/awsS3Upload.yaml
Normal file
@ -0,0 +1,34 @@
|
||||
metadata:
|
||||
name: awsS3Upload
|
||||
description: "Uploads a specified file or directory into a given AWS S3 Bucket"
|
||||
longDescription: |
|
||||
Uploads a specified file or directory as S3 Objects into a given AWS S3 Bucket.
|
||||
In case a file is uploaded that is already contained in the S3 bucket, it will be overwritten with the latest version.
|
||||
spec:
|
||||
inputs:
|
||||
secrets:
|
||||
- name: awsCredentialsId
|
||||
description: Jenkins 'Secret Text' credentials ID containing the JSON file to authenticate to the AWS S3 Bucket
|
||||
type: jenkins
|
||||
params:
|
||||
- name: jsonCredentialsAWS
|
||||
description: JSON String Credentials to access AWS S3 Bucket
|
||||
type: string
|
||||
mandatory: true
|
||||
scope:
|
||||
- PARAMETERS
|
||||
secret: true
|
||||
resourceRef:
|
||||
- name: awsCredentialsId
|
||||
type: secret
|
||||
- name: filePath
|
||||
resourceRef:
|
||||
- name: commonPipelineEnvironment
|
||||
param: mtarFilePath
|
||||
type: string
|
||||
mandatory: true
|
||||
description: "Name/Path of the file which should be uploaded"
|
||||
scope:
|
||||
- PARAMETERS
|
||||
- STAGES
|
||||
- STEPS
|
@ -215,6 +215,7 @@ public class CommonStepsTest extends BasePiperTest{
|
||||
'apiKeyValueMapUpload', //implementing new golang pattern without fields
|
||||
'apiProviderUpload', //implementing new golang pattern without fields
|
||||
'pythonBuild', //implementing new golang pattern without fields
|
||||
'awsS3Upload'
|
||||
]
|
||||
|
||||
@Test
|
||||
|
15
vars/awsS3Upload.groovy
Normal file
15
vars/awsS3Upload.groovy
Normal file
@ -0,0 +1,15 @@
|
||||
import groovy.transform.Field
|
||||
import static com.sap.piper.Prerequisites.checkScript
|
||||
|
||||
@Field String STEP_NAME = getClass().getName()
|
||||
@Field String METADATA_FILE = 'metadata/awsS3Upload.yaml'
|
||||
|
||||
void call(Map parameters = [:]) {
|
||||
def script = checkScript(this, parameters) ?: this
|
||||
|
||||
List credentials = [
|
||||
[type: 'token', id: 'awsCredentialsId', env: ['PIPER_jsonCredentialsAWS']]
|
||||
]
|
||||
|
||||
piperExecuteBin(parameters, STEP_NAME, METADATA_FILE, credentials)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user