Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching .terraform #83

Merged
merged 37 commits into from
Aug 19, 2024
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
8857288
Test caching
goruha Jul 25, 2024
5d20c5c
Test caching
goruha Jul 25, 2024
c5a72c2
Test caching
goruha Jul 25, 2024
a6e07aa
Test caching
goruha Jul 25, 2024
c328519
Test caching
goruha Jul 25, 2024
7ef1b3c
Test caching
goruha Jul 25, 2024
e7a17e7
Test caching
goruha Jul 25, 2024
1c2f758
Test .terraform caching
goruha Jul 25, 2024
784d3c4
Test .terraform caching
goruha Jul 25, 2024
421f626
Test .terraform caching
goruha Jul 25, 2024
b99b328
Update action.yml
goruha Jul 25, 2024
8dad3dc
Update action.yml
goruha Jul 26, 2024
a81f79c
Fix tfstate for gitops
goruha Jul 26, 2024
ac44bd9
Merge branch 'support-caching' of github.com:cloudposse/github-action…
goruha Jul 26, 2024
83b399c
Fix tfstate for gitops
goruha Jul 26, 2024
c4e3408
Fix tfstate for gitops
goruha Jul 26, 2024
bc5199e
Fix tfstate for gitops
goruha Jul 26, 2024
60b657f
Fix tfstate for gitops
goruha Jul 26, 2024
af4bbe6
Fix tfstate for gitops
goruha Jul 26, 2024
fc23a1c
Fix tfstate for gitops
goruha Jul 26, 2024
0cfbabc
Fix tfstate for gitops
goruha Jul 26, 2024
25f9b73
Fix tfstate for gitops
goruha Jul 26, 2024
fa8084a
Update action.yml
goruha Jul 26, 2024
beb278c
Update action.yml
goruha Jul 26, 2024
8f33954
Update action.yml
goruha Jul 26, 2024
2a91fa3
Update action.yml
goruha Jul 26, 2024
b3d7de6
Update integration-tests.yml
goruha Jul 26, 2024
1bd5f75
Update action.yml
goruha Jul 26, 2024
77e69f9
Update action.yml
goruha Jul 26, 2024
f9da565
Update action.yml
goruha Jul 26, 2024
a3a0a41
Fix tfstate for gitops
goruha Jul 26, 2024
4b8ae63
Update integration-tests.yml
goruha Jul 26, 2024
a7a08ff
Use cloudposse-github-actions/install-gh-releases
goruha Aug 16, 2024
b986e6e
Merge branch 'support-caching' of github.com:cloudposse/github-action…
goruha Aug 16, 2024
9ded8e6
Update action.yml
goruha Aug 16, 2024
5a2e1d9
Improve condiition
goruha Aug 19, 2024
d352589
Improve condiition
goruha Aug 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/[email protected]
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/[email protected]
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 }}
Loading