Skip to content

Instantly share code, notes, and snippets.

@micedreams
Last active January 22, 2024 05:32
Show Gist options
  • Save micedreams/d2452ab96255da983e9527b361e46bc1 to your computer and use it in GitHub Desktop.
Save micedreams/d2452ab96255da983e9527b361e46bc1 to your computer and use it in GitHub Desktop.
Workflow to turn any repository into a journal that automatically creates a new entry every day and archives the last entry into an archive folder..
name: Journal
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
schedule:
- cron: '0 0 * * *'
jobs:
create-file:
runs-on: ubuntu-latest
steps:
- name: Set up Git repository
uses: actions/checkout@v2
- name: Create a new archive folder.
run: mkdir -p "archive"
- name: Create a new template file.
run: |
if [[ ! -f "template.md" ]]; then
touch "template.md"
echo "# Template" >> "${DATE}.md"
fi
- name: Archive last entry.
run: |
DATE=$(DATE --DATE="-1 day" +"%d.%m.%Y")
if [[ -f "${DATE}.md" ]]; then
mv "${DATE}.md" "archive/"
fi
- name: Create a new entry
run: |
DATE=$(DATE +"%d.%m.%Y")
touch "${DATE}.md"
file_size=$(stat -c%s "${DATE}.md")
if [[ $file_size -eq 0 ]]; then
echo "###### ${DATE}" >> "${DATE}.md"
cat "template.md" >> "${DATE}.md"
fi
- name: Commit files
run: |
if [[ ! -z "$(git status --porcelain)" ]]; then
git config --local core.autocrlf false
git config --local user.email "${{ github.actor }}@users.noreply.github.com"
git config --local user.name "${{ github.actor }}"
git add . && git add --renormalize .
git pull origin ${{ github.head_ref }} --autostash --rebase -X ours
git commit --allow-empty -am "Add new entry."
NO_PAGER=1 git --no-pager diff HEAD^
fi
- name: Push changes
uses: ad-m/github-push-action@v0.6.0
with:
branch: ${{ github.head_ref }}
github_token: ${{ secrets.TOKEN }}
@noordawod
Copy link

noordawod commented Nov 13, 2023

A few suggestions for improvement:

  • Always mention the Shell of the script, add shell: bash wherever you have a run: … line.
  • You need to have a space inside an if-then check: if [[! -f "archive" ]]; then -> if [[ ! -f "archive" ]]; then.
  • There's no need to check for the existence of a directory before creating it, it's enough to use mkdir -p 'folder'.
  • I like to CAPS variables in shell scripts (so DATE instead of date) – reasoning? UNIX-based commands are lowercase, would be harder to differentiate between date being a command from a variable.
  • When defining or putting a value in a variable, use quotes: DATE="$(date --date='-1 day' +'%d.%m.%Y')"
  • When you're passing an argument to a command, favor single quotes over double quotes. The latter must be used only if a variable needs to be evaluated. So, use --date='-1 day' instead of --date="-1 day", but if you want to use $COUNT somewhere and pass as a value, then you use --date="+$COUNT day".

Otherwise, a very nice workflow and a good writeup! Keep it up 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment