giovandrea
giovandrea

Reputation: 207

Setting up some specific variables in an Azure Pipeline

I am struggling to understand how I can pass some variables in an Azure pipeline.

I have a React + Java project built using Maven.

In my POM file I have this to build the React side:

             <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.6</version>
                <configuration>
                    <nodeVersion>v16.13.2</nodeVersion>
                    <npmVersion>8.1.2</npmVersion>              
                    <workingDirectory>ui</workingDirectory>
                    <installDirectory>target</installDirectory>             
                </configuration>
                <executions>
                    <execution>
                        <id>install node and npm</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>install-node-and-npm</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>npm install</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>npm run build</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>npm</goal>
                        </goals>
                        <configuration>
                            <arguments>run build</arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

I have 2 additional files, an application.yml containing some profiling and some DB resources, i.e.:

system:
  name: "MyApp"
spring:
  config:
    activate:
      on-profile: profile1
  datasource:
    url: URL;

and an .env file with some variables:

   var1=URI
   var2=ANOTHERURI

Now, when I build locally I do something like:

mvnw.cmd package spring-boot:run -Dspring-boot.run.profiles=profile1

So basically I am passing a profile and spring will get my application-profile1.yml file with the DB details + my .env file with those 2 variables.

In my Azure pipeline I have:

- task: Maven@3
  displayName: Build Dock
  inputs:
    mavenPomFile: 'pom.xml'
    goals: 'spring-boot:build-image'
    publishJUnitResults: false
    jdkVersionOption: $(jdkVersion)

which is building a Docker image to publish on Azure.

I am trying to find a way to have different sets of my variables, say:

that doesn't rely on me creating an application-profile for each site and different .env.* file. I would like to set all of it directly on my pipeline, something like:

DBsource = source
var1 = URL
var2 = ANOTHER URL

 - task: Maven@3
      displayName: Build Dock
      inputs:
        mavenPomFile: 'pom.xml'
        goals: 'spring-boot:build-image -useMyVariablesSomehow'
        publishJUnitResults: false
        jdkVersionOption: $(jdkVersion)

or

get var from a file that I create for each site

 - task: Maven@3
      displayName: Build Dock
      inputs:
        mavenPomFile: 'pom.xml'
        goals: 'spring-boot:build-image -useMyFileSomehow'
        publishJUnitResults: false
        jdkVersionOption: $(jdkVersion)

Is it something that I can do?

Thanks

Upvotes: 0

Views: 975

Answers (1)

user3067684
user3067684

Reputation: 1258

If i understand you correctly, you are looking to add environment variables, depending on which environment/site, when you build your project with Azure Pipelines.

If so.

I make use of the Azure Devops Pipeline Libraries, one global and one for each site. Then pass in the names of the libraries like so

variables:
      - group: ${{parameters.globalLibraryName}}
      - group: ${{parameters.specificSiteLibrary}}

The second overwrites the first.

Have a look at Azure Pipeline Variable Groups

Upvotes: 2

Related Questions