1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-04 04:07:16 +02:00

enforce that step metadata yaml file is called <step>.yaml (#3226)

Co-authored-by: Philipp Stehle <philipp.stehle@sap.com>
This commit is contained in:
Philipp Stehle 2021-11-15 14:20:20 +01:00 committed by GitHub
parent 7be9b2b038
commit f9f0cbfd33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
50 changed files with 51 additions and 47 deletions

2
.github/CODEOWNERS vendored
View File

@ -41,7 +41,7 @@
##### Sonar #####
/pkg/sonar/ @CCFenner
/resources/metadata/sonar.yaml @CCFenner
/resources/metadata/sonarExecuteScan.yaml @CCFenner
/vars/sonarExecuteScan.groovy @CCFenner
/cmd/sonarExecuteScan.go @CCFenner
/cmd/sonarExecuteScan_test.go @CCFenner

View File

@ -115,15 +115,15 @@ Examples are:
There are certain extensions:
* **aliases** allow alternative parameter names also supporting deeper configuration structures. [Example](https://github.com/SAP/jenkins-library/blob/master/resources/metadata/kubernetesdeploy.yaml)
* **resources** allow to read for example from a shared `commonPipelineEnvironment` which contains information which has been provided by a previous step in the pipeline via an output. [Example](https://github.com/SAP/jenkins-library/blob/master/resources/metadata/githubrelease.yaml)
* **secrets** allow to specify references to Jenkins credentials which can be used in the `groovy` library. [Example](https://github.com/SAP/jenkins-library/blob/master/resources/metadata/kubernetesdeploy.yaml)
* **aliases** allow alternative parameter names also supporting deeper configuration structures. [Example](https://github.com/SAP/jenkins-library/blob/master/resources/metadata/kubernetesDeploy.yaml)
* **resources** allow to read for example from a shared `commonPipelineEnvironment` which contains information which has been provided by a previous step in the pipeline via an output. [Example](https://github.com/SAP/jenkins-library/blob/master/resources/metadata/githubPublishRelease.yaml)
* **secrets** allow to specify references to Jenkins credentials which can be used in the `groovy` library. [Example](https://github.com/SAP/jenkins-library/blob/master/resources/metadata/kubernetesDeploy.yaml)
* **outputs** allow to write to dedicated outputs like
* Influx metrics. [Example](https://github.com/SAP/jenkins-library/blob/master/resources/metadata/checkmarx.yaml)
* Influx metrics. [Example](https://github.com/SAP/jenkins-library/blob/master/resources/metadata/checkmarxExecuteScan.yaml)
* Sharing data via `commonPipelineEnvironment` which can be used by another step as input
* **conditions** allow for example to specify in which case a certain container is used (depending on a configuration parameter). [Example](https://github.com/SAP/jenkins-library/blob/master/resources/metadata/kubernetesdeploy.yaml)
* **conditions** allow for example to specify in which case a certain container is used (depending on a configuration parameter). [Example](https://github.com/SAP/jenkins-library/blob/master/resources/metadata/kubernetesDeploy.yaml)
## Best practices for writing piper-go steps

View File

@ -24,6 +24,7 @@ func GetAllStepMetadata() map[string]config.StepData {
"abapEnvironmentRunAUnitTest": abapEnvironmentRunAUnitTestMetadata(),
"apiKeyValueMapDownload": apiKeyValueMapDownloadMetadata(),
"apiProxyDownload": apiProxyDownloadMetadata(),
"artifactPrepareVersion": artifactPrepareVersionMetadata(),
"batsExecuteTests": batsExecuteTestsMetadata(),
"checkmarxExecuteScan": checkmarxExecuteScanMetadata(),
"cloudFoundryCreateService": cloudFoundryCreateServiceMetadata(),
@ -34,6 +35,7 @@ func GetAllStepMetadata() map[string]config.StepData {
"cloudFoundryDeploy": cloudFoundryDeployMetadata(),
"cnbBuild": cnbBuildMetadata(),
"containerExecuteStructureTests": containerExecuteStructureTestsMetadata(),
"containerSaveImage": containerSaveImageMetadata(),
"detectExecuteScan": detectExecuteScanMetadata(),
"fortifyExecuteScan": fortifyExecuteScanMetadata(),
"gaugeExecuteTests": gaugeExecuteTestsMetadata(),
@ -77,7 +79,6 @@ func GetAllStepMetadata() map[string]config.StepData {
"npmExecuteScripts": npmExecuteScriptsMetadata(),
"pipelineCreateScanSummary": pipelineCreateScanSummaryMetadata(),
"protecodeExecuteScan": protecodeExecuteScanMetadata(),
"containerSaveImage": containerSaveImageMetadata(),
"sonarExecuteScan": sonarExecuteScanMetadata(),
"terraformExecute": terraformExecuteMetadata(),
"transportRequestDocIDFromGit": transportRequestDocIDFromGitMetadata(),
@ -87,7 +88,6 @@ func GetAllStepMetadata() map[string]config.StepData {
"transportRequestUploadSOLMAN": transportRequestUploadSOLMANMetadata(),
"uiVeri5ExecuteTests": uiVeri5ExecuteTestsMetadata(),
"vaultRotateSecretId": vaultRotateSecretIdMetadata(),
"artifactPrepareVersion": artifactPrepareVersionMetadata(),
"whitesourceExecuteScan": whitesourceExecuteScanMetadata(),
"xsDeploy": xsDeployMetadata(),
}

View File

@ -484,6 +484,10 @@ func ProcessMetaFiles(metadataFiles []string, targetDir string, stepHelperData S
stepName := stepData.Metadata.Name
fmt.Printf("Step name: %v\n", stepName)
if stepName+".yaml" != filepath.Base(configFilePath) {
fmt.Printf("Expected file %s to have name %s.yaml (<stepName>.yaml)\n", configFilePath, filepath.Join(filepath.Dir(configFilePath), stepName))
os.Exit(1)
}
allSteps.Steps = append(allSteps.Steps, stepName)
for _, parameter := range stepData.Spec.Inputs.Parameters {

View File

@ -89,7 +89,7 @@ spec:
`
var r string
switch name {
case "test.yaml":
case "testStep.yaml":
r = meta1
default:
r = ""
@ -110,7 +110,7 @@ func writeFileMock(filename string, data []byte, perm os.FileMode) error {
func TestProcessMetaFiles(t *testing.T) {
stepHelperData := StepHelperData{configOpenFileMock, writeFileMock, ""}
ProcessMetaFiles([]string{"test.yaml"}, "./cmd", stepHelperData)
ProcessMetaFiles([]string{"testStep.yaml"}, "./cmd", stepHelperData)
t.Run("step code", func(t *testing.T) {
goldenFilePath := filepath.Join("testdata", t.Name()+"_generated.golden")
@ -135,7 +135,7 @@ func TestProcessMetaFiles(t *testing.T) {
t.Run("custom step code", func(t *testing.T) {
stepHelperData = StepHelperData{configOpenFileMock, writeFileMock, "piperOsCmd"}
ProcessMetaFiles([]string{"test.yaml"}, "./cmd", stepHelperData)
ProcessMetaFiles([]string{"testStep.yaml"}, "./cmd", stepHelperData)
goldenFilePath := filepath.Join("testdata", t.Name()+"_generated.golden")
expected, err := ioutil.ReadFile(goldenFilePath)

View File

@ -54,7 +54,7 @@ class CheckmarxExecuteScanTest extends BasePiperTest {
return closure()
})
credentialsRule.withCredentials('idOfCxCredential', "PIPER_username", "PIPER_password")
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/checkmarx.yaml\'', '{"checkmarxCredentialsId": "idOfCxCredential", "verbose": false}')
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/checkmarxExecuteScan.yaml\'', '{"checkmarxCredentialsId": "idOfCxCredential", "verbose": false}')
helper.registerAllowedMethod('findFiles', [Map.class], {return null})
helper.registerAllowedMethod("writePipelineEnv", [Map.class], {m -> return })
@ -70,7 +70,7 @@ class CheckmarxExecuteScanTest extends BasePiperTest {
script: nullScript
)
// asserts
assertThat(writeFileRule.files['.pipeline/tmp/metadata/checkmarx.yaml'], containsString('name: checkmarxExecuteScan'))
assertThat(writeFileRule.files['.pipeline/tmp/metadata/checkmarxExecuteScan.yaml'], containsString('name: checkmarxExecuteScan'))
assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'), containsString('"testParam":"This is test content"')))
assertThat(shellCallRule.shell[1], is('./piper checkmarxExecuteScan'))
}

View File

@ -49,7 +49,7 @@ class GithubPublishReleaseTest extends BasePiperTest {
return closure()
})
credentialsRule.withCredentials('githubTokenId', 'thisIsATestToken')
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/githubrelease.yaml\'', '{"githubTokenCredentialsId":"githubTokenId"}')
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/githubPublishRelease.yaml\'', '{"githubTokenCredentialsId":"githubTokenId"}')
helper.registerAllowedMethod("writePipelineEnv", [Map.class], {m -> return })
helper.registerAllowedMethod("readPipelineEnv", [Map.class], {m -> return })
}
@ -64,7 +64,7 @@ class GithubPublishReleaseTest extends BasePiperTest {
testParam: "This is test content"
)
// asserts
assertThat(writeFileRule.files['.pipeline/tmp/metadata/githubrelease.yaml'], containsString('name: githubPublishRelease'))
assertThat(writeFileRule.files['.pipeline/tmp/metadata/githubPublishRelease.yaml'], containsString('name: githubPublishRelease'))
assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'), containsString('"testParam":"This is test content"')))
assertThat(shellCallRule.shell[1], is('./piper githubPublishRelease'))
}

View File

@ -75,7 +75,7 @@ class KubernetesDeployTest extends BasePiperTest {
@Test
void testKubernetesDeployAllCreds() {
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/kubernetesdeploy.yaml\'', '{"kubeConfigFileCredentialsId":"kubeConfig", "kubeTokenCredentialsId":"kubeToken", "dockerCredentialsId":"dockerCredentials", "dockerImage":"my.Registry/K8S:latest"}')
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/kubernetesDeploy.yaml\'', '{"kubeConfigFileCredentialsId":"kubeConfig", "kubeTokenCredentialsId":"kubeToken", "dockerCredentialsId":"dockerCredentials", "dockerImage":"my.Registry/K8S:latest"}')
stepRule.step.kubernetesDeploy(
juStabUtils: utils,
@ -84,7 +84,7 @@ class KubernetesDeployTest extends BasePiperTest {
script: nullScript
)
// asserts
assertThat(writeFileRule.files['.pipeline/tmp/metadata/kubernetesdeploy.yaml'], containsString('name: kubernetesDeploy'))
assertThat(writeFileRule.files['.pipeline/tmp/metadata/kubernetesDeploy.yaml'], containsString('name: kubernetesDeploy'))
assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'), containsString('"testParam":"This is test content"')))
assertThat(shellCallRule.shell[1], is('./piper kubernetesDeploy'))
assertThat(credentials.size(), is(3))
@ -94,7 +94,7 @@ class KubernetesDeployTest extends BasePiperTest {
@Test
void testKubernetesDeploySomeCreds() {
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/kubernetesdeploy.yaml\'', '{"kubeTokenCredentialsId":"kubeToken", "dockerCredentialsId":"dockerCredentials"}')
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/kubernetesDeploy.yaml\'', '{"kubeTokenCredentialsId":"kubeToken", "dockerCredentialsId":"dockerCredentials"}')
stepRule.step.kubernetesDeploy(
juStabUtils: utils,
jenkinsUtilsStub: jenkinsUtils,

View File

@ -71,7 +71,7 @@ class MavenExecuteStaticCodeChecksTest extends BasePiperTest {
-> pmdParserStepCalled = true
})
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/mavenStaticCodeChecks.yaml\'', '{"dockerImage": "maven:3.6-jdk-8"}')
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/mavenExecuteStaticCodeChecks.yaml\'', '{"dockerImage": "maven:3.6-jdk-8"}')
helper.registerAllowedMethod('findFiles', [Map.class], {return null})
helper.registerAllowedMethod("writePipelineEnv", [Map.class], {m -> return })
@ -91,7 +91,7 @@ class MavenExecuteStaticCodeChecksTest extends BasePiperTest {
script: nullScript
)
assertThat(writeFileRule.files['.pipeline/tmp/metadata/mavenStaticCodeChecks.yaml'], containsString('name: mavenExecuteStaticCodeChecks'))
assertThat(writeFileRule.files['.pipeline/tmp/metadata/mavenExecuteStaticCodeChecks.yaml'], containsString('name: mavenExecuteStaticCodeChecks'))
assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'), containsString('"testParam":"This is test content"')))
assertThat(shellCallRule.shell[shellCallRule.shell.size() -1], is('./piper mavenExecuteStaticCodeChecks'))
assertTrue(spotBugsStepCalled)
@ -112,7 +112,7 @@ class MavenExecuteStaticCodeChecksTest extends BasePiperTest {
script: nullScript
)
assertThat(writeFileRule.files['.pipeline/tmp/metadata/mavenStaticCodeChecks.yaml'], containsString('name: mavenExecuteStaticCodeChecks'))
assertThat(writeFileRule.files['.pipeline/tmp/metadata/mavenExecuteStaticCodeChecks.yaml'], containsString('name: mavenExecuteStaticCodeChecks'))
assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'), containsString('"testParam":"This is test content"')))
assertThat(shellCallRule.shell[shellCallRule.shell.size() -1], is('./piper mavenExecuteStaticCodeChecks'))
assertFalse(spotBugsStepCalled)

View File

@ -15,7 +15,7 @@ class ContainerMapTest extends BasePiperTest {
String exampleConfigYaml = """
# Mapping of Go step names to their YAML metadata resource file
stepMetadata:
artifactPrepareVersion: versioning.yaml
artifactPrepareVersion: artifactPrepareVersion.yaml
containerMaps:
init:
- artifactPrepareVersion
@ -54,7 +54,7 @@ containerMaps:
]
])
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/versioning.yaml\'', '{"dockerImage":"artifact-image"}')
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/artifactPrepareVersion.yaml\'', '{"dockerImage":"artifact-image"}')
}
@Test

View File

@ -5,7 +5,7 @@ import groovy.transform.Field
import static com.sap.piper.Prerequisites.checkScript
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/versioning.yaml'
@Field String METADATA_FILE = 'metadata/artifactPrepareVersion.yaml'
void call(Map parameters = [:]) {
final script = checkScript(this, parameters) ?: this

View File

@ -1,9 +1,9 @@
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/checkmarx.yaml'
@Field String METADATA_FILE = 'metadata/checkmarxExecuteScan.yaml'
//Metadata maintained in file project://resources/metadata/checkmarx.yaml
//Metadata maintained in file project://resources/metadata/checkmarxExecuteScan.yaml
void call(Map parameters = [:]) {
List credentials = [[type: 'usernamePassword', id: 'checkmarxCredentialsId', env: ['PIPER_username', 'PIPER_password']]]

View File

@ -1,9 +1,9 @@
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/savecontainer.yaml'
@Field String METADATA_FILE = 'metadata/containerSaveImage.yaml'
//Metadata maintained in file project://resources/metadata/savecontainer.yaml
//Metadata maintained in file project://resources/metadata/containerSaveImage.yaml
void call(Map parameters = [:]) {
List credentials = []

View File

@ -5,7 +5,7 @@ import groovy.transform.Field
import static com.sap.piper.Prerequisites.checkScript
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/detect.yaml'
@Field String METADATA_FILE = 'metadata/detectExecuteScan.yaml'
void call(Map parameters = [:]) {
final script = checkScript(this, parameters) ?: this

View File

@ -5,9 +5,9 @@ import groovy.transform.Field
import static com.sap.piper.Prerequisites.checkScript
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/fortify.yaml'
@Field String METADATA_FILE = 'metadata/fortifyExecuteScan.yaml'
//Metadata maintained in file project://resources/metadata/fortify.yaml
//Metadata maintained in file project://resources/metadata/fortifyExecuteScan.yaml
void call(Map parameters = [:]) {
final script = checkScript(this, parameters) ?: this

View File

@ -2,7 +2,7 @@ import static com.sap.piper.Prerequisites.checkScript
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/gauge.yaml'
@Field String METADATA_FILE = 'metadata/gaugeExecuteTests.yaml'
void call(Map parameters = [:]) {
List credentials = [

View File

@ -1,7 +1,7 @@
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/githubbranchprotection.yaml'
@Field String METADATA_FILE = 'metadata/githubCheckBranchProtection.yaml'
void call(Map parameters = [:]) {
List credentials = [

View File

@ -1,7 +1,7 @@
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/githubcommentissue.yaml'
@Field String METADATA_FILE = 'metadata/githubCommentIssue.yaml'
void call(Map parameters = [:]) {
List credentials = [

View File

@ -1,7 +1,7 @@
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/githubcreateissue.yaml'
@Field String METADATA_FILE = 'metadata/githubCreateIssue.yaml'
void call(Map parameters = [:]) {
List credentials = [

View File

@ -1,7 +1,7 @@
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/githubrelease.yaml'
@Field String METADATA_FILE = 'metadata/githubPublishRelease.yaml'
void call(Map parameters = [:]) {
List credentials = [

View File

@ -1,7 +1,7 @@
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/githubstatus.yaml'
@Field String METADATA_FILE = 'metadata/githubSetCommitStatus.yaml'
void call(Map parameters = [:]) {
List credentials = [

View File

@ -5,7 +5,7 @@ import com.sap.piper.Utils
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/hadolint.yaml'
@Field String METADATA_FILE = 'metadata/hadolintExecute.yaml'
@Field Set GENERAL_CONFIG_KEYS = []
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus([

View File

@ -1,7 +1,7 @@
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/kaniko.yaml'
@Field String METADATA_FILE = 'metadata/kanikoExecute.yaml'
void call(Map parameters = [:]) {
List credentials = [[type: 'file', id: 'dockerConfigJsonCredentialsId', env: ['PIPER_dockerConfigJSON']]]

View File

@ -9,7 +9,7 @@ import groovy.text.GStringTemplateEngine
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/karma.yaml'
@Field String METADATA_FILE = 'metadata/karmaExecuteTests.yaml'
void call(Map parameters = [:]) {
List credentials = [

View File

@ -1,7 +1,7 @@
import groovy.transform.Field
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/kubernetesdeploy.yaml'
@Field String METADATA_FILE = 'metadata/kubernetesDeploy.yaml'
void call(Map parameters = [:]) {
List credentials = [

View File

@ -7,7 +7,7 @@ import groovy.transform.Field
import static com.sap.piper.Prerequisites.checkScript
@Field String METADATA_FILE = 'metadata/mavenStaticCodeChecks.yaml'
@Field String METADATA_FILE = 'metadata/mavenExecuteStaticCodeChecks.yaml'
@Field String STEP_NAME = getClass().getName()
void call(Map parameters = [:]) {

View File

@ -3,9 +3,9 @@ import groovy.transform.Field
import static com.sap.piper.Prerequisites.checkScript
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/protecode.yaml'
@Field String METADATA_FILE = 'metadata/protecodeExecuteScan.yaml'
//Metadata maintained in file project://resources/metadata/protecode.yaml
//Metadata maintained in file project://resources/metadata/protecodeExecuteScan.yaml
void call(Map parameters = [:]) {
final script = checkScript(this, parameters) ?: this

View File

@ -6,7 +6,7 @@ import groovy.transform.Field
import java.nio.charset.StandardCharsets
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/sonar.yaml'
@Field String METADATA_FILE = 'metadata/sonarExecuteScan.yaml'
void call(Map parameters = [:]) {
handlePipelineStepErrors(stepName: STEP_NAME, stepParameters: parameters) {

View File

@ -5,9 +5,9 @@ import groovy.transform.Field
import static com.sap.piper.Prerequisites.checkScript
@Field String STEP_NAME = getClass().getName()
@Field String METADATA_FILE = 'metadata/whitesource.yaml'
@Field String METADATA_FILE = 'metadata/whitesourceExecuteScan.yaml'
//Metadata maintained in file project://resources/metadata/whitesource.yaml
//Metadata maintained in file project://resources/metadata/whitesourceExecuteScan.yaml
void call(Map parameters = [:]) {
final script = checkScript(this, parameters) ?: this