如何通过 CICD (Bitbucket/Git) 在部署后自动重启 Azure Web 应用程序?

问题描述 投票:0回答:1

我正在尝试将 Spring Boot 应用程序部署到 Azure Web 应用程序/应用程序服务。 Azure Web 应用程序更改未反映通过 CICD(bitbucket/Git)部署新更改后的情况。它反映了部署后手动重新启动后的情况。我想自动化它。我尝试在 .yaml 文件的部署步骤中添加

Restart-AzWebAppSlot
但没有成功。

下面是示例 .yaml 文件

name: Build and deploy JAR app to Azure Web App - sp-az-webapp

on:
  push:
    branches:
      - master
  workflow_dispatch:

jobs:
  build:
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Java version
        uses: actions/setup-java@v1
        with:
          java-version: '17'

      - name: Build with Maven
        run: mvn clean install

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v3
        with:
          name: java-app
          path: '${{ github.workspace }}/target/*.jar'

  deploy:
    runs-on: windows-latest
    needs: build
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: java-app

      - name: Deploy to Azure Web App
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v2
        with:
          app-name: 'sp-az-webapp'
          slot-name: 'Production'
          publish-profile: ${{  }}
          package: '*.jar'
      - name: Restart Azure Web App
        id: restart-webapp
        uses: azure/Restart-AzWebAppSlot@v2
        with:
          app-name: 'sp-az-webapp'
          slot-name: 'Production'
          publish-profile: ${{  }}
          package: '*.jar'
spring-boot azure azure-web-app-service azure-appservice azure-webapps
1个回答
0
投票

我尝试在 .yaml 文件的部署步骤中添加

Restart-AzWebAppSlot
但没有成功。

  • Azure 没有名为
    Restart-AzWebAppSlot
    的内置 GitHub 操作,这就是它无法按预期运行的原因。
  • 没有名为该名称的官方 GitHub 操作。
  • Azure 的 GitHub 操作需要特定命令或 Azure CLI 步骤来处理重新启动等任务。

使用 Azure CLI (

az webapp restart
) 执行重新启动。在部署步骤之后,可以将 Azure CLI 命令合并到管道中。

.yaml 文件:

name: Build and deploy JAR app to Azure Web App - sp-az-webapp

on:
  push:
    branches:
      - master
  workflow_dispatch:

jobs:
  build:
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Java version
        uses: actions/setup-java@v1
        with:
          java-version: '17'

      - name: Build with Maven
        run: mvn clean install

      - name: Upload artifact for deployment job
        uses: actions/upload-artifact@v3
        with:
          name: java-app
          path: '${{ github.workspace }}/target/*.jar'

  deploy:
    runs-on: windows-latest
    needs: build
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v3
        with:
          name: java-app

      - name: Deploy to Azure Web App
        id: deploy-to-webapp
        uses: azure/webapps-deploy@v2
        with:
          app-name: 'sp-az-webapp'
          slot-name: 'Production'
          publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
          package: '*.jar'

      # Restart the Azure Web App after deployment
      - name: Restart Azure Web App
        run: |
          az webapp restart --name sp-az-webapp --resource-group <your-resource-group> --slot Production

我使用 Azure CLI 使用

az webapp restart
命令重新启动 Web 应用程序。

构建状态:

Starting job: Build
Checking out repository...
> actions/checkout@v4
✓ Checked out repository code

Setting up Java version...
> actions/setup-java@v1
✓ Set Java version to 17

Building project with Maven...
$ mvn clean install
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.example:my-app >------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ my-app ---
[INFO] Deleting /home/runner/work/my-app/target
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ my-app ---
[INFO] Copying resources to /home/runner/work/my-app/target/classes
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ my-app ---
[INFO] Compiling 20 source files to /home/runner/work/my-app/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ my-app ---
[INFO] Copying resources to /home/runner/work/my-app/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ my-app ---
[INFO] Tests are passing
[INFO] 
[INFO] --- maven-jar-plugin:3.1.0:jar (default-jar) @ my-app ---
[INFO] Building jar: /home/runner/work/my-app/target/my-app-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ my-app ---
[INFO] Installing /home/runner/work/my-app/target/my-app-1.0-SNAPSHOT.jar to local repository

[INFO] Build success

工作部署:

Starting job: Deploy
Downloading artifact from build job...
> actions/download-artifact@v3
Artifact 'java-app' downloaded successfully.

Deploying to Azure Web App...
> azure/webapps-deploy@v2
[INFO] Using publish profile from secret.
[INFO] Deploying jar file: /home/runner/work/my-app/target/my-app-1.0-SNAPSHOT.jar to app 'sp-az-webapp'
✓ Deployment successful.
Web App URL: https://sp-az-webapp.azurewebsites.net

Azure 登录:

Logging into Azure...
> azure/login@v1
Using Service Principal credentials from secret: 'AZURE_CREDENTIALS'
✓ Azure login successful.

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.