搭配 GitHub Actions 使用 Azure Spring Apps CI/CD

注意

Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但在我們努力更新資產,例如螢幕快照、影片和圖表時,您會在某些地方看到舊名稱一段時間。

本文適用於: ✔️基本/標準✔️企業

本文說明如何使用 GitHub Actions 建置 Azure Spring Apps 的 CI/CD 工作流程。

GitHub Actions 支援自動化軟體開發生命週期工作流程。 使用適用於 Azure Spring Apps 的 GitHub Actions,您可以在存放庫中建立工作流程,以建置、測試、封裝、發行和部署至 Azure。

必要條件

此範例需要 Azure CLI

設定 GitHub 存放庫並驗證

您需要 Azure 服務主體認證,才能授權 Azure 登入動作。 若要取得 Azure 認證,請在本機電腦上執行下列命令:

az login
az ad sp create-for-rbac \
    --role contributor \
    --scopes /subscriptions/<SUBSCRIPTION_ID> \
    --json-auth

若要存取特定資源群組,您可以減少範圍:

az ad sp create-for-rbac \
    --role contributor \
    --scopes /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP> \
    --json-auth

這個指令應該會輸出 JSON 物件:

{
    "clientId": "<GUID>",
    "clientSecret": "<GUID>",
    "subscriptionId": "<GUID>",
    "tenantId": "<GUID>",
    ...
}

此範例使用 GitHub 上的 steeltoe 範例。 分支存放庫、開啟分支的 GitHub 存放庫頁面,然後選取 [設定] 索引標籤。開啟 [秘密] 功能表,然後選取 [新增秘密]:

Screenshot of the GitHub Actions secrets and variables page with the New repository secret button highlighted.

將秘密名稱設定為 AZURE_CREDENTIALS ,並將其值設定為您在設定 GitHub 存放庫並驗證標題底下找到的 JSON 字串。

Screenshot of the GitHub Actions secrets / New secret page.

您也可以從 GitHub Actions 中的 金鑰保存庫 取得 Azure 登入認證,如使用 GitHub Actions 中的 金鑰保存庫 驗證 Azure Spring 中所述

布建服務實例

若要布建 Azure Spring Apps 服務實例,請使用 Azure CLI 執行下列命令。

az extension add --name spring
az group create \
    --name <resource-group-name> \
    --location eastus 
az spring create \
    --resource-group <resource-group-name> \
    --name <service-instance-name> 
az spring config-server git set \
    --name <service-instance-name> \
    --uri https://github.com/Azure-Samples/azure-spring-apps-samples \
    --label main \
    --search-paths steeltoe-sample/config

建置工作流程

工作流程是使用下列選項來定義。

準備使用 Azure CLI 進行部署

命令 az spring app create 目前不是等冪的。 執行一次之後,如果您再次執行相同的命令,就會收到錯誤。 我們建議在現有的 Azure Spring Apps 應用程式和實例上使用此工作流程。

使用下列 Azure CLI 命令來準備:

az config set defaults.group=<service-group-name>
az config set defaults.spring=<service-instance-name>
az spring app create --name planet-weather-provider
az spring app create --name solar-system-weather

直接使用 Azure CLI 進行部署

使用下列內容,在存放庫中建立 .github/workflows/main.yml 檔案。 以正確的值取代資源組名>和服務<名稱>。<

name: Steeltoe-CD

# Controls when the action runs. Triggers the workflow on push or pull request
# events but only for the main branch
on:
  push:
    branches: [ main]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job runs on
    runs-on: ubuntu-latest
    env:
      working-directory: ./steeltoe-sample
      resource-group-name: <your resource group name>
      service-name: <your service name>

    # Supported .NET Core version matrix.
    strategy:
      matrix:
        dotnet: [ '3.1.x' ]

    # Steps represent a sequence of tasks that is executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      # Set up .NET Core 3.1 SDK
      - uses: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ matrix.dotnet }}

      # Set credential for az login
      - uses: azure/login@v1.1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: install Azure CLI extension
        run: |
          az extension add --name spring --yes

      - name: Build and package planet-weather-provider app
        working-directory: ${{env.working-directory}}/src/planet-weather-provider
        run: |
          dotnet publish
          az spring app deploy -n planet-weather-provider --runtime-version NetCore_31 --main-entry Microsoft.Azure.SpringCloud.Sample.PlanetWeatherProvider.dll --artifact-path ./publish-deploy-planet.zip -s ${{ env.service-name }} -g ${{ env.resource-group-name }}
      - name: Build solar-system-weather app
        working-directory: ${{env.working-directory}}/src/solar-system-weather
        run: |
          dotnet publish
          az spring app deploy -n solar-system-weather --runtime-version NetCore_31 --main-entry Microsoft.Azure.SpringCloud.Sample.SolarSystemWeather.dll --artifact-path ./publish-deploy-solar.zip -s ${{ env.service-name }} -g ${{ env.resource-group-name }}

設定 GitHub 存放庫並驗證

