Skip to content

create-pr-if-outdated #704

create-pr-if-outdated

create-pr-if-outdated #704

name: create-pr-if-outdated
on:
schedule:
- cron: '0 0 * * *'
workflow_dispatch:
env:
BRANCH_NAME: update-date-in-readme
BASE_BRANCH_NAME: main
jobs:
create-pr-if-outdated:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
with:
ref: ${{ env.BASE_BRANCH_NAME }}
- name: update date in readme
run: |
git_last_updated_date=$(git log -1 --format="%at" -- . ":(exclude)README.md" | xargs -I{} date -d @{} +%Y-%m-%d)
echo "git_last_updated_date: $git_last_updated_date"
sed -i "s/.*Last Updated: \(.*\)/- Last Updated: $git_last_updated_date/" README.md
- name: check if need to commit
id: need_commit
run: |
if [[ -n "$(git status --porcelain)" ]]; then
echo "need_commit=true" >> "$GITHUB_OUTPUT"
else
echo "need_commit=false" >> "$GITHUB_OUTPUT"
fi
- name: commit and push
if: steps.need_commit.outputs.need_commit == 'true'
run: |
git config user.name github-actions
git config user.email [email protected]
git checkout -b ${{ env.BRANCH_NAME }}
git add .
git commit -m 'update date'
git push -u origin HEAD -f
- name: check if pr exists
id: check_pr
if: steps.need_commit.outputs.need_commit == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # used by gh
run: |
echo "count=$(gh pr list -S head:${{ env.BRANCH_NAME }} -B ${{ env.BASE_BRANCH_NAME }} | wc -l)" >> "$GITHUB_OUTPUT"
- name: create pr
if: ${{ steps.need_commit.outputs.need_commit == 'true' && steps.check_pr.outputs.count == 0 }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} # used by gh
run: |
gh pr create -B "${{ env.BASE_BRANCH_NAME }}" -t "Update last updated date in README" -b ""