Simple minds think alike

より多くの可能性を

【GitHub Actions】configuration variablesで機密性の無い変数を設定する

本記事は以下のGitHub公式のGitHub Docsを参照して記載しました。なお、configuration variablesの機能は現在ベータ版のため、仕様が変更される恐れがあります。最新の情報は公式をご参照ください。

docs.github.com

Configuration variables in workflowsとは

以下のGitHubからのAnnouncementの引用によると、非機密データをプレーンテキストとして格納し、GitHub Actionsワークフロー内から参照できる変数のようです。また、組織、環境、リポジトリレベルで変数を定義できるようです。

Configuration variables allows you to store your non sensitive data as plain text variables that can be reused across your workflows in your repository or organization. You can define variables at Organization, Repository or Environment level based on your requirement.

なお、環境(environments)というのは、本番、ステージング、開発などの一般的なデプロイメント対象を表すために使用される機能で、GitHub Actionsワークフローから環境を指定してデプロイが可能になります。この機能は、2023年1月現在公開リポジトリまたは有償プランで利用可能です。

Configuration variablesの特徴

以下の引用によると組織、環境、リポジトリの順番でより狭い設定が優先されるようです。

You can create configuration variables for use across multiple workflows, and can define them at either the organization, repository, or environment level.

For example, you can use configuration variables to set default values for parameters passed to build tools at an organization level, but then allow repository owners to override these parameters on a case-by-case basis.

イメージとしては以下のような状態です。レポジトリの変数userには"repouser"が設定されていて、3つある環境のうちdevelopmentにだけ変数の設定がない状態を示しています。

適用優先度のイメージ図

もし、組織にしか設定されていなければそれが、全リポジトリ、全環境に反映されるという具合です。(ここは未検証です。)

この特徴が、単一リポジトリにしか設定できない環境変数とは明確に異なる点かと思います。

上記の特徴から、機密性の高い情報ではなく環境変数として設定するほどでもないが、大体設定同じでレポジトリ全部に入れて管理するには手間が掛かる。組織全体に変数設定して、少ない個別のレポジトリにだけ別の値を設定したい、という場合に向いていそうです。

Configuration Variablesを参照するには

GitHub Actions のワークフローファイルで、以下のようにvar contextを使うと参照できます。

steps:
- name: Use variables
  run: |
    echo "variable : ${{ vars.REPOSITORY_VAR }}"

Configuration variablesの制限

組織、リポジトリ変数、環境変数の保存可能な個数はそれぞれ、組織変数は1,000個まで、リポジトリ変数は100個まで、環境変数は100個まで保存可能です。

また、変数名にはデフォルトの環境変数の名前は使用できません。

試してみた

上記のイメージと同じように設定してみました。

環境の設定

以下のように3つの環境(development, staging, production)を作ります。 環境一覧

一例として、prduction環境に以下のように変数USER(値はprduser)を設定しています。

変数の追加

リポジトリの設定

リポジトリには変数USER(値はrepouser)を設定しています。 リポジトリの変数設定画面

GitHub Actionsワークフロー作成

以下のworkflow.ymlを追加し、ワークフローを手動実行します。

on:
  workflow_dispatch:
env:
  # Setting an environment variable with the value of a configuration variable
  env_var: ${{ vars.USER }}

jobs:
  display-variables-development:
    runs-on: ubuntu-latest
    environment:
      name: development
    steps:
    - name: Use variables
      run: |
        echo "configuration variable : ${{ vars.USER }}"
        echo "variable from shell environment : $env_var"

  display-variables-staging:
    runs-on: ubuntu-latest
    environment:
      name: staging
    steps:
    - name: Use variables
      run: |
        echo "configuration variable : ${{ vars.USER }}"
        echo "variable from shell environment : $env_var"

  display-variables-production:
    runs-on: ubuntu-latest
    environment:
      name: production
    steps:
    - name: Use variables
      run: |
        echo "configuration variable : ${{ vars.USER }}"
        echo "variable from shell environment : $env_var"

実行結果

development環境だけ、リポジトリで設定した変数が表示されていることを確認できます。

development

環境developmentのワークフロージョブ実行結果

staging

環境stagingのワークフロージョブ実行結果

production

環境productionのワークフロージョブ実行結果

検証リポジトリ

github.com

参考資料