0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

az loginせずにGithub ActionsでWebAppsにデプロイする

0
Posted at

Github Actionsを利用してWebAppsをデプロイする方法は公式ドキュメントで詳しく紹介されていますが、今回は手順の中にあるAzure/webapps-deployのアクションを使わずデプロイします。

Azure/webapps-deployは認証プロキシに対応しておらず自分の環境では利用できませんでした。
この場合、Actionsの中でaz loginコマンドを使うこともできますが、今回はazure-webapp-maven-pluginに認証情報を登録する形で実行できるようにします。

前提条件

  • プロキシは以下の環境にセルフホストランナーを構築してます
  • セルフホストランナーにはmavenを事前にインストールしています
  • ドキュメントに記載のSpring Bootアプリケーションを作成

pom.xmlを準備

azure-webapp-maven-pluginの設定


mvn com.microsoft.azure:azure-webapp-maven-plugin:2.2.1:config

Please confirm webapp properties
Subscription Id : ********-****-****-****-************
AppName : spring-boot-1599007116351
ResourceGroup : spring-boot-1599007116351-rg
Region : centralus
PricingTier : P1v2
OS : Linux
Web server stack : Java SE
Deploy to slot : false
Confirm (Y/N)? : Y
[INFO] Saving configuration to pom.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20.925 s
[INFO] Finished at: 2020-09-01T17:38:51-07:00
[INFO] ------------------------------------------------------------------------

対話式のコンソールに回答していくだけである程度デプロイに必要な情報をpom.xmlを編集してくれます。
後は直接pom.xmlを編集してAuthの設定をします。

~~~省略~~~
<plugin> 
        <groupId>com.microsoft.azure</groupId>  
        <artifactId>azure-webapp-maven-plugin</artifactId>  
        <version>2.2.0</version>  
        <configuration> 
          <auth>
            <client>${env.APP_ID}</client>
            <tenant>${env.TENANT_ID}</tenant>
            <key>${env.PASSWORD}</key>
            <environment>azure</environment>
          </auth>
          <schemaVersion>v2</schemaVersion>  
          <subscriptionId>my-subscriptionId</subscriptionId>  
          <resourceGroup>my-resourceGroup</resourceGroup>  
          <appName>MyApp</appName>  
          <pricingTier>P1v2</pricingTier>  
          <region>centralus</region>  
          <runtime> 
            <os>Linux</os>  
            <javaVersion>Java 8</javaVersion>  
            <webContainer>Java SE</webContainer> 
          </runtime>  
          <deployment> 
            <resources> 
              <resource> 
                <directory>${project.basedir}/target</directory>  
                <includes> 
                  <include>*.jar</include> 
                </includes> 
              </resource> 
            </resources> 
          </deployment>
          <deploymentSlot>
            <name>staging</name>
            <configurationSource></configurationSource>
          </deploymentSlot>
        </configuration> 
      </plugin>
    </plugins> 
~~~省略~~~

以下の部分は事前にAzure Portalからサービスプリンシパルを作成し、Github ActionsのSecretsに登録しておきます。

<auth>
  <client>${env.APP_ID}</client>
  <tenant>${env.TENANT_ID}</tenant>
  <key>${env.PASSWORD}</key>
  <environment>azure</environment>
</auth>

Github Actionsのワークフローを作成

name: Build and deploy WAR app to Azure Web Apps

on:
  push:
    branches:
      - master

jobs:
  deploy:
    runs-on: <セルフホストランナー名>

    steps:
      - uses: actions/checkout@v2

      - name: Set up Java version
        uses: actions/setup-java@v1
        with:
          java-version: "8"

      - name: Deploy to Azure Web App
        run: |
          mvn package --file ./complete/pom.xml azure-webapp:deploy -Dhttps.proxyHost=${{ secrets.proxyHost }} -Dhttps.proxyPort=${{ secrets.proxyPort }}
        env:
          APP_ID: ${{ secrets.APP_ID }}
          TENANT_ID: ${{ secrets.TENANT_ID }}
          PASSWORD: ${{ secrets.PASSWORD }}

「Deploy to Azure Web App」のステップでは成果物を作成してデプロイを実行するだけの記載で済みました。
※自環境ではプロキシサーバーの設定が必要だったので無理やりコマンドライン引数に渡しています(本来はランナー側の%M2_HOME%/settings.xmlに設定しておくと良いかもしれません)
envにはしっかりと登録したSecretsを設定しておきましょう。

いざ実行

GithubのmasterブランチにpushするとActionsが動き出し、デプロイが実施されます。

pom.xmlに認証の情報を持たせることでセルホストランナー側にAzure CLIをインストールしなくてよいというメリットがあります。
周りくどい実行方法かもしれませんが、実行環境に制限があるような方の助けになれば幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?