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

feat: cache installs #53

Closed
wants to merge 14 commits into from
Closed
110 changes: 75 additions & 35 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,78 @@ runs:
patterns: |
- ${{ inputs.atmos-gitops-config-path }}

- name: Set atmos cli config path vars
- 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: Cache Dependencies
id: cache-dependencies
uses: actions/cache/restore@v3
env:
cache-name: atmos-plan
with:
path: |
/runner/_work/_temp/*/terraform
/runner/_work/_actions/cloudposse/github-action-setup-atmos/atmos/atmos
/runner/_work/_tool/suzuki-shunsuke/tfcmt/v4.6.0/linux-x64/tfcmt
key: atmos-plan-${{ hashFiles( inputs.atmos-gitops-config-path ) }}

- name: Update PATH if cache-hit for each install
if: ${{ steps.cache-dependencies.outputs.cache-hit == 'true' }}
shell: bash
run: |-
echo "ATMOS_CLI_CONFIG_PATH=$(realpath ${{ steps.config.outputs.atmos-config-path }})" >> $GITHUB_ENV
run: |
for i in /runner/_work/_temp/*/terraform; do
echo $(dirname $i) >> $GITHUB_PATH
cp $i /usr/local/bin/
done
echo "/runner/_work/_actions/cloudposse/github-action-setup-atmos/atmos" >> $GITHUB_PATH
cp /runner/_work/_actions/cloudposse/github-action-setup-atmos/atmos/atmos /usr/local/bin/
echo "/runner/_work/_tool/suzuki-shunsuke/tfcmt/v4.6.0/linux-x64" >> $GITHUB_PATH
cp /runner/_work/_tool/suzuki-shunsuke/tfcmt/v4.6.0/linux-x64/tfcmt /usr/local/bin/

- name: Install Terraform
if: ${{ steps.cache-dependencies.outputs.cache-hit != 'true' }}
uses: hashicorp/setup-terraform@v2
with:
terraform_version: ${{ steps.config.outputs.terraform-version }}
terraform_wrapper: false

- name: Install Atmos
if: ${{ steps.cache-dependencies.outputs.cache-hit != 'true' }}
uses: cloudposse/github-action-setup-atmos@v1
with:
atmos-version: ${{ steps.config.outputs.atmos-version }}
token: ${{ inputs.token }}
install-wrapper: false

- name: Filter Atmos Settings Value
uses: cloudposse/github-action-atmos-get-setting@v1
id: atmos-github-actions-enabled
- name: Install tfcmt
if: ${{ steps.cache-dependencies.outputs.cache-hit != 'true' }}
uses: jaxxstorm/[email protected]
with:
component: ${{ inputs.component }}
stack: ${{ inputs.stack }}
settings-path: settings.github.actions_enabled
repo: suzuki-shunsuke/tfcmt
tag: v4.6.0

