5
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?

Github ActionsでSecretが取得できないときに疑うこと

Last updated at Posted at 2024-07-30

Github ActionsからSecretを取得できない事象にはまったので解決した方法を残します。

まずEnvironment SecretとRepository Secretの2種類のSecretがあります。
2つのSecretの取得方法は異なります。
ちゃんとGithub上で環境変数を設定し、Actionsのyamlの記述もあっていそうなときは2種類のSecretを混同しているかもしれません。

Environment Secret

こちらはGithub Actionsのyamlでenvironmentを指定してSecretを取得します。
設定 > Code and automation > Environments で作成したらEnvironment Secretとなります。
test-environmentというEnvironmentの中に、VITE_SUPABASE_URL, VITE_SUPABASE_KEYの2つのSecretを作成したのが以下の画像です。
スクリーンショット 2024-07-30 22.23.34.png
スクリーンショット 2024-07-30 22.24.12.png

environmentを指定し、以下のように取得します。

name: Build and Deploy
on:
  [push, workflow_dispatch]
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    environment:
      name: test-environment
    steps:
    - run: echo ${{ secrets.VITE_SUPABASE_URL }}
    - run: echo ${{ secrets.VITE_SUPABASE_KEY }}

Repository Secret

こちらはリポジトリ内で一意のSecretです。
Github Actionsではenvironmentを指定せずに取得できます。
設定 > Secret and Valiables > Actionsで作成したらRepository Secretです。
スクリーンショット 2024-07-30 22.34.47.png

取得は以下です。

name: Build and Deploy
on:
  [push, workflow_dispatch]
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
    - run: echo ${{ secrets.VITE_SUPABASE_URL }}
    - run: echo ${{ secrets.VITE_SUPABASE_KEY }}

使い分けとしては、Repository Secretはリポジトリ内で共通で良いもの。
Environment Secretは開発、本番とでSecretを分けたいときに使用するのが良いと思います。

最後に

私はRepository SecretとEnvironment Secretを混同してしまいSecretが取れない状況にはまってしまいました。
(Environment Secretなのにenviromentsを指定せずに参照しようとしていた)
これを読んだ方がGithunのSecretには2種類あることを知り、エラーが早く解消されると嬉しいです。

5
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
5
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?