Skip to content

Commit a504a2e

Browse files
committed
feat(tf): use GitHub API to commit tf-state in CI
If we are running a terraform apply from CI, the GH API will sign the commit for us and provide the correct identity.
1 parent 024db2b commit a504a2e

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

modules/terraform/tf-options.nix

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,24 @@
298298
# Git commit encrypted state
299299
echo " Committing encrypted state ..."
300300
git -C "$WORKTREE" add "$WORKTREE/$ENC_STATE_PATH" &>> "$WORKLOG"
301-
commitPrompt
302-
git -C "$WORKTREE" commit --no-verify -m "$(echo -e "$(printf '%s' "''${MSG[@]}")")" &>> "$WORKLOG"
303-
git -C "$WORKTREE" push -u "$REMOTE" "$TF_BRANCH" &>> "$WORKLOG"
301+
MESSAGE="$(echo -e "$(printf '%s' "''${MSG[@]}")")"
302+
if [[ -v CI && $CI == true ]] && type gh &>/dev/null; then
303+
SHA=$(git rev-parse "$TF_BRANCH:$ENC_STATE_PATH")
304+
305+
base64 -w 0 "$WORKTREE/$ENC_STATE_PATH" \
306+
| jq -R '{content: ., $message, $branch, $sha, encoding: "base64"}' \
307+
--arg message "$MESSAGE" \
308+
--arg sha "$SHA" \
309+
--arg branch "$TF_BRANCH" \
310+
| GH_TOKEN=$GITHUB_TOKEN gh api --method PUT "/repos/{owner}/{repo}/contents/$ENC_STATE_PATH" \
311+
--input /dev/stdin &>> "$WORKLOG" \
312+
&& git -C "$WORKTREE" restore --staged . &>> "$WORKLOG" \
313+
&& git -C "$WORKTREE" checkout . &>> "$WORKLOG"
314+
else
315+
commitPrompt
316+
git -C "$WORKTREE" commit --no-verify -m "$MESSAGE" &>> "$WORKLOG"
317+
git -C "$WORKTREE" push -u "$REMOTE" "$TF_BRANCH" &>> "$WORKLOG"
318+
fi
304319
echo " ...done"
305320
echo
306321

0 commit comments

Comments
 (0)