From d8de2c65ebd41f2d2955866e19a08ca118c30ecb Mon Sep 17 00:00:00 2001 From: Alina Freydina Date: Fri, 5 Jan 2024 16:36:44 +0300 Subject: [PATCH] feat: added cache folders and variables --- gh-action-terragrunt-apply/README.md | 40 +++++++++++++++++++++++++ gh-action-terragrunt-apply/action.yaml | 20 +++++++++++++ gh-action-terragrunt-plan/README.md | 40 +++++++++++++++++++++++++ gh-action-terragrunt-plan/action.yaml | 20 +++++++++++++ image/actions.sh | 24 +++++++++++++-- image/src/github_actions/inputs.py | 6 +++- image/src/github_pr_comment/__main__.py | 9 +----- 7 files changed, 148 insertions(+), 11 deletions(-) diff --git a/gh-action-terragrunt-apply/README.md b/gh-action-terragrunt-apply/README.md index 6d8bc83..99d4585 100644 --- a/gh-action-terragrunt-apply/README.md +++ b/gh-action-terragrunt-apply/README.md @@ -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` diff --git a/gh-action-terragrunt-apply/action.yaml b/gh-action-terragrunt-apply/action.yaml index e708046..ac7dad6 100644 --- a/gh-action-terragrunt-apply/action.yaml +++ b/gh-action-terragrunt-apply/action.yaml @@ -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 diff --git a/gh-action-terragrunt-plan/README.md b/gh-action-terragrunt-plan/README.md index 3603908..6de653c 100644 --- a/gh-action-terragrunt-plan/README.md +++ b/gh-action-terragrunt-plan/README.md @@ -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` diff --git a/gh-action-terragrunt-plan/action.yaml b/gh-action-terragrunt-plan/action.yaml index c1e09ca..f808b46 100644 --- a/gh-action-terragrunt-plan/action.yaml +++ b/gh-action-terragrunt-plan/action.yaml @@ -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 diff --git a/image/actions.sh b/image/actions.sh index a665976..f9fc511 100755 --- a/image/actions.sh +++ b/image/actions.sh @@ -75,6 +75,7 @@ function set_common_plan_args() { fi fi export PLAN_ARGS + export PARALLEL_ARG } function plan() { @@ -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 @@ -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 diff --git a/image/src/github_actions/inputs.py b/image/src/github_actions/inputs.py index d3ca867..a56fd92 100644 --- a/image/src/github_actions/inputs.py +++ b/image/src/github_actions/inputs.py @@ -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): diff --git a/image/src/github_pr_comment/__main__.py b/image/src/github_pr_comment/__main__.py index 5693c17..1d68f03 100644 --- a/image/src/github_pr_comment/__main__.py +++ b/image/src/github_pr_comment/__main__.py @@ -1,6 +1,5 @@ import hashlib import os -import subprocess import re import sys from pathlib import Path @@ -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)