Skip to content

Commit

Permalink
Update state file in same job
Browse files Browse the repository at this point in the history
  • Loading branch information
carlssonk committed Sep 24, 2024
1 parent 7eb65d2 commit 73542e7
Showing 1 changed file with 19 additions and 39 deletions.
58 changes: 19 additions & 39 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,15 @@ jobs:
environment: ${{ needs.setup.outputs.env }}
permissions:
id-token: write
contents: read
contents: write
pull-requests: write
strategy:
matrix: ${{ fromJson(needs.setup.outputs.matrix) }}
fail-fast: ${{ fromJSON(needs.setup.outputs.fail-fast) }}
max-parallel: 1
env:
TF_ROOT_DIRECTORY: ./environments/${{ (needs.setup.outputs.env == 'staging' || needs.setup.outputs.env == 'prod') && 'staging-and-prod' || 'dev' }}
TFVARS_FILE_NAME: ${{ needs.setup.outputs.env == 'dev' && 'terraform' || needs.setup.outputs.env }}
outputs:
terraform_state_json: ${{ steps.set-tf-state.outputs.terraform_state_json }}
steps:
- name: Set TF_VAR_workflow_step
run: echo "TF_VAR_workflow_step=${{ matrix.step }}" >> $GITHUB_ENV
Expand Down Expand Up @@ -154,13 +153,26 @@ jobs:
fi
working-directory: ${{ env.TF_ROOT_DIRECTORY }}

- name: Terraform Show
- name: Update Terraform State File
id: set-tf-state
if: matrix.step == 'resources' && github.event_name != 'workflow_dispatch'
env:
branch_name: terraform-state
run: |
git config user.name github-actions
git config user.email [email protected]
if git ls-remote --exit-code --heads origin ${{ env.branch_name }} >/dev/null 2>&1; then
git fetch --all
git checkout ${{ env.branch_name }}
else
git checkout --orphan ${{ env.branch_name }}
git reset --hard
fi
terraform show -json | sed -n "2p" > terraform_show.json
echo "$(cat terraform_show.json)"
echo "terraform_state_json='$(cat terraform_show.json)'" >> $GITHUB_OUTPUT
git diff --staged --quiet || (git commit -m "Updated by github-actions" && git push -u origin ${{ env.branch_name }})
working-directory: ${{ env.TF_ROOT_DIRECTORY }}

- name: Delay for 15 seconds
Expand Down Expand Up @@ -207,36 +219,4 @@ jobs:
body: |
Automated release for tag ${{ steps.create_tag.outputs.TAG_NAME }}
Commit: ${{ github.sha }}
create-terraform-state-json:
needs: [setup, terraform-operation]
if: needs.setup.outputs.env == 'prod'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Git
run: |
git config user.name github-actions
git config user.email [email protected]
- name: Checkout and update branch
env:
branch_name: terraform-state
run: |
if git ls-remote --exit-code --heads origin ${{ env.branch_name }} >/dev/null 2>&1; then
git fetch --all
git checkout ${{ env.branch_name }}
else
git checkout --orphan ${{ env.branch_name }}
git reset --hard
fi
echo "${{ needs.terraform-operation.outputs.terraform_state_json }}" > terraform_state.json
git diff --staged --quiet || (git commit -m "Updated by github-actions" && git push -u origin ${{ env.branch_name }})
Commit: ${{ github.sha }}

0 comments on commit 73542e7

Please sign in to comment.