Issue
I have a Jenkins monorepo pipeline for an Angular project using nrwl/nx.
I'm trying to use the npx nx affected:build
command to build affected changes for the project apps / libs.
I need to populate the --baseHref based on a dynamic value based on the project name.
In the below example a APPS_BASE_HREF
enumeration value based on the current project.name
property.
It seems this approach does not work.
The function expression returns null
, it does not resolve to the enumerator value.
I'm grooved up with trying! :)
The command resolves into :
nx run app2:build:development --baseHref=null
APPS_BASE_HREF = [
'app1': '/test/app1',
'app2': '/test/app2'
]
def getBaseHref(project){
return APPS_BASE_HREF.get(project);
}
pipeline {
agent any
environment { }
options {
buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '5'))
}
stages {
stage('build') {
steps {
sh "npx nx affected:build --base=origin/develop --base-href ${getBaseHref("{project.name}")}"
}
}
}
}
When modifying the getBaseHref
function to :
def getBaseHref(project){
return project
}
The command does seem to recognize the project
value, and resolves into :
nx run app2:build:development --baseHref=app2
Anybody ... any suggestions?
Solution
I have decided, due to reason that I didn't find a way to solve the above, to step away from the sh "npx nx affected:build --base=origin/develop --base-href ${getBaseHref("{project.name}")}"
command approach.
I have simply added a function to return the affected projects, an Array, which I loop and build a seperate sh
command for. Through this iteration, I'm able to put the right variables into place.
The outcome of this approach is the same, just requires some extra lines of code. Not a blocker.
/**
* Get list of affected resources.
*
* @param target Select the specific target (build | test)
* @param select Select the subset of the returned json document (e.g., --selected=projects)
*
* @return Array List of affected items
*/
def getAffected(String target = 'build', String select = 'tasks.target.project') {
def affected = sh(script: "npx nx print-affected --base=${LAST_COMMIT} --head=HEAD --plain --target=${target} --select=${select}", returnStdout: true)
return affected.replaceAll('[\r\n]+', '').split(', ')
}
stage('build') {
steps {
script {
def affected = getAffected()
for (int i = 0; i < affected.size(); ++i) {
sh "npx nx run ${affected[i]}:build:production --base-href ${APPS_BASE_HREF.get(affected[i])}"
}
}
}
}
Answered By - daan.desmedt
Answer Checked By - Clifford M. (JavaFixing Volunteer)