jenkinsci/pipeline-as-yaml-plugin

Converting `environment` with `credentials` method is broken?

Bargamut opened this issue · 2 comments

Describe the bug
The environment section parser bug: a special helper method credentials parser is broken. It's adding single quotes around method. Then it will be errors in pipeline.

To Reproduce
Steps to reproduce the behavior:

  1. Go to 'Pipeline As YAML Converter'
  2. Paste the code from Additional context
  3. Press 'Convert to Pipeline'
  4. See like environment section converted
    4.1 Press 'Valid' & see validation errors

Expected behavior
As expected by docs of environment syntax.
Convert this

environment:
  COVERALLS_SECRET_TOKEN: credentials('COVERALLS_SECRET_TOKEN')

to this (without quotes around credentials method):

environment {
  COVERALLS_SECRET_TOKEN = credentials('COVERALLS_SECRET_TOKEN')
}

Desktop (please complete the following information):

  • Ubuntu 18.04 on server, Linux Mint 20 on desktop
  • Chrome 84 on desktop

Additional context
I have code like this:

pipeline
  agent:
    label: 'master'
  stages:
    - stage: Tests
      stages:
        - stage: Start tests
          environment:
            COVERALLS_SECRET_TOKEN: credentials('COVERALLS_SECRET_TOKEN')
          steps:
            - echo "====++++executing Start tests++++===="
            - echo "COVERALLS_SECRET_TOKEN = ${COVERALLS_SECRET_TOKEN}"
            - sh 'npm install'
            - sh 'npm test'
          post:
            always:
              - echo "====++++always++++===="
            success:
              - echo "====++++Start tests executed successfully++++===="
              - sh 'npm run coverage'
            failure:
              - echo "====++++Start tests execution failed++++===="

and this is parsing result:

pipeline {
  agent {
    node {
      label 'master'
    }
  }
  stages {
    stage('Tests') {
      stages {
        stage('Start tests') {
          environment {
            COVERALLS_SECRET_TOKEN = 'credentials('COVERALLS_SECRET_TOKEN')'
          }
          steps {
            echo "====++++executing Start tests++++===="
            echo "COVERALLS_SECRET_TOKEN = ${COVERALLS_SECRET_TOKEN}"
            sh 'npm install'
            sh 'npm test'
          }

Validation says that:

startup failed:
WorkflowScript: 18: Environment variable values must either be single quoted, double quoted, or function calls. @ line 18, column 50.
LS_SECRET_TOKEN = 'credentials('COVERALLS_
^

WorkflowScript: 17: No variables specified for environment @ line 17, column 11.
environment {
^

2 errors

Hi @Bargamut,

Thanks for the detailed explanation. I've made the changes regarding the usage of the credentials in Environments.

Parser will parse this definitions

pipeline:
  agent:
    label: 'master'
  stages:
    - stage: Tests
      stages:
        - stage: Start tests
          environment:
            KEY1: 'VAL1'
            KEY2: "VAL2"
            COVERALLS_SECRET_TOKEN: credentials('COVERALLS_SECRET_TOKEN')

to this

pipeline {
  agent {
    node {
      label 'master'
    }
  }
  stages {
    stage('Tests') {
      stages {
        stage('Start tests') {
          environment {
            KEY1 = 'VAL1'
            KEY2 = 'VAL2'
            COVERALLS_SECRET_TOKEN = credentials('COVERALLS_SECRET_TOKEN')
          }
        }
      }
    }
  }
}

in the next release.

Released with 0.14-rc