Skip to content

feat: Add GitHub Action to generate C4 diagrams #2

feat: Add GitHub Action to generate C4 diagrams

feat: Add GitHub Action to generate C4 diagrams #2

name: Generate C4 Diagrams
on:
push:
branches:
- main
- develop
paths:
- 'docs/c4-diagram.structurizr'
- '.github/workflows/generate-c4-diagrams.yml'
pull_request:
paths:
- 'docs/c4-diagram.structurizr'
- '.github/workflows/generate-c4-diagrams.yml'
workflow_dispatch:
jobs:
generate-diagrams:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
# For PRs from same repo, checkout the head branch to allow commits
# For PRs from forks, checkout base branch (no write access anyway)
ref: ${{ github.event.pull_request && github.event.pull_request.head.repo.full_name == github.repository && github.event.pull_request.head.ref || github.ref }}
- name: Generate C4 diagrams
run: |
docker run --rm -v ${{ github.workspace }}/docs:/docs extenda/structurizr-to-png \
--path c4-diagram.structurizr \
--output images
- name: Commit and push changes
# Only attempt to commit if it's a push event or a PR from the same repository
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository)
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add docs/images/
if ! git diff --staged --quiet; then
git commit -m "chore: update C4 diagrams [skip ci]"
git push
else
echo "No changes in generated diagrams"
fi
- name: Upload diagrams as artifacts
# Upload artifacts for PRs from forks (no write access) or for any PR for convenience
if: github.event_name == 'pull_request'
uses: actions/upload-artifact@v4
with:
name: c4-diagrams
path: docs/images/
retention-days: 7
- name: Comment on PR with generated diagrams
# Only comment on PRs from forks where we can't push commits
if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name != github.repository
uses: peter-evans/create-or-update-comment@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue-number: ${{ github.event.pull_request.number }}
body: |
## C4 Diagrams Generated
The C4 diagrams have been generated for this PR. Since this is from a fork, the diagrams are available as artifacts.
**[Download diagrams from workflow artifacts](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})**
Once this PR is merged, the diagrams will be automatically committed to the main branch.