Home > Enterprise >  MuleSoft Maven plugin not reading variables in Azure pipelines
MuleSoft Maven plugin not reading variables in Azure pipelines

Time:03-16

I have a MuleSoft application that I am trying to deploy from a pipeline. I am using a Maven plugin and a connected app for credentials. Plugin configuration looks like this:

<configuration>
  <armDeployment>
    <muleVersion>${app.runtime}</muleVersion>
    <uri>https://anypoint.mulesoft.com</uri>
    <businessGroupId>${BUSINESSGROUPID}</businessGroupId>
    <target>${TARGET}</target>
    <targetType>${TARGETGROUP}</targetType>
    <connectedAppClientId>${APPCLIENTID}</connectedAppClientId>
    <connectedAppClientSecret>${APPCLIENTSECRET}</connectedAppClientSecret>
    <connectedAppGrantType>client_credentials</connectedAppGrantType>
    <environment>${ENVIRONMENT}</environment>
  </armDeployment>
</configuration>

I define variables in Azure pipeline(3 of them are secret credentials) and when I run the pipeline I am getting 401 Unauthorized error.

When I hard-code values in the above configuration it works fine. Only when I try to have the POM file read them from the pipeline variables do I get this error. Below is my pipeline config too:

trigger:
- master

variables:
  APPCLIENTID: $(APPCLIENTID)
  APPCLIENTSECRET: $(APPCLIENTSECRET)
  ENVIRONMENT: $(ENVIRONMENT)
  BUSINESSGROUPID: $(BUSINESSGROUPID)
  TARGET: $(TARGET)
  TARGETGROUP: $(TARGETGROUP)


pool:
  vmImage: ubuntu-latest

steps:
- task: Maven@3
  inputs:
    mavenPomFile: 'pom.xml'
    mavenOptions: '-Xmx3072m'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: '1.8'
    jdkArchitectureOption: 'x64'
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    goals: 'clean package deploy -DmuleDeploy'

I am not sure whether I need to define variables here again or not.

How do I make the POM file read variables correctly?

CodePudding user response:

You can't use Azure Pipeline variables directly in the pom. They are not properties in Maven. You have to define them explicitly as such in Maven's command line.

In the goals input you can define Maven properties for the command line and assign them the values from the respective Azure Pipeline variable using the 'goals' input.

I'm guessing that the syntax for referencing variables is $(var) so as an example:

    goals: 'clean package deploy -DmuleDeploy -DAPPCLIENTID=$(APPCLIENTID)'

Just try adding the other properties next to APPCLIENTID.

CodePudding user response:

Ok, I found out what the issue is. Azure Pipelines documentation states that you need to use $() in order to access variables but in the case of a POM file you need to use {}. So the POM file should look like this:

<configuration>
<armDeployment>
    <muleVersion>${app.runtime}</muleVersion>
    <uri>https://anypoint.mulesoft.com</uri>
    <businessGroupId>${BUSINESSGROUPID}</businessGroupId>
    <target>${TARGET}</target>
    <targetType>${TARGETGROUP}</targetType>
    <connectedAppClientId>${APPCLIENTID}</connectedAppClientId>
    <connectedAppClientSecret>${APPCLIENTSECRET}</connectedAppClientSecret>
    <connectedAppGrantType>client_credentials</connectedAppGrantType>
    <environment>${ENVIRONMENT}</environment>
  </armDeployment>
</configuration>

One more thing I noticed is that doing this will not allow POM file to read variables set as secret. I am yet to find out how to make this work properly but for now I got most of it to work.

  • Related