Skip to content

Commit

Permalink
Merge pull request #20 from wayofdev/develop
Browse files Browse the repository at this point in the history
feat: added cache folders and variables
  • Loading branch information
Fenikks authored Jan 8, 2024
2 parents 3ffdb68 + d8de2c6 commit f774142
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 11 deletions.
40 changes: 40 additions & 0 deletions gh-action-terragrunt-apply/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,46 @@ These input values must be the same as any wayofdev/gh-action-terragrunt-plan fo
- Optional
- Default: parallel

* `create_cache_folder_in_workspace`

Set to true to create a cache folder in workspace. It can be reused in other steps, jobs and workflows. By default it created in /tmp folder inside docker container and not available outside.

- Type: boolean
- Optional
- Default: false

* `cache_folder`

Specify custom name for the cache folder which will be created in github workspace. Two additional folders will be created inside `cache_folder`: `tf_plugin_cache_folder` and `tg_cache_folder`

- Type: string
- Optional
- Default: .terragrunt-cache

* `use_tf_plugin_cache_folder`

If set to `true`, the varibale TF_PLUGIN_CACHE_DIR will be enabled. Sometimes there can be problems when using these variable in Terragrunt

- Type: boolean
- Optional
- Default: false

* `tf_plugin_cache_folder`

Specify custom name for the tf_plugin_cache_folder folder which will be created inside `cache_folder`. Variable TF_PLUGIN_CACHE_DIR will be set to `${GITHUB_WORKSPACE}/${cache_folder}/${tf_plugin_cache_folder}`. Only applicable if `use_tf_plugin_cache_folder` is set to true

- Type: string
- Optional
- Default: tf-plugin-cache

* `tg_cache_folder`

Specify custom name for the tg_cache_folder folder which will be created inside `cache_folder`. Terragrunt commands will be executed with option `--terragrunt-download-dir ${GITHUB_WORKSPACE}/${cache_folder}/${tg_cache_folder}`

- Type: string
- Optional
- Default: tg-cache

## Environment Variables

* `GITHUB_TOKEN`
Expand Down
20 changes: 20 additions & 0 deletions gh-action-terragrunt-apply/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ inputs:
description: "What strategy to use when applying: parallel or sequential"
required: false
default: "parallel"
create_cache_folder_in_workspace:
description: "Create a cache folder in the workspace"
required: false
default: "false"
cache_folder:
description: "Cache folder name"
required: false
default: ".terragrunt-cache"
use_tf_plugin_cache_folder:
description: "If set to true, the variable TF_PLUGIN_CACHE_DIR will be enabled"
required: false
default: "false"
tf_plugin_cache_folder:
description: "Cache folder name for Terraform plugins. Only applicable if use_tf_plugin_cache_folder is set to true"
required: false
default: "tf-plugin-cache"
tg_cache_folder:
description: "Cache folder name for Terragrunt"
required: false
default: "tg-cache"

runs:
using: docker
Expand Down
40 changes: 40 additions & 0 deletions gh-action-terragrunt-plan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,46 @@ Don't use terragrunt_download setting in your terragrunt code and also don't cle
- Optional
- Default: false

* `create_cache_folder_in_workspace`

Set to true to create a cache folder in workspace. It can be reused in other steps, jobs and workflows. By default it created in /tmp folder inside docker container and not available outside.

- Type: boolean
- Optional
- Default: false

* `cache_folder`

Specify custom name for the cache folder which will be created in github workspace. Two additional folders will be created inside `cache_folder`: `tf_plugin_cache_folder` and `tg_cache_folder`

- Type: string
- Optional
- Default: .terragrunt-cache

* `use_tf_plugin_cache_folder`

If set to `true`, the varibale TF_PLUGIN_CACHE_DIR will be enabled. Sometimes there can be problems when using these variable in Terragrunt

- Type: boolean
- Optional
- Default: false

* `tf_plugin_cache_folder`

Specify custom name for the tf_plugin_cache_folder folder which will be created inside `cache_folder`. Variable TF_PLUGIN_CACHE_DIR will be set to `${GITHUB_WORKSPACE}/${cache_folder}/${tf_plugin_cache_folder}`. Only applicable if `use_tf_plugin_cache_folder` is set to true

- Type: string
- Optional
- Default: tf-plugin-cache

* `tg_cache_folder`

Specify custom name for the tg_cache_folder folder which will be created inside `cache_folder`. Terragrunt commands will be executed with option `--terragrunt-download-dir ${GITHUB_WORKSPACE}/${cache_folder}/${tg_cache_folder}`

- Type: string
- Optional
- Default: tg-cache

## Environment Variables

