Skip to content

Commit

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

* Test caching

* Test caching

* Test caching

* Test caching

* Test caching

* Test caching

* Test .terraform caching

* Test .terraform caching

* Test .terraform caching

* Update action.yml

* Update action.yml

* Fix tfstate for gitops

* Fix tfstate for gitops

* Fix tfstate for gitops

* Fix tfstate for gitops

* Fix tfstate for gitops

* Fix tfstate for gitops

* Fix tfstate for gitops

* Fix tfstate for gitops

* Fix tfstate for gitops

* Update action.yml

* Update action.yml

* Update action.yml

* Update action.yml

* Update integration-tests.yml

* Update action.yml

* Update action.yml

* Update action.yml

* Fix tfstate for gitops

* Update integration-tests.yml

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

* Update action.yml

* Improve condiition

* Improve condiition
  • Loading branch information
goruha authored Aug 19, 2024
1 parent a40b1dd commit 85cbbac
Showing 1 changed file with 51 additions and 57 deletions.
108 changes: 51 additions & 57 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,78 +96,63 @@ 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: Filter Atmos Settings Value
uses: cloudposse/github-action-atmos-get-setting@v1
id: atmos-github-actions-enabled
with:
component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settings-path: settings.github.actions_enabled

- name: Find component path
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
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:
tag: v4.11.0
- name: Get atmos settings
uses: cloudposse/github-action-atmos-get-setting@v1
id: component-path
id: component
with:
component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settings-path: component_info.component_path

- name: Find atmos cli base path
if: ${{ steps.atmos-github-actions-enabled.outputs.value == '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: 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: Set atmos cli base path vars
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
if: ${{ fromJson(steps.component.outputs.settings).enabled }}
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: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
uses: jaxxstorm/action-install-gh-release@v1.11.0
with:
repo: suzuki-shunsuke/tfcmt
tag: v4.11.0

- name: Prepare Artifacts Directory
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
if: ${{ fromJson(steps.component.outputs.settings).enabled }}
shell: bash
run: |
mkdir -p metadata
- name: Define Job Variables
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
if: ${{ fromJson(steps.component.outputs.settings).enabled }}
id: vars
shell: bash
run: |
STACK_NAME=$(echo "${{ inputs.stack }}" | sed 's#/#_#g')
COMPONENT_PATH=${{ steps.component-path.outputs.value }}
COMPONENT_PATH=${{ 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="$( realpath ${COMPONENT_PATH})/$COMPONENT_SLUG-${{ inputs.sha }}.planfile"
LOCK_FILE="$( realpath ${COMPONENT_PATH})/.terraform.lock.hcl"
Expand All @@ -179,6 +164,7 @@ runs:
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 All @@ -187,16 +173,25 @@ runs:
echo "issue_file=${ISSUE_SUMMARY_FILE}" >> $GITHUB_OUTPUT
- name: Configure Plan AWS Credentials
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
if: ${{ fromJson(steps.component.outputs.settings).enabled }}
uses: aws-actions/configure-aws-credentials@v4.0.2
with:
aws-region: ${{ steps.config.outputs.aws-region }}
role-to-assume: ${{ steps.config.outputs.terraform-plan-role }}
role-session-name: "atmos-terraform-plan-gitops"
mask-aws-account-id: "no"

- 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: Atmos Terraform Plan
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
if: ${{ fromJson(steps.component.outputs.settings).enabled }}
id: atmos-plan
shell: bash
run: |
Expand Down Expand Up @@ -315,7 +310,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: ${{ steps.config.outputs.enable-infracost == 'true' && steps.atmos-plan.outputs.changes == 'true' }}
Expand Down Expand Up @@ -362,9 +357,8 @@ runs:
sed -i "s/%INFRACOST_SUMMARY%/${INFRA_COST_SUMMARY}/" ${{ steps.vars.outputs.step_summary_file }}
sed -i -e '/%INFRACOST_DIFF%/{r /tmp/infracost.txt' -e 'd}' ${{ steps.vars.outputs.step_summary_file }}
- name: Store Component Metadata to Artifacts
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' && inputs.drift-detection-mode-enabled == 'true' }}
if: ${{ fromJson(steps.component.outputs.settings).enabled && inputs.drift-detection-mode-enabled == 'true' }}
shell: bash
run: |
echo -n '{ "stack": "${{ inputs.stack }}", "component": "${{ inputs.component }}", "componentPath": "${{ steps.vars.outputs.component_path }}", "drifted": '"${{ steps.atmos-plan.outputs.changes }}"', "error": '"${{ steps.atmos-plan.outputs.error }}"' }' > "metadata/${{ steps.vars.outputs.component_slug }}.metadata.json"
Expand Down Expand Up @@ -408,7 +402,7 @@ runs:
echo "rand=$(openssl rand -hex 5)" >> "$GITHUB_OUTPUT"
- name: Upload Artifacts
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' && inputs.drift-detection-mode-enabled == 'true' }}
if: ${{ fromJson(steps.component.outputs.settings).enabled && inputs.drift-detection-mode-enabled == 'true' }}
uses: actions/upload-artifact@v4
with:
# The name of the artifact needs to be unique for every job run!
Expand All @@ -418,7 +412,7 @@ runs:
retention-days: ${{ inputs.metadata-retention-days }}

- name: Exit status
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
if: ${{ fromJson(steps.component.outputs.settings).enabled }}
shell: bash
run: |
exit ${{ steps.atmos-plan.outputs.result }}

0 comments on commit 85cbbac

Please sign in to comment.