Skip to content

Commit

Permalink
Caching .terraform (#56)
Browse files Browse the repository at this point in the history
* Caching .terraform

* Update action.yml

* Use cloudposse-github-actions/install-gh-releases

* Consolidate settings action
  • Loading branch information
goruha authored Aug 19, 2024
1 parent 8965ff3 commit ca53b7a
Showing 1 changed file with 49 additions and 61 deletions.
110 changes: 49 additions & 61 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ outputs:
runs:
using: "composite"
steps:
- name: Install Node
uses: actions/setup-node@v4
with:
node-version: 20

- name: Checkout
uses: actions/checkout@v4

Expand Down Expand Up @@ -87,34 +92,38 @@ runs:
terraform_version: ${{ steps.config.outputs.terraform-version }}
terraform_wrapper: false

- name: Install OpenTofu
if: ${{ steps.config.outputs.opentofu-version != '' && steps.config.outputs.opentofu-version != 'null' }}
uses: opentofu/setup-opentofu@v1
- name: Install Dependencies
uses: cloudposse-github-actions/install-gh-releases@v1
with:
tofu_version: ${{ steps.config.outputs.opentofu-version }}
tofu_wrapper: false

- name: Install Node
uses: actions/setup-node@v4
with:
node-version: 20

- name: Filter Atmos Settings Value
cache: true
config: |-
opentofu/opentofu:
tag: ${{ startsWith(steps.config.outputs.opentofu-version, 'v') && steps.config.outputs.opentofu-version || format('v{0}', steps.config.outputs.opentofu-version) }}
skip: ${{ steps.config.outputs.opentofu-version == '' || steps.config.outputs.opentofu-version == 'null' }}
suzuki-shunsuke/tfcmt: v4.11.0
terraform-docs/terraform-docs: v0.18.0
- name: Get atmos settings
uses: cloudposse/github-action-atmos-get-setting@v1
id: atmos-github-actions-enabled
id: component
with:
component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settings-path: settings.github.actions_enabled

- name: Find command in config
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
uses: cloudposse/github-action-atmos-get-setting@v1
id: command
with:
component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settings-path: command
settings: |
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: settings.github.actions_enabled
outputPath: enabled
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: component_info.component_path
outputPath: component-path
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: atmos_cli_config.base_path
outputPath: base-path
- component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settingsPath: command
outputPath: command
- name: Define Job Control State Variables
shell: bash
Expand All @@ -126,68 +135,38 @@ runs:
- name: Check If GitHub Actions is Enabled For Component
shell: bash
run: |
if [[ "${{ steps.atmos-github-actions-enabled.outputs.value }}" == "true" ]]; then
if [[ "${{ fromJson(steps.component.outputs.settings).enabled }}" == "true" ]]; then
echo "ACTIONS_ENABLED=true" >> $GITHUB_ENV
else
echo "ACTIONS_ENABLED=false" >> $GITHUB_ENV
fi
- name: Find component path
if: env.ACTIONS_ENABLED == 'true'
uses: cloudposse/github-action-atmos-get-setting@v1
id: component-path
with:
component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settings-path: component_info.component_path

- name: Find atmos cli base path
if: env.ACTIONS_ENABLED == 'true'
uses: cloudposse/github-action-atmos-get-setting@v1
id: base-path
with:
component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settings-path: atmos_cli_config.base_path

- name: Set atmos cli base path vars
if: env.ACTIONS_ENABLED == 'true'
shell: bash
run: |-
# Set ATMOS_BASE_PATH allow `cloudposse/utils` provider to read atmos config from the correct path
ATMOS_BASE_PATH="${{ steps.base-path.outputs.value }}"
ATMOS_BASE_PATH="${{ fromJson(steps.component.outputs.settings).base-path }}"
echo "ATMOS_BASE_PATH=$(realpath ${ATMOS_BASE_PATH:-./})" >> $GITHUB_ENV
- name: Install tfcmt
if: env.ACTIONS_ENABLED == 'true'
uses: jaxxstorm/[email protected]
with:
repo: suzuki-shunsuke/tfcmt
tag: v4.11.0

- name: Install terraform-docs
if: env.ACTIONS_ENABLED == 'true'
uses: jaxxstorm/[email protected]
with:
repo: terraform-docs/terraform-docs
tag: v0.18.0

- name: Define Job Variables
if: env.ACTIONS_ENABLED == 'true'
id: vars
shell: bash
run: |
STACK_NAME=$(echo "${{ inputs.stack }}" | sed 's#/#_#g')
COMPONENT_PATH=$( realpath ${{ steps.component-path.outputs.value }})
COMPONENT_PATH=$( realpath ${{ fromJson(steps.component.outputs.settings).component-path }})
COMPONENT_NAME=$(echo "${{ inputs.component }}" | sed 's#/#_#g')
COMPONENT_SLUG="$STACK_NAME-$COMPONENT_NAME"
COMPONENT_CACHE_KEY=$(basename "${{ fromJson(steps.component.outputs.settings).component-path }}")
PLAN_FILE="${COMPONENT_PATH}/$COMPONENT_SLUG-${{ inputs.sha }}.planfile"
LOCK_FILE="${COMPONENT_PATH}/.terraform.lock.hcl"
echo "stack_name=$STACK_NAME" >> $GITHUB_OUTPUT
echo "component_name=$COMPONENT_NAME" >> $GITHUB_OUTPUT
echo "component_slug=$COMPONENT_SLUG" >> $GITHUB_OUTPUT
echo "component_path=${COMPONENT_PATH}" >> $GITHUB_OUTPUT
echo "cache-key=${COMPONENT_CACHE_KEY}" >> $GITHUB_OUTPUT
echo "plan_file=$PLAN_FILE" >> $GITHUB_OUTPUT
echo "lock_file=$LOCK_FILE" >> $GITHUB_OUTPUT
Expand Down Expand Up @@ -257,7 +236,7 @@ runs:
shell: bash
working-directory: ${{ steps.vars.outputs.component_path }}
run: |
${{ steps.command.outputs.value }} show -json "${{ steps.vars.outputs.plan_file }}" > "${{ steps.vars.outputs.plan_file }}.json"
${{ fromJson(steps.component.outputs.settings).command }} show -json "${{ steps.vars.outputs.plan_file }}" > "${{ steps.vars.outputs.plan_file }}.json"
- name: Generate Infracost Diff
if: env.INFRACOST_ENABLED == 'true'
Expand Down Expand Up @@ -298,6 +277,15 @@ runs:
echo "infracost_details_diff_breakdown=$INFRACOST_DETAILS_DIFF_BREAKDOWN" >> "$GITHUB_OUTPUT"
echo "infracost_diff_total_monthly_cost=$INFRACOST_DIFF_TOTAL_MONTHLY_COST" >> "$GITHUB_OUTPUT"
- name: Cache .terraform
id: cache
uses: actions/cache@v4
if: ${{ fromJson(steps.component.outputs.settings).enabled }}
with:
path: |
${{ steps.vars.outputs.component_path }}/.terraform
key: ${{ steps.vars.outputs.cache-key }}

- name: Terraform Apply
if: env.ACTIONS_ENABLED == 'true'
id: apply
Expand Down

0 comments on commit ca53b7a

Please sign in to comment.