* `GITHUB_TOKEN`
Expand Down
20 changes: 20 additions & 0 deletions gh-action-terragrunt-plan/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ inputs:
description: Create a plan to destroy all resources
required: false
default: "false"
create_cache_folder_in_workspace:
description: "Create a cache folder in the workspace"
required: false
default: "false"
cache_folder:
description: "Cache folder name"
required: false
default: ".terragrunt-cache"
use_tf_plugin_cache_folder:
description: "If set to true, the variable TF_PLUGIN_CACHE_DIR will be enabled"
required: false
default: "false"
tf_plugin_cache_folder:
description: "Cache folder name for Terraform plugins. Only applicable if use_tf_plugin_cache_folder is set to true"
required: false
default: "tf-plugin-cache"
tg_cache_folder:
description: "Cache folder name for Terragrunt"
required: false
default: "tg-cache"

runs:
using: docker
Expand Down
24 changes: 22 additions & 2 deletions image/actions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function set_common_plan_args() {
fi
fi
export PLAN_ARGS
export PARALLEL_ARG
}

function plan() {
Expand Down Expand Up @@ -198,6 +199,10 @@ function fix_owners() {
chown -R --reference "$GITHUB_WORKSPACE" "$GITHUB_WORKSPACE/.gh-actions-terragrunt" || true
debug_cmd ls -la "$GITHUB_WORKSPACE/.gh-actions-terragrunt"
fi
if [[ -d "$GITHUB_WORKSPACE/$INPUT_CACHE_FOLDER" ]]; then
chown -R --reference "$GITHUB_WORKSPACE" "$GITHUB_WORKSPACE/$INPUT_CACHE_FOLDER" || true
debug_cmd ls -la "$GITHUB_WORKSPACE/$INPUT_CACHE_FOLDER"
fi

debug_cmd ls -la "$HOME"
if [[ -d "$HOME/.gh-actions-terragrunt" ]]; then
Expand All @@ -214,10 +219,25 @@ function fix_owners() {
fi
}

# Every file written to disk should use one of these directories
export TF_IN_AUTOMATION=true

if [[ "$INPUT_CREATE_CACHE_FOLDER_IN_WORKSPACE" == "true" ]]; then
CACHE_PATH=${GITHUB_WORKSPACE}
else
CACHE_PATH="/tmp"
fi

if [[ "$INPUT_USE_TF_PLUGIN_CACHE_FOLDER" == "true" ]]; then
TF_PLUGIN_CACHE_DIR="${CACHE_PATH}/${INPUT_CACHE_FOLDER}/${INPUT_TF_PLUGIN_CACHE_FOLDER}"
mkdir -p $TF_PLUGIN_CACHE_DIR
readonly TF_PLUGIN_CACHE_DIR
export TF_PLUGIN_CACHE_DIR
fi

STEP_TMP_DIR="/tmp"
PLAN_OUT_DIR="/tmp/plan"
TG_CACHE_DIR="/tmp/tg_cache_dir"
TG_CACHE_DIR="${CACHE_PATH}/${INPUT_CACHE_FOLDER}/${INPUT_TG_CACHE_FOLDER}"

JOB_TMP_DIR="$HOME/.gh-actions-terragrunt"
WORKSPACE_TMP_DIR=".gh-actions-terragrunt/$(random_string)"
mkdir -p $PLAN_OUT_DIR $TG_CACHE_DIR
Expand Down
6 changes: 5 additions & 1 deletion image/src/github_actions/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
class InitInputs(TypedDict):
"""Common input variables for actions the need to initialize terraform"""
INPUT_PATH: str
INPUT_WORKSPACE: str
INPUT_BACKEND_CONFIG: str
INPUT_BACKEND_CONFIG_FILE: str
INPUT_CREATE_CACHE_FOLDER_IN_WORKSPACE: str
INPUT_CACHE_FOLDER: str
INPUT_USE_TF_PLUGIN_CACHE_FOLDER: str
INPUT_TF_PLUGIN_CACHE_FOLDER: str
INPUT_TG_CACHE_FOLDER: str


class PlanInputs(InitInputs):
Expand Down
9 changes: 1 addition & 8 deletions image/src/github_pr_comment/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import hashlib
import os
import subprocess
import re
import sys
from pathlib import Path
Expand All @@ -15,14 +14,8 @@
from github_actions.env import GithubEnv
from github_actions.find_pr import find_pr, WorkflowException
from github_actions.inputs import PlanPrInputs
from github_pr_comment.backend_config import complete_config, partial_config
from github_pr_comment.backend_fingerprint import fingerprint
from github_pr_comment.cmp import plan_cmp, remove_warnings, remove_unchanged_attributes
from github_pr_comment.comment import find_comment, TerraformComment, update_comment, serialize, deserialize
from github_pr_comment.hash import comment_hash, plan_hash
from plan_renderer.variables import render_argument_list, Sensitive
from terraform.module import load_module, get_sensitive_variables
from terraform import hcl
from github_pr_comment.hash import plan_hash

Plan = NewType('Plan', str)
Status = NewType('Status', str)
Expand Down

0 comments on commit f774142

Please sign in to comment.