我正在尝试通过 Azure Yml 管道部署一个简单的 Spring Boot 应用程序。部署成功,但 URL 只显示默认的 Microsoft 网页,并提示我需要部署代码。在管道中,我的工件已发布,但未使用。该工件打包为 .jar 文件。应用服务使用 zip 部署。我不知道如何确保该工件打包为 zip ,而不是 jar 。
trigger:
- master
variables:
azureSubscription: 'xxxxxxx'
webAppName: 'yyyyyyy'
stages:
- stage: BuildApplication
jobs:
- job: Build
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: 'package'
- task: CopyFiles@2
inputs:
targetFolder: '$(Build.ArtifactStagingDirectory)'
- task: PublishBuildArtifacts@1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
- stage: DeployApplication
jobs:
- job: Deploy
pool:
vmImage: ubuntu-latest
steps:
- task: DownloadBuildArtifacts@1
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: 'drop'
downloadPath: '$(System.ArtifactsDirectory)'
- task: AzureRmWebAppDeployment@4
displayName: 'Azure Web App Deploy: webApp'
inputs:
ConnectionType: 'AzureRM'
azureSubscription: $(azureSubscription)
appType: 'webApp'
WebAppName: $(webAppName)
packageForLinux: '$(System.ArtifactsDirectory)/**/*.zip'
您可以在
BuildApplication
阶段再添加一项任务来压缩构建步骤的输出。
例如,您可以使用
Archive files
任务并指定 .jar 文件所在的文件夹。您应该更改 rootFolderOrFile
变量。 zip 输出将位于 $(Build.ArtifactStagingDirectory)' with the name '$(Build.BuildId).zip
当您在此文件夹中发布
ArtifactStagingDirectory
时,您的 BuildId.zip 将包含您的文件。部署步骤无需额外更改即可正常工作。
- task: ArchiveFiles@2
inputs:
rootFolderOrFile: '$(Build.ArtifactStagingDirectory)'
includeRootFolder: true
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
replaceExistingArchive: true
如果您的应用程序是基于 Maven 的,您应该获得 jar/war 文件。
packageForLinux: '$(System.ArtifactsDirectory)/**/*.war'
packageForLinux: '$(System.ArtifactsDirectory)/**/*.jar'
基于您的工件输出。
由于 2 年后仍然没有最终答案,我决定分享两个使用适合我的 Azure Devops 部署 java jar 应用程序的选项。
选项 1:之前发布的答案的组合
# First rename your jar file name to the standard App Service name: app.jar (not ROOT.jar)
- bash: mv $(Build.ArtifactStagingDirectory)/your_file.jar $(Build.ArtifactStagingDirectory)/app.jar
displayName: 'Rename JAR file to app.jar'
#Then pack the file to zip (note: includeRootFolder: false - it shouldn't be included any folder)
#Assumed the your_file.jar is one file only in the root folder if not rewrite the task that included only this file to the zip
- task: ArchiveFiles@2
inputs:
rootFolderOrFile: '$(Build.ArtifactStagingDirectory)'
includeRootFolder: false
archiveType: 'zip'
archiveFile: '$(Build.ArtifactStagingDirectory)/package.zip'
replaceExistingArchive: true
displayName: 'Pack JAR file to zip for deployment'
#Just deploy the zip file, during the deployment it will be unpacked to the /home/site/wwwroot folder on App service
- task: AzureRmWebAppDeployment@4
displayName: 'Deploy $(webAppName) with version=$(version) to Azure App Service: $(appServiceName)'
inputs:
azureSubscription: '$(serviceConnectionName)'
appType: 'webAppLinux'
WebAppName: '$(appServiceName)'
runtimeStack: 'JAVA|21-java21'
packageForLinux: '$(Build.ArtifactStagingDirectory)/package.zip'
选项 2:使用 Azure CLI 任务
- task: AzureCLI@2
inputs:
azureSubscription: '$(serviceConnectionName)'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
echo "Deploying your_file.jar to $(appServiceName)"
az webapp deploy --resource-group $(resourceGroup) --name $(appServiceName) --src-path '$(Build.ArtifactStagingDirectory)/your_file.jar' --type jar
displayName: 'Deploy $(webAppName) with version=$(version) to Azure App Service: $(appServiceName)'
我个人更喜欢第一种选择。它运行速度更快,并在部署中心记录更多详细信息。另一方面,第二个选项会等到应用程序在部署后刷新,有时当我们对已部署的应用程序执行其他操作时会很有用。