Skip to content
This repository was archived by the owner on May 6, 2020. It is now read-only.

Commit 25487d5

Browse files
authored
Adding CLI credentials file (#118)
1 parent 9b888f8 commit 25487d5

File tree

7 files changed

+78
-15
lines changed

7 files changed

+78
-15
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,19 @@ This was a simplified example showing the basic features of these Terraform GitH
6969

7070
Inputs configure Terraform GitHub Actions to perform different actions.
7171

72-
* `tf_actions_version` - (Required) The Terraform version to install and execute.
7372
* `tf_actions_subcommand` - (Required) The Terraform subcommand to execute. Valid values are `fmt`, `init`, `validate`, `plan`, and `apply`.
74-
* `tf_actions_working_dir` - (Optional) The working directory to change into before executing Terraform subcommands. Defaults to `.` which means use the root of the GitHub repository.
73+
* `tf_actions_version` - (Required) The Terraform version to install and execute.
74+
* `tf_actions_cli_credentials_hostname` - (Optional) Hostname for the CLI credentials file. Defaults to `app.terraform.io`.
75+
* `tf_actions_cli_credentials_token` - (Optional) Token for the CLI credentials file.
7576
* `tf_actions_comment` - (Optional) Whether or not to comment on GitHub pull requests. Defaults to `true`.
77+
* `tf_actions_working_dir` - (Optional) The working directory to change into before executing Terraform subcommands. Defaults to `.` which means use the root of the GitHub repository.
7678

7779
## Outputs
7880

7981
Outputs are used to pass information to subsequent GitHub Actions steps.
8082

81-
* `tf_actions_plan_has_changes` - Whether or not the Terraform plan contained changes.
8283
* `tf_actions_output` - The Terraform outputs in JSON format.
84+
* `tf_actions_plan_has_changes` - Whether or not the Terraform plan contained changes.
8385

8486
## Secrets
8587

action.yml

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,28 @@ branding:
55
icon: 'terminal'
66
color: 'purple'
77
inputs:
8-
tf_actions_version:
9-
description: 'Terraform version to install.'
10-
required: true
118
tf_actions_subcommand:
129
description: 'Terraform subcommand to execute.'
1310
required: true
14-
tf_actions_working_dir:
15-
description: 'Terraform working directory.'
16-
default: '.'
11+
tf_actions_version:
12+
description: 'Terraform version to install.'
13+
required: true
14+
tf_actions_cli_credentials_hostname:
15+
description: 'Hostname for the CLI credentials file.'
16+
default: 'app.terraform.io'
17+
tf_actions_cli_credentials_token:
18+
description: 'Token for the CLI credentials file.'
1719
tf_actions_comment:
1820
description: 'Whether or not to comment on pull requests.'
1921
default: true
22+
tf_actions_working_dir:
23+
description: 'Terraform working directory.'
24+
default: '.'
2025
outputs:
21-
tf_actions_plan_has_changes:
22-
description: 'Whether or not the Terraform plan contained changes.'
2326
tf_actions_output:
2427
description: 'The Terraform outputs in JSON format.'
28+
tf_actions_plan_has_changes:
29+
description: 'Whether or not the Terraform plan contained changes.'
2530
runs:
2631
using: 'docker'
2732
image: './Dockerfile'

examples/arguments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
tf_actions_subcommand: 'init'
2525
tf_actions_working_dir: '.'
2626
tf_actions_comment: true
27-
args: '-var="env=dev"'
27+
args: '-var="env=dev"'
2828
env:
2929
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3030
```

examples/backends.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
tf_actions_subcommand: 'init'
2323
tf_actions_working_dir: '.'
2424
tf_actions_comment: true
25-
args: '-backend-config="token=${{ secrets.TF_API_TOKEN }}" -backend-config="organization=CHANGE_ME"'
25+
args: '-backend-config="token=${{ secrets.TF_API_TOKEN }}" -backend-config="organization=CHANGE_ME"'
2626
env:
2727
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2828
```

examples/credentials-file.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Terraform CLI Credentials File
2+
3+
The Terraform CLI credentials file is used to authenticate to Terraform Cloud/Enterprise. This is useful if the Terraform configuration contains many `terraform_remote_state` data sources that read from the same Terraform Cloud/Enterprise instance or if the configuration uses modules located in the Private Module Registry.
4+
5+
This example shows how to pass the hostname and token needed to create the CLI credentials file.
6+
7+
```yaml
8+
name: 'Terraform GitHub Actions'
9+
on:
10+
- pull_request
11+
jobs:
12+
terraform:
13+
name: 'Terraform'
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: 'Checkout'
17+
uses: actions/checkout@master
18+
- name: 'Terraform Init'
19+
uses: hashicorp/terraform-github-actions@master
20+
with:
21+
tf_actions_version: 0.12.13
22+
tf_actions_subcommand: 'init'
23+
tf_actions_working_dir: '.'
24+
tf_actions_comment: true
25+
tf_actions_cli_credentials_hostname: app.terraform.io
26+
tf_actions_cli_credentials_token: ${{ secrets.TF_API_TOKEN }}
27+
env:
28+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
29+
- name: 'Terraform Plan'
30+
uses: hashicorp/terraform-github-actions@master
31+
with:
32+
tf_actions_version: 0.12.13
33+
tf_actions_subcommand: 'plan'
34+
tf_actions_working_dir: '.'
35+
```

examples/variables.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
tf_actions_subcommand: 'init'
2525
tf_actions_working_dir: '.'
2626
tf_actions_comment: true
27-
args: '-var="env=dev"'
27+
args: '-var="env=dev"'
2828
env:
2929
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3030
```
@@ -49,7 +49,7 @@ jobs:
4949
tf_actions_subcommand: 'init'
5050
tf_actions_working_dir: '.'
5151
tf_actions_comment: true
52-
args: '-var-file="dev.tfvars"'
52+
args: '-var-file="dev.tfvars"'
5353
env:
5454
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5555
```

src/main.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ function parseInputs {
4141
if [ "${INPUT_TF_ACTIONS_COMMENT}" == "1" ] || [ "${INPUT_TF_ACTIONS_COMMENT}" == "true" ]; then
4242
tfComment=1
4343
fi
44+
45+
tfCLICredentialsHostname=""
46+
if [ "${INPUT_TF_ACTIONS_CLI_CREDENTIALS_HOSTNAME}" != "" ]; then
47+
tfCLICredentialsHostname=${INPUT_TF_ACTIONS_CLI_CREDENTIALS_HOSTNAME}
48+
fi
49+
50+
tfCLICredentialsToken=""
51+
if [ "${INPUT_TF_ACTIONS_CLI_CREDENTIALS_TOKEN}" != "" ]; then
52+
tfCLICredentialsToken=${INPUT_TF_ACTIONS_CLI_CREDENTIALS_TOKEN}
53+
fi
54+
}
55+
56+
function configureCLICredentials {
57+
if [[ ! -f "${HOME}/.terraformrc" ]] && [[ "${tfCLICredentialsToken}" != "" ]]; then
58+
cat > ${HOME}/.terraformrc << EOF
59+
credentials "${tfCLICredentialsHostname}" {
60+
token = "${tfCLICredentialsToken}"
61+
}
62+
EOF
63+
fi
4464
}
4565

4666
function installTerraform {
@@ -74,6 +94,7 @@ function main {
7494
source ${scriptDir}/terraform_output.sh
7595

7696
parseInputs
97+
configureCLICredentials
7798
cd ${GITHUB_WORKSPACE}/${tfWorkingDir}
7899

79100
case "${tfSubcommand}" in

0 commit comments

Comments
 (0)