Skip to content

Commit a18817f

Browse files
authored
fix(docs): Sign commits for documentation changes (#257)
1 parent 4ae2f67 commit a18817f

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

.github/actions/action.yml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,35 @@ runs:
9999
else
100100
echo "tf_docs=false" >> "$GITHUB_OUTPUT"
101101
fi
102-
- name: Render terraform docs inside the README.md and push changes back to PR branch
102+
- name: Render terraform docs inside the README.md
103103
if: ${{ steps.tf_docs.outputs.tf_docs == 'true' }}
104+
id: terraform_docs
104105
uses: terraform-docs/gh-actions@v1.3.0
105106
with:
106107
working-dir: ${{ inputs.package-name }}
107108
output-file: README.md
108-
git-push: "true"
109109
config-file: .terraform-docs.yml
110+
- name: If documentation is updated, push to PR branch with a signed commit
111+
if: ${{ steps.terraform_docs.outputs.num_changed != '0' }}
112+
env:
113+
DESTINATION_BRANCH: ${{ github.event.pull_request.head.ref }}
114+
FILE_TO_COMMIT: ${{ inputs.package-name }}/README.md
115+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
116+
shell: bash
117+
run: |
118+
### Signed commit workaround - if we do a normal `git commit` here, it will be unsigned
119+
# GHA doesn't have a good native way to sign commits (https://github.com/actions/runner/issues/667)
120+
# Commits submitted via the API do get signed, so do that instead - adapted from https://gist.github.com/swinton/03e84635b45c78353b1f71e41007fc7c
121+
export TODAY=$( date -u '+%Y-%m-%d' )
122+
export MESSAGE="chore(docs): ${FILE_TO_COMMIT}"
123+
export SHA=$( git rev-parse $DESTINATION_BRANCH:$FILE_TO_COMMIT )
124+
export CONTENT=$( base64 -i $FILE_TO_COMMIT )
125+
gh api --method PUT /repos/:owner/:repo/contents/$FILE_TO_COMMIT \
126+
--field message="$MESSAGE" \
127+
--field content="$CONTENT" \
128+
--field encoding="base64" \
129+
--field branch="$DESTINATION_BRANCH" \
130+
--field sha="$SHA"
110131
- name: Sparse checkout unmodified changelogs from main
111132
uses: actions/checkout@v4
112133
with:

0 commit comments

Comments
 (0)