您需要 Azure 服務主體認證,才能授權 Azure 登入動作。 若要取得 Azure 認證,請在本機電腦上執行下列命令:

az login
az ad sp create-for-rbac \
    --role contributor \
    --scopes /subscriptions/<SUBSCRIPTION_ID> \
    --json-auth

若要存取特定資源群組,您可以減少範圍:

az ad sp create-for-rbac \
    --role contributor \
    --scopes /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP> \
    --json-auth

這個指令應該會輸出 JSON 物件:

{
    "clientId": "<GUID>",
    "clientSecret": "<GUID>",
    "subscriptionId": "<GUID>",
    "tenantId": "<GUID>",
    ...
}

此範例使用 GitHub 上的 PiggyMetrics 範例。 分叉範例,取消核取 [僅複製 Azure 分支],開啟 [GitHub 存放庫] 頁面,然後選取 [設定] 索引卷標。開啟 [秘密] 功能表,然後選取 [新增秘密]:

Screenshot of the GitHub Actions secrets and variables page with the New repository secret button highlighted.

將秘密名稱設定為 AZURE_CREDENTIALS ,並將其值設定為您在設定 GitHub 存放庫並驗證標題底下找到的 JSON 字串。

Screenshot of the GitHub Actions secrets / New secret page.

您也可以從 GitHub Actions 中的 金鑰保存庫 取得 Azure 登入認證,如使用 GitHub Actions 中的 金鑰保存庫 驗證 Azure Spring 中所述

布建服務實例

若要布建 Azure Spring Apps 服務實例,請使用 Azure CLI 執行下列命令。

az extension add --name spring
az group create --location eastus --name <resource group name>
az spring create -n <service instance name> -g <resource group name>
az spring config-server git set -n <service instance name> --uri https://github.com/xxx/piggymetrics --label config

端對端範例工作流程

下列範例示範常見的使用案例。

部署

下列各節說明部署應用程式的各種選項。

生產環境

Azure Spring Apps 支援部署至具有建置成品的部署(例如 JAR 或 .NET Core ZIP)或原始程式碼封存。

下列範例會使用 Maven 所建置的 JAR 檔案,部署至 Azure Spring Apps 中的預設生產部署。 此範例是使用基本 SKU 時唯一可能的部署案例:

注意

套件搜尋模式應該只傳回一個套件。 如果建置工作產生多個 JAR 套件,例如 sources.jarjavadoc.jar,您需要精簡搜尋模式,使其只符合應用程式二進製成品。

name: AzureSpringApps
on: push
env:
  ASC_PACKAGE_PATH: ${{ github.workspace }}
  AZURE_SUBSCRIPTION: <azure subscription name>

jobs:
  deploy_to_production:
    runs-on: ubuntu-latest
    name: deploy to production with artifact
    steps:
      - name: Checkout GitHub Action
        uses: actions/checkout@v2

      - name: Set up Java 11
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: '11'

      - name: maven build, clean
        run: |
          mvn clean package

      - name: Login via Azure CLI
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: deploy to production with artifact
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: Deploy
          service-name: <service instance name>
          app-name: <app name>
          use-staging-deployment: false
          package: ${{ env.ASC_PACKAGE_PATH }}/**/*.jar

下列範例會使用原始程式碼部署至 Azure Spring Apps 中的預設生產環境部署。

name: AzureSpringApps
on: push
env:
  ASC_PACKAGE_PATH: ${{ github.workspace }}
  AZURE_SUBSCRIPTION: <azure subscription name>

jobs:
  deploy_to_production:
    runs-on: ubuntu-latest
    name: deploy to production with source code
    steps:
      - name: Checkout GitHub Action
        uses: actions/checkout@v2

      - name: Login via Azure CLI
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: deploy to production step with source code
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: deploy
          service-name: <service instance name>
          app-name: <app name>
          use-staging-deployment: false
          package: ${{ env.ASC_PACKAGE_PATH }}

下列範例會使用企業方案中的原始程式碼,部署至 Azure Spring Apps 中的預設生產環境部署。 您可以使用 選項指定要用於部署動作的 builder 產生器。

name: AzureSpringApps
on: push
env:
  ASC_PACKAGE_PATH: ${{ github.workspace }}
  AZURE_SUBSCRIPTION: <azure subscription name>

jobs:
  deploy_to_production:
    runs-on: ubuntu-latest
    name: deploy to production with source code
    steps:
      - name: Checkout GitHub Action
        uses: actions/checkout@v2

      - name: Login via Azure CLI
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: deploy to production step with source code in the Enterprise plan
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: deploy
          service-name: <service instance name>
          app-name: <app name>
          use-staging-deployment: false
          package: ${{ env.ASC_PACKAGE_PATH }}
          builder: <builder>

下列範例會使用現有的容器映射,部署到 Azure Spring Apps 中的默認生產環境部署。

name: AzureSpringApps
on: push
env:
  ASC_PACKAGE_PATH: ${{ github.workspace }}
  AZURE_SUBSCRIPTION: <azure subscription name>

