Skip to content

Commit

Permalink
Merge pull request #6 from gruntwork-io/feature/pre-exec-code
Browse files Browse the repository at this point in the history
Add support for custom code execution
  • Loading branch information
denis256 authored Jul 20, 2023
2 parents 8f7f8d5 + 29c95d7 commit fc077c6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
43 changes: 33 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# terragrunt-action

A GitHub Action for installing and running Terragrunt

## Inputs
Expand All @@ -13,25 +14,30 @@ Supported GitHub action inputs:
| tg_command | Terragrunt command to execute | `true` |
| tg_comment | Add comment to Pull request with execution output | `false` |

## Environment Variables

Supported environment variables:

| Input Name | Description |
|:----------------------|:------------------------------------------------------------------------------------------------------------|
| GITHUB_TOKEN | GitHub token used to add comment to Pull request |
| TF_LOG | Log level for Terraform |
| TF_VAR_name | Define custom variable name as inputs |
| INPUT_PRE_EXEC_number | Environment variable is utilized to provide custom commands that will be executed before running Terragrunt |

## Outputs

Outputs of GitHub action:

| Input Name | Description |
|:--------------------|:--------------------------------|
| tg_action_exit_code | Terragrunt exit code |
| tg_action_output | Terragrunt output as plain text |

## Environment Variables

Supported environment variables:
* `GITHUB_TOKEN` - GitHub token used to add comment to Pull request
* `TF_LOG` - log level for Terraform
* `TF_VAR_name` - Define custom variable name as inputs
| tg_action_output | Terragrunt output as plain text |

## Usage

Example definition of Github Action workflow:

```yaml
name: 'Terragrunt GitHub Actions'
on:
Expand Down Expand Up @@ -59,7 +65,7 @@ jobs:

plan:
runs-on: ubuntu-latest
needs: [checks]
needs: [ checks ]
steps:
- name: 'Checkout'
uses: actions/checkout@master
Expand All @@ -74,7 +80,7 @@ jobs:

deploy:
runs-on: ubuntu-latest
needs: [plan]
needs: [ plan ]
environment: 'prod'
if: github.ref == 'refs/heads/master'
steps:
Expand All @@ -90,3 +96,20 @@ jobs:
tg_command: 'apply'
```
Example of passing custom code before running Terragrunt:
```yaml
...
- name: Plan
uses: gruntwork-io/terragrunt-action@v1
env:
# configure git to use custom token to clone repository.
INPUT_PRE_EXEC_1: |
git config --global url."https://user:${{secrets.PAT_TOKEN}}@github.com".insteadOf "https://github.com"
# print git configuration
INPUT_PRE_EXEC_2: |
git config --global --list
with:
tg_command: 'plan'
...
```
18 changes: 18 additions & 0 deletions src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ function setup_git {
git config --global --add safe.directory /github/workspace
}

# Run INPUT_PRE_EXEC_* environment variables as Bash code
function setup_pre_exec {
env
# Get all environment variables that match the pattern INPUT_PRE_EXEC_*
local -r pre_exec_vars=$(env | grep -o '^INPUT_PRE_EXEC_[0-9]\+' | sort)
# Loop through each pre-execution variable and execute its value (Bash code)
local pre_exec_command
while IFS= read -r pre_exec_var; do
log "Evaluating ${pre_exec_var}"
pre_exec_command="${!pre_exec_var}"
if [ -n "$pre_exec_command" ]; then
eval "$pre_exec_command"
fi
done <<< "$pre_exec_vars"
}

function main {
log "Starting Terragrunt Action"
trap 'log "Finished Terragrunt Action execution"' EXIT
Expand All @@ -104,6 +120,8 @@ function main {
exit 1
fi
setup_git
setup_pre_exec

install_terraform "${tf_version}"
install_terragrunt "${tg_version}"

Expand Down

0 comments on commit fc077c6

Please sign in to comment.