hashicorp / setup-terraform

Sets up Terraform CLI in your GitHub Actions workflow.
https://developer.hashicorp.com/terraform/tutorials/automation/github-actions
Mozilla Public License 2.0
1.37k stars 241 forks source link

Get files from global folder #309

Closed kaefferlein closed 1 year ago

kaefferlein commented 1 year ago

Hi all,

I have a question regarding the scopes of this action.

First of all let me show you the simplified repo structure:

project
└─── .github 
    │   some workflow files
└─── configuration
    │   containing some JSON files with customer details
└─── azure
    │   containing .tf files regarding azure resources
└─── gcp
    │   containing .tf files regarding GCP resources

Both folders contain terraform files for setting up resources in either Azure or GCP. Workspaces are located in Terraform Enterprise. Each folder (gcp and azure) reside in an own workspace. Both folders rely on information stored in the project/configuration/<someconfig>.json folder.

Unfortunately I did not find a way to use the global configuration folder from the GitHub action part. Instead I had to copy the configuration folder to the azure and gcp folder, resulting in a repo structure like that:

project
└─── .github 
    │   some workflow files
└─── configuration
    │   containing some JSON files with customer details
└─── azure
    └─── configuration 
    │   containing .tf files regarding azure resources
└─── gcp
    └─── configuration 
    │   containing .tf files regarding GCP resources

As I want to avoid duplicate code (even if it's just copied/pasted via another GH action) I wanted to know if it's possible to stick to the first example repo structure and get the contents of the configuration files from the global folder.

Following you can see an excerpt from the GH action:

name: "Terraform"

env:
  work-dir: gcp
  cli_config_credentials_hostname: my-host.com

on:
  push:
    branches:
      - gha # todo change to dev
  pull_request:

jobs:
  github:
    name: "Terraform run for GCP"
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
      contents: read
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v2
        with:
          terraform_version: 1.3.7
          cli_config_credentials_token: ${{ secrets.TOKEN }}
          cli_config_credentials_hostname: ${{ env.cli_config_credentials_hostname }}

      - name: Terraform Init
        id: init
        run: terraform init
        working-directory: ${{ env.work-dir }}

      - name: Terraform Validate
        id: validate
        run: terraform validate -no-color
        working-directory: ${{ env.work-dir }}

      - name: Terraform Plan
        id: plan
        run: terraform  plan -input=false
        working-directory: ${{ env.work-dir }}
        continue-on-error: true

Within Terraform I get the files like that (note: when getting the files from the copied configuration sub folders):

fileList = fileset(path.module, "customers/**/*.json")

Any help would be highly appreciated. :)

Best, Anna

kaefferlein commented 1 year ago

nvm... 🤦‍♀️

you need to specify the working directory both in the action itself as well as in the workspace in terraform cloud/enterprise.

github-actions[bot] commented 4 months ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.