🎈

GitHub Actionsの環境変数をTerraformのオブジェクト型として渡す

に公開

やりたいこと

  • GitHub SecretsにあるパスワードやキーをTerraformに渡す
  • オブジェクト型として渡す
  • Workflowでのオブジェクト宣言を一行にまとめたくない

Workflowの中でTF_VAR_argocd_credentialsという環境変数を宣言して、以下のTerraform変数を渡す

variable "argocd_credentials" {
  type = object({
    oci_repo_username     = string
    oci_repo_password     = string
    repo_url              = string
    repo_ssh_key          = string
  })
  sensitive = true
}

問題

ChatGPTからもらったコードをそのまま使うと、構文エラーが発生する

...
jobs:    
  Terraform_plan_and_apply:
    runs-on: ubuntu-latest

    env:
        # 構文エラー
        TF_VAR_argocd_credentials |-
           "oci_repo_username": ${{inputs.ARGO_HELM_OCI_REPO_USERNAME}},
           "oci_repo_password": ${{secrets.ARGO_HELM_OCI_REPO_PASSWORD}},
           "repo_url": ${{inputs.ARGO_REPO_URL}},
           "repo_ssh_key": ${{secrets.ARGO_REPO_SSH_KEY}}

The template is not valid. my-template.yml@main (Line: 20, Col: 39): A mapping was not expected

解決案

公式ドキュメントを調べると、formatという関数が必要そう

...
jobs:    
  Terraform_plan_and_apply:
    runs-on: ubuntu-latest

    env:
        TF_VAR_argocd_credentials |-
       ${{ format(
            '{{
              "oci_repo_username": "{0}",
              "oci_repo_password": "{1}",
              "repo_url": "{2}",
              "repo_ssh_key": "{3}"
            }}',
            inputs.ARGO_HELM_OCI_REPO_USERNAME,
            secrets.ARGO_HELM_OCI_REPO_PASSWORD,
            inputs.ARGO_REPO_URL,
            secrets.ARGO_REPO_SSH_KEY
        ) }}

しかし、環境変数のデータは数行になると、Terraform Planする際にマルチラインのエラーが発生する

Error: invalid multi-line string
Quoted strings may not be split over multiple lines. To produce a multi-line string, either use the \n escape to represent a newline character or use the "heredoc" multi-line template syntax

toJSONを使えば、データをTerraformが対応できるStringに変換する

toJSON(secrets.MULTIPLE_LINE_STRING)

最後に、全部の環境変数を利用すると、問題なく実行できるようになる

...
jobs:    
  Terraform_plan_and_apply:
    runs-on: ubuntu-latest

    env:
        TF_VAR_argocd_credentials |-
       ${{ format(
            '{{
              "oci_repo_username": {0},
              "oci_repo_password": {1},
              "repo_url": {2},
              "repo_ssh_key": {3}
            }}',
            toJSON(inputs.ARGO_HELM_OCI_REPO_USERNAME),
            toJSON(secrets.ARGO_HELM_OCI_REPO_PASSWORD),
            toJSON(inputs.ARGO_REPO_URL),
            toJSON(secrets.ARGO_REPO_SSH_KEY)
        ) }}

参考

https://zenn.dev/lollipop_onl/articles/gha-conditional-env

https://qiita.com/Hiro_Onozawa/items/2df214e7b2316fe35d6a

Discussion