jobs:
  deploy_to_production:
    runs-on: ubuntu-latest
    name: deploy to production with source code
    steps:
      - name: Checkout GitHub Action
        uses: actions/checkout@v2

      - name: Login via Azure CLI
        uses: azure/login@v1
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}

      - name: Deploy Custom Image
        uses: Azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: deploy
          service-name: <service instance name>
          app-name: <app name>
          deployment-name: <deployment name>
          container-registry: <your container image registry>
          registry-username: ${{ env.REGISTRY_USERNAME }}
          registry-password: ${{ secrets.REGISTRY_PASSWORD }}
          container-image: <your image tag>

在部署期間,您可以使用更多自變數來達成更多功能。 如需詳細資訊,請參閱 GitHub Action 的自變數一節,以部署至 Azure Spring Apps。

藍綠

下列範例會部署到現有的預備部署。 此部署在設定為生產環境部署之前,不會收到生產流量。 您可以設定 use-staging-deployment true,以自動尋找預備部署,或只配置特定的部署名稱。 我們只關注行動, spring-apps-deploy 排除文章其餘部分的準備工作。

# environment preparation configurations omitted
    steps:
      - name: blue green deploy step use-staging-deployment
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: deploy
          service-name: <service instance name>
          app-name: <app name>
          use-staging-deployment: true
          package: ${{ env.ASC_PACKAGE_PATH }}/**/*.jar
# environment preparation configurations omitted
    steps:
      - name: blue green deploy step with deployment-name
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: deploy
          service-name: <service instance name>
          app-name: <app name>
          deployment-name: staging
          package: ${{ env.ASC_PACKAGE_PATH }}/**/*.jar

如需藍綠部署的詳細資訊,包括替代方法,請參閱 藍綠部署策略

設定生產環境部署

下列範例會將目前的預備部署設定為生產環境,有效地交換哪些部署會接收生產流量。

# environment preparation configurations omitted
    steps:
      - name: set production deployment step
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: set-production
          service-name: <service instance name>
          app-name: <app name>
          use-staging-deployment: true

刪除預備部署

動作 Delete Staging Deployment 可讓您刪除未接收生產流量的部署。 此刪除可釋出該部署所使用的資源,並騰出空間進行新的預備部署:

# environment preparation configurations omitted
    steps:
      - name: Delete staging deployment step
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: delete-staging-deployment
          service-name: <service instance name>
          app-name: <app name>

建立或更新組建 (僅限企業方案)

下列範例會在 Enterprise 方案中建立或更新組建資源:

# environment preparation configurations omitted
    steps:
      - name: Create or update build
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: build
          service-name: <service instance name>
          build-name: <build name>
          package: ${{ env.ASC_PACKAGE_PATH }}
          builder: <builder>

移除組建 (只限企業方案)

下列範例會刪除 Enterprise 方案中的組建資源:

# environment preparation configurations omitted
    steps:
      - name: Delete build
        uses: azure/spring-apps-deploy@v1
        with:
          azure-subscription: ${{ env.AZURE_SUBSCRIPTION }}
          action: delete-build
          service-name: <service instance name>
          build-name: <build name>

使用 Maven 外掛程式進行部署

另一個選項是使用 Maven 外掛程式 來部署 Jar 和更新應用程式設定。 此命令 mvn azure-spring-apps:deploy 是等冪的,並視需要自動建立應用程式。 您不需要事先建立對應的應用程式。

name: AzureSpringApps
on: push

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:

    - uses: actions/checkout@main

    - name: Set up Java 11
      uses: actions/setup-java@v3
      with:
        distribution: 'temurin'
        java-version: '11'

    - name: maven build, clean
      run: |
        mvn clean package -DskipTests

    # Maven plugin can cosume this authentication method automatically
    - name: Azure Login
      uses: azure/login@v1
      with:
        creds: ${{ secrets.AZURE_CREDENTIALS }}

    # Maven deploy, make sure you have correct configurations in your pom.xml
    - name: deploy to Azure Spring Apps using Maven
      run: |
        mvn azure-spring-apps:deploy

執行工作流程

當您將 .github/workflow/main.yml推送至 GitHub 之後,應該會自動啟用 GitHub Actions。 當您推送新的認可時,就會觸發動作。 如果您在瀏覽器中建立此檔案,您的動作應該已經執行。

若要確認動作已啟用,請選取 GitHub 存放庫頁面上的 [動作] 索引標籤:

Screenshot of the GitHub Actions tab showing the All workflows section.

例如,如果您的動作發生錯誤,例如,如果您尚未設定 Azure 認證,您可以在修正錯誤之後重新執行檢查。 在 [GitHub 存放庫] 頁面上,選取 [動作],選取特定的工作流程工作,然後選取 [重新執行檢查 ] 按鈕以重新執行檢查:

Screenshot of the GitHub Actions tab with the Re-run checks button highlighted.

下一步