Generate D2 Diagrams #8
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: Generate D2 Diagrams | |
on: | |
workflow_dispatch: | |
jobs: | |
generate-diagram: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
pull-requests: write | |
env: | |
CI_COMMIT_MESSAGE: Update generated diagrams with D2 | |
CI_COMMIT_AUTHOR: D2 Diagram Creator | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Git Global Config | |
run: | | |
git config --global user.name "${{ env.CI_COMMIT_AUTHOR }}" | |
git config --global user.email "actions@github.com" | |
- name: Download and install D2 precompiled binary | |
run: | | |
# Download the latest D2 binary for Linux directly | |
curl -L https://github.com/terrastruct/d2/releases/download/v0.6.7/d2-linux-amd64 -o d2 | |
# Make it executable | |
chmod +x d2 | |
# Move the binary to /usr/local/bin to make it available in the PATH | |
sudo mv d2 /usr/local/bin/d2 | |
- name: Create output directory | |
run: mkdir -p output/d2-diagrams/ | |
- name: Generate Diagrams with D2 | |
run: | | |
# Get current timestamp | |
timestamp=$(date +"%Y-%m-%d-%H-%M-%S") | |
# Find all .d2 files in the repo and generate corresponding .svg files | |
mkdir -p output/d2-diagrams/ | |
for file in $(find . -name '*.d2'); do | |
# Extract the base file name without the extension | |
base_name=$(basename "$file" .d2) | |
# Generate the diagram in SVG format and store it in the output directory | |
/usr/local/bin/d2 "$file" "output/d2-diagrams/${base_name}-$timestamp.svg" | |
done | |
- name: List generated files (for debugging) | |
run: ls -al output/d2-diagrams/ | |
- name: Add output files & Status | |
run: | | |
git add output/ | |
git status | |
- name: Commit and Push generated diagrams to repository | |
run: | | |
git commit --verbose -a -m "${{ env.CI_COMMIT_MESSAGE }}" || echo "No changes to commit" | |
git push origin main --force --verbose | |
- name: Upload generated diagrams as artifact | |
uses: actions/upload-artifact@v3 | |
with: | |
name: generated-diagrams | |
path: output/d2-diagrams/ |