DevOps Classroomnotes 01/Jul/2023

Azure DevOps pipelines contd

Azure DevOps Variables

---
trigger:
  - master
variables:
  goals: package
parameters:
  - name: mavenGoal
    displayName: Maven Goal
    type: string
    default: package
    values:
      - package
      - clean package
      - clean install
      - install

jobs:
  - job: buildjob
    displayName: Build and Package Game of life
    pool: 
      vmImage: ubuntu-22.04
    steps:
      - task: Maven@3
        inputs:
          mavenPOMFile: 'pom.xml'
          goals: ${{ parameters.mavenGoal }}
          publishJUnitResults: true
          testResultsFiles: '**/surefire-reports/TEST-*.xml'
          javaHomeOption: 'JDKVersion'
          jdkVersionOption: '1.8'
      - task: CopyFiles@2
        inputs:
          Contents: "**/target/gameoflife.war"
          TargetFolder: $(Build.ArtifactStagingDirectory)
      - task: PublishBuildArtifacts@1
        inputs:
          pathToPublish: $(Build.ArtifactStagingDirectory)
          artifactName: GameOfLifeArtifacts
  • Azure DevOps parameters can be set during the execution of the pipeline
    Preview
  • Azure DevOps Pipeline values that are passed before execution is parameter and variables are set and modified during pipeline execution.
  • Setting variables from scripts in Azure DevOps Pipelines Refer Here

Making Pipelines resuable using templates

  • Refer Here for official docs
  • We have created a reusable template with a name generic-maven-java.yaml
parameters:
- name: javaVersion 
  type: string 
  default: '1.11'
- name: mavenGoal
  type: string
  default: package
- name: artifactPath
  type: string
  default: '**/*.jar'
- name: artifactName
  type: string
  default: generic

steps:
- task: Maven@3
  inputs:
    mavenPOMFile: 'pom.xml'
    goals: ${{ parameters.mavenGoal }}
    publishJUnitResults: true
    testResultsFiles: '**/surefire-reports/TEST-*.xml'
    javaHomeOption: 'JDKVersion'
    jdkVersionOption: ${{ parameters.javaVersion}}
- task: CopyFiles@2
  inputs:
    Contents: ${{ parameters.artifactPath }}
    TargetFolder: $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts@1
  inputs:
    pathToPublish: $(Build.ArtifactStagingDirectory)
    artifactName: ${{ parameters.artifactName }}
  • In the spring petclinic pipeline is now as simple as
---
trigger:
  - main

pool: 
  vmImage: ubuntu-22.04

extends:
  template: generic-maven-java.yaml
  parameters:
    javaVersion: '1.17'
    mavenGoal: 'package'
    artifactPath: '**/target/spring-petclinic*.jar'
    artifactName: 'SPCArtifacts'
  • Now lets try to reuse the template which is different git repo with the help of azure devops pipeline resources. Refer Here for usage
  • Azure DevOps Pipline resources Refer Here
  • Our yaml
---
trigger:
  - master
pool: 
    vmImage: ubuntu-22.04
resources:
  repositories:
    - repository: BuildTemplates
      name: BuildTemplates
      type: git
extends:
  template: generic-maven-java.yaml@BuildTemplates
  parameters:
    javaVersion: '1.8'
    mavenGoal: 'package'
    artifactPath: '**/target/gameoflife.war'
    artifactName: 'GameOfLifeArtifacts'

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner