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(tf): use GitHub API to commit tf state in CI #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
21 changes: 18 additions & 3 deletions modules/terraform/tf-options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,24 @@
# Git commit encrypted state
echo " Committing encrypted state ..."
git -C "$WORKTREE" add "$WORKTREE/$ENC_STATE_PATH" &>> "$WORKLOG"
commitPrompt
git -C "$WORKTREE" commit --no-verify -m "$(echo -e "$(printf '%s' "''${MSG[@]}")")" &>> "$WORKLOG"
git -C "$WORKTREE" push -u "$REMOTE" "$TF_BRANCH" &>> "$WORKLOG"
MESSAGE="$(echo -e "$(printf '%s' "''${MSG[@]}")")"
if [[ -v CI && $CI == true ]] && type gh &>/dev/null; then
SHA=$(git rev-parse "$TF_BRANCH:$ENC_STATE_PATH")

base64 -w 0 "$WORKTREE/$ENC_STATE_PATH" \
| jq -R '{content: ., $message, $branch, $sha, encoding: "base64"}' \
--arg message "$MESSAGE" \
--arg sha "$SHA" \
--arg branch "$TF_BRANCH" \
| GH_TOKEN=$GITHUB_TOKEN gh api --method PUT "/repos/{owner}/{repo}/contents/$ENC_STATE_PATH" \
--input /dev/stdin &>> "$WORKLOG" \
&& git -C "$WORKTREE" restore --staged . &>> "$WORKLOG" \
&& git -C "$WORKTREE" checkout . &>> "$WORKLOG"
else
commitPrompt
git -C "$WORKTREE" commit --no-verify -m "$MESSAGE" &>> "$WORKLOG"
git -C "$WORKTREE" push -u "$REMOTE" "$TF_BRANCH" &>> "$WORKLOG"
fi
echo " ...done"
echo

Expand Down