1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/documentation/docs/guidedtour.md
Roland Stengel b65f0a2461 review fixes
2019-05-23 21:56:42 +02:00

10 KiB

Getting Started with Project "Piper"

Follow this guided tour to become familiar with the basics of using project "Piper".

Prerequisites

(Optional) Install the cx-server Toolkit for Jenkins

cx-serveris a lifecycle management toolkit that provides Docker images with a preconfigured Jenkins and a Nexus-based cache to facilitate the configuration and usage of Jenkins.

To use the toolkit, get the cx-server script and its configuration file server.cfg by using the following command:

docker run -it --rm -u $(id -u):$(id -g) -v "${PWD}":/cx-server/mount/ ppiper/cx-server-companion:latest init-cx-server

When the files are downloaded into the current directory, launch the Jenkins server by using the following command:

./cx-server start

For more information on the Jenkins lifecycle management and how to customize your Jenkins, have a look at the Operations Guide for Cx Server.

Create Your First Pipeline

For the beginning, we recommend using an SAP sample application. The repository cloud-cf-helloworld-nodejs contains a simple nodejs application that can be enriched with a pipeline built with MTA and deployed into the Cloud Foundry environment.

  1. Fork the cloud-cf-helloworld-nodejs repository into your GitHub organization.

  2. Select the 1_REST_persist_in_Memory branch of your cloud-cf-helloworld-nodejs fork.

  3. Create a new file with the name Jenkinsfile in the root level of your repository and enter the following code:

    @Library('piper-lib-os') _
    node() {
        stage('prepare') {
            checkout scm
            setupCommonPipelineEnvironment script:this
        }
    }
    

    The "prepare" step synchronizes the repository and initializes the project specific settings. For more information about Jenkinsfiles and pipelines, see Using a Jenkinsfile.

  4. Save your changes to your remote repository.

  5. To set up a Jenkins job for your repository, open the Jenkins UI under http://<jenkins-server-address>:<http-port> and choose New Item. Per default, the cx-server starts Jenkins on HTTP port 80. For more information, see the Jenkins User Documentation.

    ![Clicke New Item](../images/JenkinsHomeMenu-1.png "Jenkins Home Menu")

  6. Provide a name for your new item (for example, My First Pipeline) and select Pipeline.

    ![Create Pipeline Job](../images/JenkinsNewItemPipeline-1.png "Jenkins New Item")

  7. For Definition in the Pipeline options, choose Pipeline script from SCM.

  8. For SCM, choose Git.

  9. For Repository URL in the Repositories section, enter the URL of your Git repository, for example https://github.com/<your-org>/cloud-cf-helloworld-nodejs. Note: If your repository is protected, you must provide your credentials in the Credentials section.

    ![Create Pipeline Job](../images/JenkinsNewItemPipeline-2.png "Jenkins New Item")

  10. For Branch Specifier in the Branches to build section, enter the branch name */1_REST_persist_in_Memory.

  11. Choose Save.

  12. To run your pipeline, choose Build Now in the job UI.

Add a Build Step

  1. In your Jenkinsfile, add the following code snippet after stage("prepare") { ... }:

    stage('build') {
        mtaBuild script: this
    }
    

    The mtaBuild step calls a build tool to build a multi-target application (MTA). The tool consumes an MTA descriptor that contains the metadata of all entities which comprise an application or are used by one during deployment or runtime, and the dependencies between them. For more information about MTAs, see sap.com.

  2. Create the MTA descriptor file with the name mta.yaml in the root level of the repository. Insert the following code:

    _schema-version: 2.1.0
    ID: com.sap.piper.node.hello.world
    version: 1.0.0
    description: A Hello World sample application
    provider: SAP Sample generator
    modules:
      - name: piper.node.hello.world
        type: nodejs
        path: .
    
  3. Configure the step to build an MTA for the Cloud Foundry environment. Create the configuration file .pipeline/config.yml relative to the root level of the repository and insert the following content:

    general:
    steps:
      mtaBuild:
        buildTarget: 'CF'
    

    For additional information about the configuration, have a look at the Common Configuration Guide and the MTA build step documentation.

  4. Save your changes to your remote repository.

  5. To run your pipeline, choose Build Now in the job UI.

Add a Deploy Step

  1. In your Jenkinsfile, add the following code snippet:
stage('deploy') {
    def mtarFilePath = commonPipelineEnvironment.getMtarFilePath()
    cloudFoundryDeploy( script: this, mtaPath: mtarFilePath)
}

The cloudFoundryDeploy step calls the Cloud Foundry command line client to deploy into SAP Cloud Platform.

  1. To configure the step to deploy into the Cloud Foundry environment, in your repository, open or create the .pipeline/config.yml and add the following content:

    cloudFoundryDeploy:
      deployTool: 'mtaDeployPlugin'
      deployType: 'standard'
      cloudFoundry:
        org: '<your-organisation>'
        space: '<your-space>'
        credentialsId: 'CF_CREDENTIALSID'
    

    The key CF_CREDENTIALSID refers to a user-password credential you must create in Jenkins: In Jenkins, choose Credentials from the main menu and add a Username with Password entry.

    ![Add Credentials](../images/JenkinsCredentials-1.png "Add Credentials")

    For more information about the configuration, see the [Common Configuration Guide][resources-configuration] and [cloudFoundryDeploy][resources-step-cloudFoundryDeploy].
  2. Save your changes to your remote repository.

  3. To run your pipeline, choose Build Now in the job UI.

Complete the Guided Tour

Your application has been deployed into your space in the Cloud Foundry space on SAP Cloud Platform. Logon to SAP Cloud Platform and verify the status of your application.

![Deployed Application](../images/SCPDeployApp-1.png "SAP Cloud Platform")

To view the URL of your application, choose the application name. Open the Route and add /users to the URL. Result: The application returns data.

If your pipeline fails, compare it to the final Jenkinsfile, the config.yml, and the mta.yaml. Yaml files are sensitive regarding indentation.

What's Next

You are now familiar with the basics of using project "Piper". Through the concept of pipeline as code, project "Piper" and Jenkins pipelines are extremely powerful. While Jenkins pipelines offer a full set of common programming features, project "Piper" adds SAP-specific flavors. Have a look at the increasing list of features you can implement through the project "Piper" steps and see the different scenarios to understand how to integrate SAP systems into your pipeline.

The configuration pattern supports simple pipelines that can be reused by multiple applications. To understand the principles of inheritance and customization, have a look at the the configuration documentation.