Merge pull request #118 from linkorb/playbook-idempotency-9352 #35
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Auto-run repo-ansible | |
on: | |
workflow_dispatch: | |
pull_request_target: | |
paths: | |
- 'repo.yaml' | |
push: | |
branches: | |
- main | |
- master | |
paths: | |
- 'repo.yaml' | |
permissions: | |
contents: write # allow git commits & push | |
pull-requests: write # allow comments on PR | |
env: | |
# XXX alternative to missing ternary syntax | |
IS_PULL_REQUEST: ${{ github.event_name == 'pull_request_target' && '1' || '0' }} | |
jobs: | |
run: | |
runs-on: ubuntu-latest | |
steps: | |
- if: ${{ env.IS_PULL_REQUEST == '0' }} | |
uses: actions/checkout@v4 | |
- if: ${{ env.IS_PULL_REQUEST == '1' }} | |
uses: actions/checkout@v4 | |
with: | |
ref: refs/pull/${{ github.event.pull_request.number }}/merge | |
- name: repo-ansible | |
run: | | |
docker pull ghcr.io/linkorb/repo-ansible:latest >/dev/null | |
docker run --rm -v "$PWD":/app ghcr.io/linkorb/repo-ansible:latest | tee /tmp/repo_ansible_output | |
export OUTPUT=$(cat /tmp/repo_ansible_output) | |
{ | |
echo 'REPO_ANSIBLE_OUTPUT<<EOF' | |
echo "$OUTPUT" | |
echo EOF | |
} >> "$GITHUB_ENV" | |
if ! echo "$OUTPUT" | grep "changed=0"; then | |
echo "REPOSITORY_CHANGED=1" >> "$GITHUB_ENV" | |
fi | |
- if: ${{ env.IS_PULL_REQUEST == '0' }} | |
name: commit changes | |
run: | | |
git config user.name "github-actions[bot]" | |
git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
git add . | |
if git commit -m "chore: automatic repo-ansible run #0000"; then | |
git push | |
fi | |
- if: ${{ env.IS_PULL_REQUEST == '1' && env.REPOSITORY_CHANGED == '1' }} | |
name: comment with changes | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const changes = process.env.REPO_ANSIBLE_OUTPUT | |
github.rest.issues.createComment({ | |
issue_number: context.issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: `Following repo-ansible changes will be applied when merged to main/master branch | |
\`\`\`shell | |
${changes} | |
\`\`\` | |
` | |
}) |