🐇

Azure Pipelines で Azure Functions へのCI/CDを設定する

2023/02/01に公開

公式にもあんまり書いてないのでご参考までに。

環境

  • Azure DevOps
  • Azure Functions
  • Java17
  • SpringBoot2.7.8
  • Maven

pipelines.yml

azure-pipelines.yml
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
  branches:
    - develop

variables:
  appName: "pipeline-test"

stages:
  - stage: "Build"
    displayName: "Build the web application"
    jobs:
      - job: "Build"
        displayName: "Build job"
        pool:
          vmImage: "ubuntu-22.04"

        steps:
          - task: Maven@4
            inputs:
              mavenPomFile: "pom.xml"
              publishJUnitResults: true
              testResultsFiles: "**/surefire-reports/TEST-*.xml"
              javaHomeOption: "JDKVersion"
              jdkVersionOption: "1.17"
              mavenVersionOption: "Default"
              mavenOptions: "-Xmx3072m"
              mavenAuthenticateFeed: false
              effectivePomSkip: false
              sonarQubeRunAnalysis: false
              goals: "package"
          
          - task: ArchiveFiles@2    # ここがポイント。zipで固める。
            inputs:
              rootFolderOrFile: 'target/azure-functions/$(appName)'
              includeRootFolder: false
              archiveType: 'zip'
              archiveFile: '$(Build.ArtifactStagingDirectory)/$(appName)$(Build.BuildId).zip'
              replaceExistingArchive: true

          - upload: $(Build.ArtifactStagingDirectory)
            artifact: drop

  - stage: "Deploy"
    displayName: "Deploy"
    dependsOn: Build
    condition: succeeded()
    jobs:
      - deployment: Deploy
        pool:
          vmImage: "ubuntu-22.04"
        environment: dev
        strategy:
          runOnce:
            deploy:
              steps:
                - download: current
                  artifact: drop
                - task: AzureFunctionApp@1
                  displayName: "Functions Deploy"
                  inputs:
                    appType: functionAppLinux
                    azureSubscription: $(serviceConnection)
                    appName: $(appName)
                    package: '$(Pipeline.Workspace)/drop/$(appName)$(Build.BuildId).zip'

ポイント

appNameにはFunctionsのnameが入ります。
AppServiceでは、packageしたjarをアップロードして渡してますが、functionsはこのjarではなく、target/azure-functions で生成されたディレクトリをzip圧縮して渡す必要があるようです。

参考

Azure Functions Java CI/CD pipeline in Azure DevOps

Discussion