I use dev.to as a headless CMS via the dev.to API. I run that in a nightly GitHub action that pulls the latest content, and if anything has changed, I merge those changes to the main branch. I also update my streaming page on my website with my latest videos from YouTube using the YouTube API.
This has been working fine, but it has some shortcomings:
Pushing straight to the main branch without a pull request (PR), there are no deploy previews on Netlify.
To be able to auto-merge a branch, you need to modify some settings in your repository. Navigate to, e.g. https://github.com/nickytonline/iamdeveloper.com/settings, scroll to the bottom of the general settings and ensure that Allow auto-merge is checked.
Optionally, what I did was auto-delete branches. If you want to do the same, ensure that Automatically delete head branches is checked.
The scripts above generate changes if any. If there are changes, they're committed and merged into the main branch.
git config user.name "GitHub Actions Bot" git config user.email "<>" git pull origin main gitadd. if[[ -n "$(git status --porcelain)"]];then git commit -m "chore (automated): update blog posts" git push origin main fi
To use a PR instead, I went with the following, in this case, for updating blog posts.
PR_TITLE="chore (automated): update blog posts" BRANCH_NAME="chore_automated_update_blog_posts_$(date +%s)"
git branch $BRANCH_NAME git switch $BRANCH_NAME
# There are potentially multiple files if the blog post has images. gitadd.
# See if we have any changes. We should. if[[ -n "$(git status --porcelain)"]];then echo"Creating PR \"$PR_TITLE\" for branch $BRANCH_NAME" git commit -m "$PR_TITLE" git push origin $BRANCH_NAME gh pr create --title "$PR_TITLE" --body "This is an automated PR to update blog posts" gh pr merge --auto --delete-branch --squash "$BRANCH_NAME" else # Shouldn't end up here, but log that there was nothing to sync echo"Looks like there was nothing to update." fi
So like before, the GitHub action has already gotten the latest code from the main branch, and we've run our Node.js scripts to get the latest blog posts.
Instead of committing straight to the main branch, we now create a PR via the GitHub CLI.
gh pr create --title "$PR_TITLE" --body "This is an automated PR to update blog posts"
Once the pull request is created, the following GitHub CLI command sets up the PR to auto-merge if all the checks pass.