根据分支更改customWorkspace

时间:2019-05-07 19:01:42

标签: jenkins jenkins-pipeline

我正在使用Jenkins管道,并且在运行用于获取CustomWorkspace的函数时遇到以下错误:No such DSL method '$' found among steps

def getLabel() {
    // Which server to run this on.
    return "php7a"
}
// Choose the site name based on git name and if it is a Pull Request or branch.
def getSitename() {
  if (env.BRANCH_NAME == 'updates') {
    SITENAME = "reo"
    if (env.CHANGE_BRANCH && !env.CHANGE_FORK){
      return "${SITENAME}-${env.CHANGE_BRANCH.toLowerCase()}"
    }
    else {
      return "${SITENAME}-${env.BRANCH_NAME.toLowerCase()}"
    }
  }
  else {
    // Set the project name, most likely the git repo name.
    if (env.CHANGE_BRANCH && !env.CHANGE_FORK){
      return "${env.CHANGE_BRANCH.toLowerCase()}"
    }
    else {
      return "${env.BRANCH_NAME.toLowerCase()}"
    }
  }
}

def projectName() {
    return "myproject"
}


/**
 * Get custom workspace path.
 *
 * @param string projectName
 *   Project name.
 * @param string siteName
 *   Site name.
 */
def getCustomworkspace(String projectName,
                       String siteName) {
  if (env.BRANCH_NAME == 'updates') {
    return "/var/www/${siteName}"
  }
  else {
    return "/var/build/${projectName}-${siteName}"
  }
}

...
...

  agent {
    node {
      label "${getLabel()}"
      customWorkspace "${getCustomworkspace(${projectName()}, ${getSitename()})}"
    }
  }
...
...

2 个答案:

答案 0 :(得分:0)

您可以如下更改customWorkspace行

customWorkspace "${getCustomworkspace('${projectName()}', '${getSitename()}')}"

答案 1 :(得分:0)

我无法使用参数调用该函数,但是我可以在函数内部调用该函数,所以我做到了:

/**
 * Get custom workspace path.
 */
def getCustomworkspace() {
  if (env.BRANCH_NAME == 'xupdates') {
    return "/var/www/${getSitename()}"
  }
  else {
    return "/var/build/${projectName()}-${getSitename()}"
  }
}
customWorkspace "${getCustomworkspace()}"