Update check_and_update_json_date.yml

This commit is contained in:
CanbiZ 2025-01-15 11:12:18 +01:00 committed by GitHub
parent d186557488
commit 12a61a1d71
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,33 +1,59 @@
name: Update Date Created in PR
name: Update date_created in JSON files
on:
# Dieser Trigger wird für das Öffnen von PRs sowie für das Aktualisieren von offenen PRs verwendet
pull_request:
paths:
- '*.json'
types: [opened, synchronize]
workflow_dispatch:
schedule:
# Dieser Trigger wird 4x am Tag ausgelöst, um sicherzustellen, dass das Datum aktualisiert wird
- cron: "0 0,6,12,18 * * *" # Führt alle 6 Stunden aus
jobs:
update-date:
runs-on: ubuntu-latest
steps:
- name: Checkout PR branch
- name: Checkout repository
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Install yq
run: |
curl -sSL https://github.com/mikefarah/yq/releases/download/v4.18.1/yq_linux_amd64 -o /usr/local/bin/yq
chmod +x /usr/local/bin/yq
sudo apt-get update
sudo apt-get install -y yq
- name: Update date_created in JSON
run: |
TODAY=$(date -u +%Y-%m-%d)
yq e '.date_created = strftime("%Y-%m-%d")' -i your_file.json
- name: Set the current date
id: set_date
run: echo "TODAY=$(date -u +%Y-%m-%d)" >> $GITHUB_ENV
- name: Commit changes if necessary
- name: Check for changes in PR
run: |
git status
git diff --quiet || (git commit -m "Update date_created to $TODAY" && git push origin ${{ github.head_ref }})
# Hole den PR-Branch
PR_BRANCH="refs/pull/${{ github.event.pull_request.number }}/merge"
git fetch origin $PR_BRANCH
# Liste alle JSON-Dateien im PR auf, die geändert wurden
CHANGED_JSON_FILES=$(git diff --name-only origin/main...$PR_BRANCH | grep '.json')
if [ -z "$CHANGED_JSON_FILES" ]; then
echo "No JSON files changed in this PR."
exit 0
fi
# Gehe alle geänderten JSON-Dateien durch und aktualisiere das Datum
for file in $CHANGED_JSON_FILES; do
echo "Updating date_created in $file"
# Setze das aktuelle Datum
yq eval ".date_created = \"${{ env.TODAY }}\"" -i "$file"
git add "$file"
done
- name: Commit and push changes
run: |
# Prüfe, ob es Änderungen gibt und committe sie
git config user.name "json-updater-bot"
git config user.email "github-actions[bot]@users.noreply.github.com"
git commit -m "Update date_created to ${{ env.TODAY }}" || echo "No changes to commit"
# Push zurück in den PR-Branch
git push origin $PR_BRANCH