Skip to content

Commit cf41cfa

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 cf41cfa

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

modules/terraform/tf-options.nix

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,22 @@
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+
if [[ -v CI && $CI == 'true' ]] && type gh &>/dev/null; then
302+
MESSAGE="$(echo -e "$(printf '%s' "''${MSG[@]}")")"
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+
else
313+
commitPrompt
314+
git -C "$WORKTREE" commit --no-verify -m "$(echo -e "$(printf '%s' "''${MSG[@]}")")" &>> "$WORKLOG"
315+
git -C "$WORKTREE" push -u "$REMOTE" "$TF_BRANCH" &>> "$WORKLOG"
316+
fi
304317
echo " ...done"
305318
echo
306319

0 commit comments

Comments
 (0)