1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

Abap pipeline extension for cloneGitRepo & checkoutBranch to be used (#2004)

* Added CFDeleteServiceKeys

* Added ServiceKey deletion tests

* added cfServiceKeys flag explanation to documentation

* removed trailing spaces from documentation

* resolving conflicts

* Changed deletion message an variable naming

* Changed tests

* Changed tests

* Changed tests

* Changed tests

* Changed CloudFoundryDeleteServiceOptions to options

* Changed CloudFoundryDeleteServiceOptions to options

* Minor changes

* Minor changes

* Changed variable naming

* Changed error handling

* Changed error handling and logging

* Changed documentation

* Simplified code

* Fixed CodeClimate issues

* Changed from returning err to nil where no errur returned needed

* Add cloudFoundryCreateServiceKey Go Step

* Changed Groovy File

* Changed aliases

* Removed unneccessary parts

* Minor changes

* Minor changes

* Adapted documentation

* Adapted tests

* Adapted Groovy File

* Changed Groovy file

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Removed Groovy Tests for cfCreateServiceKey

* Minor changes

* Added ATC Check YAML

* Added ATC Check generated files

* Added test class

* Added abapEnvironmentRunATCCheck

* Minor changes

* Minor changes

* Changed groovy

* Minor changes

* Changed groovy

* Changed groovy

* Minor changes

* Adapted Groovy imports

* Adapted Groovy imports

* Adapted Groovy imports

* Adapted Groovy

* Getting ATC results

* Changed error message

* changed groovy

* removed trailing spaces

* Added login check

* Minor changes

* Added step to whitelistScriptReference

* Added ATC error message handling

* Added groovy file

* Added step to groovy tests

* corrected metadata file

* Debugging

* Debugging

* Added yaml config parameter for ATC run

* Adapted file location of ATC run config to jenkins specific location

* Implementing universal pipeline logic for finding yaml config regardless of pipeline

* Changed error handling for reading config yaml file

* Changed atcrunconfig alias

* minor changes

* Minor changes

* Minor changes

* Changed back to dynamic file reading

* Minor changes

* filepath changes

* Removing CF Login

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Minor changes

* Removed whitespaces

* Added CF functions unit tests

* Added invalid parameter handling

* Removed package and SC flag

* Minor changes

* Changed tests

* Changed tests

* Changed tests

* Minor changes

* Changed tests

* removed unnecessary logout

* Added documentation

* Changed docu

* Changed docu

* Changed docu

* Changed docu

* Changed docu

* Changed docu

* Changed docu

* Changed docu

* Changed docu

* Changed docu

* Removed trailing spaces

* Added newline at end of file

* code climate fixes

* code climate fixes

* code climate fixes

* Minor changes

* Minor changes

* Minor changes

* Changed tests

* Test changes

* Splitted Cloud Foundry functions into two classes

* Removed two steps from whtielistScriptReference

* removed atcrunConfig alias

* issue fixes

* Changed docu

* Changed docu

* Changed docu

* Removed trailing spaced from docu

* Changed docu

* Go generator run

* Issue fixes

* Remove unnecessary imports

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>

* Update whitelistScript

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>

* Adding piperutils for writing xml file

* Persisting ATC Results with piperutils

* Set failonMissingReports to true

* Refactoring for CodeClimate

* Changed result file name

* Changed credentials aliases

* changing secret name

* Removing trailing spaces

* Added secret name and alias to docu

* Add cloneGitRepo & checkoutBranch steps

* Tests added

* Docu added

* docu changes

* remark changes

* Class name changed

* minor changes

* Test added for prepare system stage

* Review changes

* Tests

* Step order tests

* Step order tests

* Step order tests

* Step order tests

* strategy parameter tests

* configHelper import added

* Default value for strategy added and remove pull config check

* corrected stage default

* Tests added

* changes in TestConfig

* Tests adapted for no prepare system stage

* tests changed

* changed tests

* input added

* changed tests

* changed tests

* changed tests

* commonPipeline removed

* add input register

* remove step key

* remove return

* remove comment

* correct method usage

* CodeClimate Fixes

* PR fixes

* addonBuild rename

* removed trailing spaces

Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Daniel Mieg <56156797+DanielMieg@users.noreply.github.com>
This commit is contained in:
Dominik Lendle 2020-11-03 10:16:57 +01:00 committed by GitHub
parent 67727147f8
commit c6afd7696b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 183 additions and 15 deletions

View File

@ -62,7 +62,21 @@ The communication to the ABAP system is done using a Communication Arrangement.
Please have a look at the [step documentation](https://sap.github.io/jenkins-library/steps/cloudFoundryCreateServiceKey/) for more details.
## 5. Configuration for ATC
## 5. Configuration for Cloning the repositories
If you have specified the `Clone Repositories` Stage you can make use of a dedicated configuration file containing the repositories to be pulled and the branches to be switched on. The `repositories` flag makes use of such a configuration file and helps executing a Pull, Clone and Checkout of the Branches of the Repositores. Create the file `repositories.yml` with the following structure containing your repositories including the branches for this Stage.
```yml
repositories:
- name: '/DMO/GIT_REPOSITORY'
branch: 'master'
- name: '/DMO/GIT_REPO'
branch: 'master'
```
You can later use the `repositories.yml` file for the `repositories` parameter in the `Clone Repositories` stage used in chapter [7. Technical Pipeline Configuration](#7-technical-pipeline-configuration).
## 6. Configuration for ATC
Create a file `atcConfig.yml` to store the configuration for the ATC run. In this file, you can specify which Packages or Software Components shall be checked. Please have a look at the step documentation for more details. Here is an example of the configuration:
@ -74,7 +88,7 @@ atcobjects:
Please have a look at the [step documentation](https://sap.github.io/jenkins-library/steps/abapEnvironmentRunATCCheck/) for more details.
## 6. Technical Pipeline Configuration
## 7. Technical Pipeline Configuration
Create a file `.pipeline/config.yml` where you store the configuration for the pipeline, e.g. apiEndpoints and credentialIds. The steps make use of the Credentials Store of the Jenkins Server. Here is an example of the configuration file:
@ -91,7 +105,8 @@ stages:
cfServiceManifest: 'manifest.yml'
cfServiceKeyConfig: 'sap_com_0510.json'
Clone Repositories:
repositoryNames: ['/DMO/REPO']
strategy: 'Pull'
repositories: 'repositories.yml'
ATC:
atcConfig: 'atcConfig.yml'
steps:
@ -99,13 +114,18 @@ steps:
cfDeleteServiceKeys: true
```
If one of the stages of the pipeline is not configured in this yml file, the stage will not be executed during the pipeline run. If the stage `Prepare System` is configured, the system will be deprovisioned in the cleanup routine - although it is necessary to configure the step `cloudFoundryDeleteService` as above.
If one of the stages of the pipeline is not configured in this yml file, the stage will not be executed during the pipeline run. If the stage `Prepare System` is configured, the system will be deprovisioned in the cleanup routine - although it is necessary to configure the step `cloudFoundryDeleteService` as above. Also, if the `Prepare System` stage is configured, you can specify the strategy for the `strategy` step parameter that should be performed on the Software Components and the Branches that you have configured in the `respositories.yml` file in step [5. Configuration for Cloning the repositories](#5-configuration-for-cloning-the-repositories). Per default the strategy will be set to `Pull` if not specified. The following strategies are supported and can be used on the Software Components and Branches:
Please make sure the parameters align with the values defined in the other configuration files, e.g. the service name in the `manifest.yml` needs to be the same as the value in `general.cfServiceInstance`.
* `Pull`: If you have specified Pull as the strategy the [abapEnvironmentPullGitRepo](https://sap.github.io/jenkins-library/steps/abapEnvironmentPullGitRepo/) step will be used
* `Clone`: If you have specified the Clone strategy the [abapEnvironmentCloneGitRepo](https://sap.github.io/jenkins-library/steps/abapEnvironmentCloneGitRepo/) step will be used
* `CheckoutPull`: This strategy performs a Checkout of Branches with the [abapEnvironmentCheckoutBranch](https://sap.github.io/jenkins-library/steps/abapEnvironmentCheckoutBranch/) step followed by a Pull of the Software Component with the [abapEnvironmentPullGitRepo](https://sap.github.io/jenkins-library/steps/abapEnvironmentPullGitRepo/) step
* `addonBuild`: This will perform a Pull of the Software Component with the [abapEnvironmentPullGitRepo](https://sap.github.io/jenkins-library/steps/abapEnvironmentPullGitRepo/) step, a Checkout of Branches with the [abapEnvironmentCheckoutBranch](https://sap.github.io/jenkins-library/steps/abapEnvironmentCheckoutBranch/) step followed by a Pull of the Software Component again with the [abapEnvironmentPullGitRepo](https://sap.github.io/jenkins-library/steps/abapEnvironmentPullGitRepo/) step
Note that you can use the `repositories.yml` file with the `repositories` parameter consistently for all strategies. Also, please make sure the parameters align with the values defined in the other configuration files, e.g. the service name in the `manifest.yml` needs to be the same as the value in `general.cfServiceInstance`.
The values for `cfApiEndpoint`,`cfOrg` and `cfSpace` can be found in the respective overview pages in the SAP Cloud Platform Cockpit. The Cloud Foundry credentials, saved in the Jenkins credentials store with the ID `cfCredentialsId`, must refer to a user with the required authorizations ("Space Developer") for the Cloud Foundry Organization and Space.
## 7. Create a Jenkins Pipeline
## 8. Create a Jenkins Pipeline
On your Jenkins server click on `New Item` to create a new pipeline. Provide a name and select the type `Pipeline`.
On the creation screen for the pipeline, scroll to the section `Pipeline` and select `Pipeline script from SCM`. Provide the URL (and credentials - if required) of the repository, in which you configured the pipeline. Make sure the `Script Path` points to your Jenkinsfile - if you created the Jenkinsfile according to the documentation above, the default value should be correct.

View File

@ -46,8 +46,13 @@ After the confirmation, the Communication Arrangement [SAP_COM_0510](https://hel
## Clone Repositories
In this stage, the Software Components / Git repositories are pulled to the ABAP Environment system using the step abapEnvironmentPullGitRepo.
As a default we assume that the ABAP Environment system is already configured and all Software Components are cloned and the latest change of the respective Software Components should be pulled with the abapEnvironmentPullGitRepo step.
In this stage, the Software Components / Git repositories are then pulled to the ABAP Environment system using the step abapEnvironmentPullGitRepo.
The step can receive a list of Software Components / repositories and pulls them successively.
If the Software Components have not been cloned on the ABAP Environment system yet or you want to e.g. checkout a different Branch you can make use of the `strategy` stage parameter and perform other steps and step orders.
Please refer to the Configuration section for the abapEnvironment Pipeline or the respective documentations for the [abapEnvironmentCheckoutBranch](https://sap.github.io/jenkins-library/steps/abapEnvironmentCheckoutBranch/), [abapEnvironmentCloneGitRepo](https://sap.github.io/jenkins-library/steps/abapEnvironmentCloneGitRepo/) and [abapEnvironmentPullGitRepo](https://sap.github.io/jenkins-library/steps/abapEnvironmentPullGitRepo/) steps.
Either way, if you chose a dedicated strategy or the default Pull variant you can optionally provide a dedicated configuration file, e.g. `repositories.yml`, containing the repositories to be cloned and the branches to be switched to. This file can be used consistently for all strategies.
## ATC

View File

@ -0,0 +1,107 @@
package templates
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import util.BasePiperTest
import util.JenkinsReadYamlRule
import util.JenkinsStepRule
import util.PipelineWhenException
import util.Rules
import static org.hamcrest.Matchers.*
import static org.junit.Assert.assertThat
class AbapEnvironmentPipelineStageCloneRepositoriesTest extends BasePiperTest {
private JenkinsStepRule jsr = new JenkinsStepRule(this)
@Rule
public RuleChain rules = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(jsr)
private stepsCalled = []
@Before
void init() {
binding.variables.env.STAGE_NAME = 'Clone Repositories'
helper.registerAllowedMethod('piperStageWrapper', [Map.class, Closure.class], {m, body ->
assertThat(m.stageName, is('Clone Repositories'))
return body()
})
helper.registerAllowedMethod('strategy', [Map], {m ->
stepsCalled.add('strategy')
})
helper.registerAllowedMethod('abapEnvironmentPullGitRepo', [Map.class], {m -> stepsCalled.add('abapEnvironmentPullGitRepo')})
helper.registerAllowedMethod('abapEnvironmentCheckoutBranch', [Map.class], {m -> stepsCalled.add('abapEnvironmentCheckoutBranch')})
helper.registerAllowedMethod('abapEnvironmentCloneGitRepo', [Map.class], {m -> stepsCalled.add('abapEnvironmentCloneGitRepo')})
}
@Test
void testAbapEnvironmentPipelineStageCloneRepositoriesPull() {
nullScript.commonPipelineEnvironment.configuration.runStage = [
'Prepare System': true
]
jsr.step.abapEnvironmentPipelineStageCloneRepositories(script: nullScript, strategy: 'Pull')
assertThat(stepsCalled, hasItems('abapEnvironmentPullGitRepo'))
assertThat(stepsCalled, not(hasItems('abapEnvironmentCloneGitRepo')))
assertThat(stepsCalled, not(hasItems('abapEnvironmentCheckoutBranch')))
}
@Test
void testAbapEnvironmentPipelineStageCloneRepositoriesClone() {
nullScript.commonPipelineEnvironment.configuration.runStage = [
'Prepare System': true
]
jsr.step.abapEnvironmentPipelineStageCloneRepositories(script: nullScript, strategy: 'Clone')
assertThat(stepsCalled, hasItems('abapEnvironmentCloneGitRepo'))
assertThat(stepsCalled, not(hasItems('abapEnvironmentPullGitRepo', 'abapEnvironmentCheckoutBranch')))
}
@Test
void testAbapEnvironmentPipelineStageCloneRepositoriesCheckoutPull() {
nullScript.commonPipelineEnvironment.configuration.runStage = [
'Prepare System': true
]
jsr.step.abapEnvironmentPipelineStageCloneRepositories(script: nullScript, strategy: 'CheckoutPull')
assertThat(stepsCalled, hasItems('abapEnvironmentPullGitRepo', 'abapEnvironmentCheckoutBranch'))
assertThat(stepsCalled, not(hasItems('abapEnvironmentCloneGitRepo')))
}
@Test
void testAbapEnvironmentPipelineStageCloneRepositoriesPullCheckoutPull() {
nullScript.commonPipelineEnvironment.configuration.runStage = [
'Prepare System': true
]
jsr.step.abapEnvironmentPipelineStageCloneRepositories(script: nullScript, strategy: 'addonBuild')
assertThat(stepsCalled, hasItems('abapEnvironmentPullGitRepo', 'abapEnvironmentCheckoutBranch'))
assertThat(stepsCalled, not(hasItems('abapEnvironmentCloneGitRepo')))
}
@Test
void testAbapEnvironmentPipelineStageCloneRepositoriesNoStrategy() {
nullScript.commonPipelineEnvironment.configuration.runStage = [
'Prepare System': true
]
jsr.step.abapEnvironmentPipelineStageCloneRepositories(script: nullScript)
assertThat(stepsCalled, hasItems('abapEnvironmentPullGitRepo'))
assertThat(stepsCalled, not(hasItems('abapEnvironmentCloneGitRepo', 'abapEnvironmentCheckoutBranch')))
}
}

View File

@ -32,7 +32,10 @@ class AbapEnvironmentPipelineStagePrepareSystemTest extends BasePiperTest {
assertThat(m.stageName, is('Prepare System'))
return body()
})
helper.registerAllowedMethod('input', [Map], {m -> return null})
helper.registerAllowedMethod('input', [Map], {m ->
stepsCalled.add('input')
return null
})
helper.registerAllowedMethod('cloudFoundryCreateService', [Map.class], {m -> stepsCalled.add('cloudFoundryCreateService')})
helper.registerAllowedMethod('cloudFoundryCreateServiceKey', [Map.class], {m -> stepsCalled.add('cloudFoundryCreateServiceKey')})
}
@ -45,6 +48,7 @@ class AbapEnvironmentPipelineStagePrepareSystemTest extends BasePiperTest {
]
jsr.step.abapEnvironmentPipelineStagePrepareSystem(script: nullScript)
assertThat(stepsCalled, hasItems('cloudFoundryCreateService', 'cloudFoundryCreateServiceKey'))
assertThat(stepsCalled, hasItem('cloudFoundryCreateService'))
assertThat(stepsCalled, hasItem('cloudFoundryCreateServiceKey'))
}
}

View File

@ -1,6 +1,6 @@
import com.cloudbees.groovy.cps.NonCPS
import com.sap.piper.GenerateStageDocumentation
import groovy.transform.Field
import com.sap.piper.Utils
import com.sap.piper.ConfigurationHelper
import static com.sap.piper.Prerequisites.checkScript
@ -8,20 +8,52 @@ import static com.sap.piper.Prerequisites.checkScript
@Field Set GENERAL_CONFIG_KEYS = []
@Field STAGE_STEP_KEYS = [
/** Pulls Software Components / Git repositories into the ABAP Environment instance */
'abapEnvironmentPullGitRepo'
'abapEnvironmentPullGitRepo',
/** Checks out a Branch in the pulled Software Component on the ABAP Environment instance */
'abapEnvironmentCheckoutBranch',
/** Clones Software Components / Git repositories into the ABAP Environment instance and checks out the respective branches */
'abapEnvironmentCloneGitRepo',
/** Specifies the strategy that should be peformed on the ABAP Environment instance*/
'strategy'
]
@Field Set STEP_CONFIG_KEYS = GENERAL_CONFIG_KEYS.plus(STAGE_STEP_KEYS)
@Field Set PARAMETER_KEYS = STEP_CONFIG_KEYS
/**
* This stage clones Git repositories / software components to the ABAP Environment instance
* This stage clones Git repositories / software components to the ABAP Environment instance and checks out the master branch
*/
void call(Map parameters = [:]) {
def script = checkScript(this, parameters) ?: this
def stageName = parameters.stageName?:env.STAGE_NAME
Map config = ConfigurationHelper.newInstance(this)
.loadStepDefaults()
.mixinGeneralConfig(script.commonPipelineEnvironment, GENERAL_CONFIG_KEYS)
.mixinStageConfig(script.commonPipelineEnvironment, stageName, STEP_CONFIG_KEYS)
.mixin(parameters, PARAMETER_KEYS)
.addIfEmpty('strategy', 'Pull')
.use()
piperStageWrapper (script: script, stageName: stageName, stashContent: [], stageLocking: false) {
abapEnvironmentPullGitRepo script: parameters.script
switch (config.strategy) {
case 'Pull':
abapEnvironmentPullGitRepo script: parameters.script
break
case 'Clone':
abapEnvironmentCloneGitRepo script: parameters.script
break
case 'CheckoutPull':
abapEnvironmentCheckoutBranch script: parameters.script
abapEnvironmentPullGitRepo script: parameters.script
break
case 'addonBuild':
abapEnvironmentPullGitRepo script: parameters.script
abapEnvironmentCheckoutBranch script: parameters.script
abapEnvironmentPullGitRepo script: parameters.script
break
default:
abapEnvironmentPullGitRepo script: parameters.script
break
}
}
}