Ccmmutty logo
Commutty IT
4 min read

【GitHub Actions】GHAでCloud Functionsをデプロイする

https://cdn.magicode.io/media/notebox/0478a311-df4b-49d5-8391-6c239cd742e5.jpeg

workflow

name: Deploy Cloud Funcstions

on:
  workflow_dispatch:
  push:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: 'actions/checkout@v3'

    - id: 'auth'
      uses: 'google-github-actions/auth@v0'
      with:
        credentials_json: '${{ secrets.GCP_CREDENTIALS }}'

    - id: 'deploy'
      uses: 'google-github-actions/deploy-cloud-functions@v0'
      with:
        name: 'test-function'
        runtime: 'python38'
        region: 'asia-northeast1'
        source_dir: 'app'
        entry_point: 'main'
        event_trigger_type: 'providers/cloud.pubsub/eventTypes/topic.publish'
        event_trigger_resource: 'projects/my-project/topics/test-topic'
↓Googleさんが用意してくれているActionsのREADME.mdを参考にしてます
https://github.com/google-github-actions/deploy-cloud-functions

事前準備

  • Pub/Subのtopicを用意する
    • ※httpをトリガーにする場合は必要無し
  • Cloud Functionsデプロイ用のサービスアカウントを作成
    • 「Cloud Functions 管理者」と「サービスアカウント ユーザ」の権限を付与

Pub/Subのtopicを用意する

下記リンクにて「トピックの作成」からトピックを作成する
  • 名前は任意、設定値はデフォルトでOK

Cloud Functionsデプロイ用のサービスアカウントを作成

下記リンクにて「サービスアカウントの作成」からサービスアカウントを作成する
  • 名前は任意
「Cloud Functions 管理者」と「サービスアカウント ユーザ」の権限を付与
  • 先ほど作成したサービスアカウントの"プリンシパルID"をコピー
  • 下記リンクから「アクセス権を付与」をクリック
  • 「新しいプリンシパル」欄に先程コピーしたプリンシパルIDを入力後、下記2つの権限を付与し、保存する
    • 「Cloud Functions 管理者」
    • 「サービスアカウント ユーザ」

worlflowの解説

on:
workflow_dispatch:
push:
branches: [ master ]
手動、もしくはmasterへのマージでActionsが動く
  • id: 'auth'
    uses: 'google-github-actions/auth@v0'
    with:
    credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
GCP_CREDENTIALSにはサービス アカウント キーを設定。
Cloud Functionsデプロイ用のサービスアカウントに対して新しい鍵をJSONで作成し、そのファイルの中身を設定する
  • id: 'deploy'
    uses: 'google-github-actions/deploy-cloud-functions@v0'
    with:
    name: 'test-function'
    runtime: 'python38'
    region: 'asia-northeast1'
    source_dir: 'app'
    entry_point: 'main'
    event_trigger_type: 'providers/cloud.pubsub/eventTypes/topic.publish'
    event_trigger_resource: 'projects/my-project/topics/test-topic'
デプロイする関数の設定。
パラメータ説明
nameデプロイする関数名
runtime関数を実行する環境
region関数を実行するリージョン
source_dirソースのディレクトリ。ここで指定したディレクトリ配下がデプロイされる
entry_point実行する関数名
event_trigger_typeトリガーのタイプ。デフォルトはhttpだが今回はtopicを設定
event_trigger_resourceトピックの名前

Discussion

コメントにはログインが必要です。