Flask動的WebサイトをAzureにGithubからDeployする

Summary

  • Azure web appsを使う。
  • GithubからGithub Actionsを使ってDeployする。

[12/8/2019追記]

GithubからDeployする方法だと、Pythonの依存パッケージの自動インストール等が動かなかった。今はLocal gitから直接AzureにPushしている。

[7/11/2021更新]

ある環境変数を設定すると、GithubからのDeployでもパッケージのインストールがされるらしい。本文中に追記した。

Repository

リポジトリ内にFlask用のapp.pyと、依存パッケージのリストであるrequirements.txtを作成する。任意のディレクトリを使ってよい。Github Actions内でそのディレクトリを指定する。

Azure Web Apps

普通にAzure Web Appsリソースを作成する。Freeプランだと後々Custom domainやSSLの設定ができないので、一番安いPaidプランを使った。

次に、Publish profileをダウンロードする。このファイルにはDeployに必要なすべての情報が含まれている。秘密の情報も含まれているので取り扱いには注意。


デフォルトだと、依存パッケージのインストールは行われない。Web AppsのSettingsページから、SCM_DO_BUILD_DURING_DEPLOYMENT=1という環境変数を登録しなければならない。詳細は以下のページに書いてある。
https://docs.microsoft.com/en-us/azure/app-service/configure-language-python


Github Actions

WebのUIからActionの新規作成。テンプレートとしてDeploy Node.js to Azure Web Appを使うのが便利だった。先ほどダウンロードしたPublish profileファイルの中身をProjectのSettingsからSecretにAZURE_WEBAPP_PUBLISH_PROFILEとして登録する。そしてテンプレートの中身を用途に合わせて編集しCommit。

自分はPythonのFlaskフレームワークを使っているので、Node.js関連のコードを削除し、以下のような設定とした。

# This workflow will build and push an application to an Azure Web App on every push to the master branch.
#
# To configure this workflow:
#
# 1. Set up a secret in your repository named AZURE_WEBAPP_PUBLISH_PROFILE with the value of your Azure publish profile.
#
# 2. Change the values for the AZURE_WEBAPP_NAME and AZURE_WEBAPP_PACKAGE_PATH environment variables  (below).
#
# For more information on GitHub Actions for Azure, refer to https://github.com/Azure/Actions
# For more samples to get started with GitHub Action workflows to deploy to Azure, refer to https://github.com/Azure/actions-workflow-samples
on:
  push:
    branches:
      - master

env:
  AZURE_WEBAPP_NAME: test # set this to your application's name
  AZURE_WEBAPP_PACKAGE_PATH: '.'      # set this to the path to your web app project, defaults to the repository root

jobs:
  build-and-deploy:
    name: Build and Deploy
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: 'Deploy to Azure WebApp'
      uses: azure/webapps-deploy@v1
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
        package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}

Comments

Popular posts from this blog

LinuxでUSBカメラ映像をストリーミングかつ録画する

Raspberry pi 4とUbuntu Server 20.04 64bitでRaspberry pi cameraを使う

WSL2上でSSH経由のSOCKSプロキシを作りWindowsから使う