SAP-archive / cloud-s4-sdk-pipeline

The Cloud SDK pipeline uses the Cloud SDK continuous delivery server for building, checking, and deploying extension applications. Projects based on the SAP Cloud SDK archetype will automatically use this pipeline.
Apache License 2.0
65 stars 25 forks source link

Environment variable `NODE_ENV` not availible in frontend unit and integration test #33

Closed tutschc closed 4 years ago

tutschc commented 4 years ago

Issue Description (Mandatory)

The pipeline is failing in the stage: frontendIntegrationTests, frontendUnitTests

We are using this pipeline to build maven projects with a frontend written with ui5. During frontend unit- and integration testing, we would like to set the environment variable NODE_ENV to BUILD. Currently, we cannot do this by setting options in .pipeline/config.yml (as far as we know), so we have to extend both stages. It would be great if this was possible using only the configuration file. Apparaently, the piper library already supports dockerEnvVars in the step npmExecute, but currently the step is not used in this pipeline lib.

We think this change is easy to implement: Changing

executeNpm(script: script, dockerImage: stageConfiguration?.dockerImage, dockerOptions: dockerOptions) {
  sh "Xvfb -ac :99 -screen 0 1280x1024x16 &"
  withEnv(['DISPLAY=:99']) {
    runOverNpmModules(script: script, npmScripts: ['ci-it-frontend']) { basePath ->
      dir(basePath) {
        sh "npm run ci-it-frontend"
      }
    }
  }
}

to

npmExecute(script: script, dockerImage: stageConfiguration?.dockerImage, dockerOptions: dockerOptions, npmCommand: "run ci-it-frontend", dockerEnvVars: ["DISPLAY": ":99"])

in stageFrontendIntegrationTests.groovy and similarly

executeNpm(script: script, dockerImage: stageConfiguration?.dockerImage, dockerOptions: dockerOptions) {
  sh "Xvfb -ac :99 -screen 0 1280x1024x16 &"
  withEnv(['DISPLAY=:99']) {
    sh "npm run ci-frontend-unit-test"
  }
}

to

npmExecute(script: script, dockerImage: stageConfiguration?.dockerImage, dockerOptions: dockerOptions, npmCommand: "run ci-frontend-unit-test", dockerEnvVars: ["DISPLAY": ":99"])

in stageFrontendUnitTests.groovy and adding the default option

  npmExecute:
    dockerImage: ppiper/node-browsers:v3

to default_s4_pipeline_environment was sufficient to run our pipeline. The only problem is the configuring of the virtual frame buffer (Xvfb -ac :99 -screen 0 1280x1024x16), which cannot be passed to npmExecute of the piper library. As a workaround, we included this command in the npm script. We did not open a pull request, as a clean solution would include this command in the docker image or in the piper project.

Search for existing solution beforehand (Mandatory)

Please try the GitHub search, it works really well

Project Details (Mandatory)

daniel-kurzynski commented 4 years ago

Could you please describe what you want to do with the variable NODE_ENV? In you examples I could not find a reference to it. In general it should be possible to send environment variables via the extension mechanism https://sap.github.io/jenkins-library/extensibility/#1-extend-individual-stages:

void call(Map params) {
  withEnv["NODE__ENV=XYZ"]{
    //execute original stage as defined in the template
    params.originalStage()
  }
}
return this

As an alternative I guess you could also set end environment variable via the npm script in the package.json:

"scripts": {
  ...
  "ci-frontend-unit-test": "NODE_ENV=build karma xyz ..."
}
tutschc commented 4 years ago

We have performance problems with frontent tests. Due to this we serve the UI5 resources (https://sapui5.hana.ondemand.com/1.56.10) on our own, local and on build environment. Due to the different url on local and build environment we need the NODE_ENV to recognize when we are running in build environment. We would prefer not to use any extension machanism.

daniel-kurzynski commented 4 years ago

And what about setting the environment variable in the package.json as described above as alternative. For this you would not have to use extensions.

tutschc commented 4 years ago

If we set the NODE_ENV hard in the package.json as described it would no longer be possible to execute the script in local environment.

daniel-kurzynski commented 4 years ago

Well the idea would be to use the ci-* scripts only in the ci pipeline and have an additional script without the ci- prefix and without the environment variable.

tutschc commented 4 years ago

We try your solution. Thanks.