2020-07-10 08:07:59 +02:00
// Code generated by piper's step-generator. DO NOT EDIT.
package cmd
import (
"fmt"
"os"
2020-10-14 11:13:08 +02:00
"path/filepath"
2020-07-10 08:07:59 +02:00
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
2020-10-14 11:13:08 +02:00
"github.com/SAP/jenkins-library/pkg/piperenv"
2021-05-17 12:14:04 +02:00
"github.com/SAP/jenkins-library/pkg/splunk"
2020-07-10 08:07:59 +02:00
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/spf13/cobra"
)
type kanikoExecuteOptions struct {
BuildOptions [ ] string ` json:"buildOptions,omitempty" `
ContainerBuildOptions string ` json:"containerBuildOptions,omitempty" `
ContainerImage string ` json:"containerImage,omitempty" `
2020-10-14 11:13:08 +02:00
ContainerImageName string ` json:"containerImageName,omitempty" `
ContainerImageTag string ` json:"containerImageTag,omitempty" `
2020-07-10 08:07:59 +02:00
ContainerPreparationCommand string ` json:"containerPreparationCommand,omitempty" `
2020-10-14 11:13:08 +02:00
ContainerRegistryURL string ` json:"containerRegistryUrl,omitempty" `
2020-07-10 08:07:59 +02:00
CustomTLSCertificateLinks [ ] string ` json:"customTlsCertificateLinks,omitempty" `
DockerConfigJSON string ` json:"dockerConfigJSON,omitempty" `
DockerfilePath string ` json:"dockerfilePath,omitempty" `
}
2020-10-14 11:13:08 +02:00
type kanikoExecuteCommonPipelineEnvironment struct {
container struct {
registryURL string
imageNameTag string
}
}
func ( p * kanikoExecuteCommonPipelineEnvironment ) persist ( path , resourceName string ) {
content := [ ] struct {
category string
name string
value interface { }
} {
{ category : "container" , name : "registryUrl" , value : p . container . registryURL } ,
{ category : "container" , name : "imageNameTag" , value : p . container . imageNameTag } ,
}
errCount := 0
for _ , param := range content {
err := piperenv . SetResourceParameter ( path , resourceName , filepath . Join ( param . category , param . name ) , param . value )
if err != nil {
log . Entry ( ) . WithError ( err ) . Error ( "Error persisting piper environment." )
errCount ++
}
}
if errCount > 0 {
log . Entry ( ) . Fatal ( "failed to persist Piper environment" )
}
}
2020-07-10 08:07:59 +02:00
// KanikoExecuteCommand Executes a [Kaniko](https://github.com/GoogleContainerTools/kaniko) build for creating a Docker container.
func KanikoExecuteCommand ( ) * cobra . Command {
const STEP_NAME = "kanikoExecute"
metadata := kanikoExecuteMetadata ( )
var stepConfig kanikoExecuteOptions
var startTime time . Time
2020-10-14 11:13:08 +02:00
var commonPipelineEnvironment kanikoExecuteCommonPipelineEnvironment
2021-05-17 12:14:04 +02:00
var logCollector * log . CollectorHook
2020-07-10 08:07:59 +02:00
var createKanikoExecuteCmd = & cobra . Command {
Use : STEP_NAME ,
Short : "Executes a [Kaniko](https://github.com/GoogleContainerTools/kaniko) build for creating a Docker container." ,
Long : ` Executes a [Kaniko](https://github.com/GoogleContainerTools/kaniko) build for creating a Docker container. ` ,
PreRunE : func ( cmd * cobra . Command , _ [ ] string ) error {
startTime = time . Now ( )
log . SetStepName ( STEP_NAME )
log . SetVerbose ( GeneralConfig . Verbose )
2021-07-08 15:26:07 +02:00
GeneralConfig . GitHubAccessTokens = ResolveAccessTokens ( GeneralConfig . GitHubTokens )
2020-07-10 08:07:59 +02:00
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 . DockerConfigJSON )
if len ( GeneralConfig . HookConfig . SentryConfig . Dsn ) > 0 {
sentryHook := log . NewSentryHook ( GeneralConfig . HookConfig . SentryConfig . Dsn , GeneralConfig . CorrelationID )
log . RegisterHook ( & sentryHook )
}
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
logCollector = & log . CollectorHook { CorrelationID : GeneralConfig . CorrelationID }
log . RegisterHook ( logCollector )
}
2020-07-10 08:07:59 +02:00
return nil
} ,
Run : func ( _ * cobra . Command , _ [ ] string ) {
telemetryData := telemetry . CustomData { }
telemetryData . ErrorCode = "1"
handler := func ( ) {
2020-10-26 15:20:04 +02:00
config . RemoveVaultSecretFiles ( )
2020-10-14 11:13:08 +02:00
commonPipelineEnvironment . persist ( GeneralConfig . EnvRootPath , "commonPipelineEnvironment" )
2020-07-10 08:07:59 +02:00
telemetryData . Duration = fmt . Sprintf ( "%v" , time . Since ( startTime ) . Milliseconds ( ) )
2020-09-29 13:49:40 +02:00
telemetryData . ErrorCategory = log . GetErrorCategory ( ) . String ( )
2020-07-10 08:07:59 +02:00
telemetry . Send ( & telemetryData )
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunk . Send ( & telemetryData , logCollector )
}
2020-07-10 08:07:59 +02:00
}
log . DeferExitHandler ( handler )
defer handler ( )
telemetry . Initialize ( GeneralConfig . NoTelemetry , STEP_NAME )
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunk . Initialize ( GeneralConfig . CorrelationID ,
GeneralConfig . HookConfig . SplunkConfig . Dsn ,
GeneralConfig . HookConfig . SplunkConfig . Token ,
GeneralConfig . HookConfig . SplunkConfig . Index ,
GeneralConfig . HookConfig . SplunkConfig . SendLogs )
}
2020-10-14 11:13:08 +02:00
kanikoExecute ( stepConfig , & telemetryData , & commonPipelineEnvironment )
2020-07-10 08:07:59 +02:00
telemetryData . ErrorCode = "0"
log . Entry ( ) . Info ( "SUCCESS" )
} ,
}
addKanikoExecuteFlags ( createKanikoExecuteCmd , & stepConfig )
return createKanikoExecuteCmd
}
func addKanikoExecuteFlags ( cmd * cobra . Command , stepConfig * kanikoExecuteOptions ) {
cmd . Flags ( ) . StringSliceVar ( & stepConfig . BuildOptions , "buildOptions" , [ ] string { ` --skip-tls-verify-pull ` } , "Defines a list of build options for the [kaniko](https://github.com/GoogleContainerTools/kaniko) build." )
cmd . Flags ( ) . StringVar ( & stepConfig . ContainerBuildOptions , "containerBuildOptions" , os . Getenv ( "PIPER_containerBuildOptions" ) , "Deprected, please use buildOptions. Defines the build options for the [kaniko](https://github.com/GoogleContainerTools/kaniko) build." )
cmd . Flags ( ) . StringVar ( & stepConfig . ContainerImage , "containerImage" , os . Getenv ( "PIPER_containerImage" ) , "Defines the full name of the Docker image to be created including registry, image name and tag like `my.docker.registry/path/myImageName:myTag`. If left empty, image will not be pushed." )
2020-10-14 11:13:08 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . ContainerImageName , "containerImageName" , os . Getenv ( "PIPER_containerImageName" ) , "Name of the container which will be built - will be used instead of parameter `containerImage`" )
cmd . Flags ( ) . StringVar ( & stepConfig . ContainerImageTag , "containerImageTag" , os . Getenv ( "PIPER_containerImageTag" ) , "Tag of the container which will be built - will be used instead of parameter `containerImage`" )
2020-07-10 08:07:59 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . ContainerPreparationCommand , "containerPreparationCommand" , ` rm -f /kaniko/.docker/config.json ` , "Defines the command to prepare the Kaniko container. By default the contained credentials are removed in order to allow anonymous access to container registries." )
2020-10-14 11:13:08 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . ContainerRegistryURL , "containerRegistryUrl" , os . Getenv ( "PIPER_containerRegistryUrl" ) , "http(s) url of the Container registry where the image should be pushed to - will be used instead of parameter `containerImage`" )
2020-07-10 08:07:59 +02:00
cmd . Flags ( ) . StringSliceVar ( & stepConfig . CustomTLSCertificateLinks , "customTlsCertificateLinks" , [ ] string { } , "List containing download links of custom TLS certificates. This is required to ensure trusted connections to registries with custom certificates." )
cmd . Flags ( ) . StringVar ( & stepConfig . DockerConfigJSON , "dockerConfigJSON" , os . Getenv ( "PIPER_dockerConfigJSON" ) , "Path to the file `.docker/config.json` - this is typically provided by your CI/CD system. You can find more details about the Docker credentials in the [Docker documentation](https://docs.docker.com/engine/reference/commandline/login/)." )
cmd . Flags ( ) . StringVar ( & stepConfig . DockerfilePath , "dockerfilePath" , ` Dockerfile ` , "Defines the location of the Dockerfile relative to the Jenkins workspace." )
}
// retrieve step metadata
func kanikoExecuteMetadata ( ) config . StepData {
var theMetaData = config . StepData {
Metadata : config . StepMetadata {
2020-11-20 09:13:59 +02:00
Name : "kanikoExecute" ,
Aliases : [ ] config . Alias { } ,
Description : "Executes a [Kaniko](https://github.com/GoogleContainerTools/kaniko) build for creating a Docker container." ,
2020-07-10 08:07:59 +02:00
} ,
Spec : config . StepSpec {
Inputs : config . StepInputs {
2021-06-16 08:43:30 +02:00
Secrets : [ ] config . StepSecrets {
{ Name : "dockerConfigJsonCredentialsId" , Description : "Jenkins 'Secret file' credentials ID containing Docker config.json (with registry credential(s)). You can create it like explained in the Docker Success Center in the article about [how to generate a new auth in the config.json file](https://success.docker.com/article/generate-new-auth-in-config-json-file)." , Type : "jenkins" } ,
} ,
2020-07-10 08:07:59 +02:00
Parameters : [ ] config . StepParameters {
{
Name : "buildOptions" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : [ ] string { ` --skip-tls-verify-pull ` } ,
2020-07-10 08:07:59 +02:00
} ,
{
Name : "containerBuildOptions" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_containerBuildOptions" ) ,
2020-07-10 08:07:59 +02:00
} ,
{
2020-10-14 11:13:08 +02:00
Name : "containerImage" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { { Name : "containerImageNameAndTag" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_containerImage" ) ,
2020-10-14 11:13:08 +02:00
} ,
{
Name : "containerImageName" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { { Name : "dockerImageName" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_containerImageName" ) ,
2020-10-14 11:13:08 +02:00
} ,
{
Name : "containerImageTag" ,
2020-09-16 14:50:09 +02:00
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
2020-10-14 11:13:08 +02:00
Param : "artifactVersion" ,
2020-09-16 14:50:09 +02:00
} ,
} ,
2020-10-14 11:13:08 +02:00
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
2020-09-16 14:50:09 +02:00
Type : "string" ,
Mandatory : false ,
2020-10-14 11:13:08 +02:00
Aliases : [ ] config . Alias { { Name : "artifactVersion" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_containerImageTag" ) ,
2020-07-10 08:07:59 +02:00
} ,
{
Name : "containerPreparationCommand" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : ` rm -f /kaniko/.docker/config.json ` ,
2020-07-10 08:07:59 +02:00
} ,
2020-10-14 11:13:08 +02:00
{
Name : "containerRegistryUrl" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
Param : "container/registryUrl" ,
} ,
} ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { { Name : "dockerRegistryUrl" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_containerRegistryUrl" ) ,
2020-10-14 11:13:08 +02:00
} ,
2020-07-10 08:07:59 +02:00
{
Name : "customTlsCertificateLinks" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : [ ] string { } ,
2020-07-10 08:07:59 +02:00
} ,
{
2020-09-16 14:50:09 +02:00
Name : "dockerConfigJSON" ,
ResourceRef : [ ] config . ResourceReference {
2021-04-01 09:47:24 +02:00
{
Name : "commonPipelineEnvironment" ,
Param : "custom/dockerConfigJSON" ,
} ,
2020-09-16 14:50:09 +02:00
{
Name : "dockerConfigJsonCredentialsId" ,
Type : "secret" ,
} ,
2020-11-06 19:06:19 +02:00
{
Name : "" ,
Paths : [ ] string { "$(vaultPath)/docker-config" , "$(vaultBasePath)/$(vaultPipelineName)/docker-config" , "$(vaultBasePath)/GROUP-SECRETS/docker-config" } ,
Type : "vaultSecretFile" ,
} ,
2020-09-16 14:50:09 +02:00
} ,
2021-06-14 15:42:23 +02:00
Scope : [ ] string { "PARAMETERS" } ,
2020-09-16 14:50:09 +02:00
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_dockerConfigJSON" ) ,
2020-07-10 08:07:59 +02:00
} ,
{
Name : "dockerfilePath" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { { Name : "dockerfile" } } ,
2021-06-16 08:43:30 +02:00
Default : ` Dockerfile ` ,
2020-07-10 08:07:59 +02:00
} ,
} ,
} ,
2020-11-20 09:13:59 +02:00
Containers : [ ] config . Container {
2021-06-16 16:37:39 +02:00
{ Image : "gcr.io/kaniko-project/executor:v1.3.0-debug" , Options : [ ] config . Option { { Name : "-u" , Value : "0" } , { Name : "--entrypoint" , Value : "''" } } } ,
2020-11-20 09:13:59 +02:00
} ,
Outputs : config . StepOutputs {
Resources : [ ] config . StepResources {
{
Name : "commonPipelineEnvironment" ,
Type : "piperEnvironment" ,
Parameters : [ ] map [ string ] interface { } {
{ "Name" : "container/registryUrl" } ,
{ "Name" : "container/imageNameTag" } ,
} ,
} ,
} ,
} ,
2020-07-10 08:07:59 +02:00
} ,
}
return theMetaData
}