- name: Cache Dependencies
id: save-dependencies
if: ${{ steps.cache-dependencies.outputs.cache-hit != 'true' }}
uses: actions/cache/save@v3
env:
cache-name: atmos-plan
with:
path: |
/runner/_work/_temp/*/terraform
/runner/_work/_actions/cloudposse/github-action-setup-atmos/atmos/atmos
/runner/_work/_tool/suzuki-shunsuke/tfcmt/v4.6.0/linux-x64/tfcmt
key: atmos-plan-${{ hashFiles( inputs.atmos-gitops-config-path ) }}

- name: Set atmos cli config path vars
shell: bash
run: |-
echo "ATMOS_CLI_CONFIG_PATH=$(realpath ${{ steps.config.outputs.atmos-config-path }})" >> $GITHUB_ENV

- name: Find component path
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
Expand All @@ -115,22 +162,15 @@ runs:
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
shell: bash
run: |-
# Set ATMOS_BASE_PATH allow `cloudposse/utils` provider to read atmos config from the correct path
# Set ATMOS_BASE_PATH allow `cloudposse/utils` provider to read atmos config from the correct path
ATMOS_BASE_PATH="${{ steps.base-path.outputs.value }}"
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.6.0

- name: Prepare Artifacts Directory
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
shell: bash
run: |
mkdir -p metadata
mkdir -p metadata

- name: Define Job Variables
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
Expand All @@ -147,7 +187,7 @@ runs:
ISSUE_SUMMARY_FILE="$GITHUB_WORKSPACE/metadata/issue-description-${COMPONENT_SLUG}.md"
STEP_SUMMARY_FILE="$GITHUB_WORKSPACE/metadata/step-summary-${COMPONENT_SLUG}.md"
SUMMARY_FILE="$GITHUB_WORKSPACE/metadata/summary-${COMPONENT_SLUG}.md"

echo "stack_name=${STACK_NAME}" >> $GITHUB_OUTPUT
echo "component_name=${COMPONENT_NAME}" >> $GITHUB_OUTPUT
echo "component_slug=${COMPONENT_SLUG}" >> $GITHUB_OUTPUT
Expand All @@ -174,7 +214,7 @@ runs:
shell: bash
run: |
set +e

TERRAFORM_OUTPUT=$( \
tfcmt \
--config ${{ github.action_path }}/config/summary.yaml \
Expand All @@ -193,7 +233,7 @@ runs:
--output ${{ steps.vars.outputs.summary_file }} \
--log-level $([[ "${{ inputs.debug }}" == "true" ]] && echo "DEBUG" || echo "INFO") \
plan -- \
atmos terraform plan ${{ inputs.component }} \
$(which atmos) terraform plan ${{ inputs.component }} \
--stack ${{ inputs.stack }} \
-out="${{ steps.vars.outputs.plan_file }}" \
-lock=false \
Expand All @@ -203,7 +243,7 @@ runs:
TERRAFORM_RESULT=$?

set -e

if [[ "${{ inputs.drift-detection-mode-enabled }}" == "true" ]]; then
# Split summary to 2 files - issue and step summary files
# Remove \0 at the end of the grep output
Expand All @@ -213,9 +253,9 @@ runs:
else
mv ${{ steps.vars.outputs.summary_file }} ${{ steps.vars.outputs.step_summary_file }}
fi

echo -ne "${TERRAFORM_OUTPUT}"

HAS_CHANGES=false
HAS_NO_CHANGES=false
HAS_ERROR=false
Expand Down Expand Up @@ -285,26 +325,26 @@ runs:
terraform show -json "$PLAN_FILE_STORED" > "$PLAN_FILE_STORED.json"

TERRAFORM_RESULT=$?

set -e

if [[ "${TERRAFORM_RESULT}" == "0" ]]; then
# sort and remove timestamp
jq 'if has("relevant_attributes") then .relevant_attributes |= sort_by(.resource, .attribute) else . end' "$PLAN_FILE.json" | jq 'del(.timestamp)' > current.json
jq 'if has("relevant_attributes") then .relevant_attributes |= sort_by(.resource, .attribute) else . end' "$PLAN_FILE_STORED.json" | jq 'del(.timestamp)' > stored.json

# calculate checksums of stored and current plans
MD5_CURRENT=$(md5sum current.json | awk '{ print $1 }')
MD5_STORED=$(md5sum stored.json | awk '{ print $1 }')

if [ "$MD5_CURRENT" == "$MD5_STORED" ]; then
echo "Current plan is equal to stored plan"
else
echo "Current plan is different from stored plan"
NEW_PLAN_FOUND=true
fi
else
# If terraform show failed that means old plan is wrong
# If terraform show failed that means old plan is wrong
NEW_PLAN_FOUND=true
fi
else
Expand Down Expand Up @@ -359,7 +399,7 @@ runs:
--format=json \
--project-name "${{ inputs.stack }}-${{ inputs.component }}" \
--out-file=/tmp/infracost.json

echo "finished=true" >> $GITHUB_OUTPUT

- name: Debug Infracost
Expand All @@ -377,7 +417,7 @@ runs:
run: |
INFRACOST_DIFF_TOTAL_MONTHLY_COST=$(cat /tmp/infracost.json | jq --raw-output .diffTotalMonthlyCost)
INFRACOST_DETAILS_DIFF_BREAKDOWN="$(cat /tmp/infracost.txt)"

if [[ "${INFRACOST_DIFF_TOTAL_MONTHLY_COST}" == "0" ]]; then
INFRA_COST_SUMMARY="Infracost Estimate: monthly cost will not change"
elif [[ "${INFRACOST_DIFF_TOTAL_MONTHLY_COST:0:1}" == "-" ]]; then
Expand All @@ -388,7 +428,7 @@ 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' }}
Expand All @@ -406,11 +446,11 @@ runs:
else
STEP_SUMMARY_FILE="${{ steps.vars.outputs.step_summary_file }}"
fi

if [[ "${{ steps.atmos-plan.outputs.no-changes }}" == "true" ]]; then
rm -f ${STEP_SUMMARY_FILE}
fi

if [ -f ${STEP_SUMMARY_FILE} ]; then
echo "${STEP_SUMMARY_FILE} found"

Expand All @@ -421,7 +461,7 @@ runs:
echo "Drift detection mode disabled"
cat $STEP_SUMMARY_FILE >> $GITHUB_STEP_SUMMARY
fi
else
else
echo "${STEP_SUMMARY_FILE} not found"
echo "result=\"\"" >> $GITHUB_OUTPUT
fi
Expand All @@ -438,4 +478,4 @@ runs:
if: ${{ steps.atmos-github-actions-enabled.outputs.value == 'true' }}
shell: bash
run: |
exit ${{ steps.atmos-plan.outputs.result }}
exit ${{ steps.atmos-plan.outputs.result }}
Loading