Extract tasks from Obsidian vault with Github actions
I wanted to be able to pull my tasks out of my vault as a summary view, this can open up future automation options for me. However as I use multiple devices no single place was great and if I am on my phone it makes it harder.
As I am synchronizing my vault with git I decided to use a github workflow to help me out. I have it setup to run once a hour and extract all the tasks that are visible in the Obsidian Tasks plugin. These can be found as they always have the global task tag in them. In this example I use #task, update the regex as needed.
To store the exported tasks I use a storage account in Azure as I can automate from there in multiple ways. You can replace that with whatever option you want to store the file or use the action to do further processing and even check in an updated file for more complex scenarios that Templater and the like cannot help with.
The contents of the file can be split on the :
delimiter.
Path to file from Vault root:Line Number:Task
To process the data in PowerShell in a nicer way assuming $Content
contains the contents of the file. Use this command.
$Content -split "`n" | % { [pscustomobject]@{File = $_.Split(':')[0]; Line = $_.Split(':')[1]; Task = $_.Split(":")[2..($_.Split(":").Length)] -join ""} }
This will output something like below, you can take this and manipulate as much as you need.
Notes/One.md 5 - [x] #task Walk the dog π
2021-07-09 β
2021-07-09
Notes/One.md 6 - [x] #task Talk to [[Fred]] about the weather π
2021-07-09 β
2021-07-09
Notes/One.md 7 - [ ] #task Plan a cool trip! #area/family π
2021-12-09
Notes/Two.md 5 - [x] #task Take the bins out π
2021-07-09 β
2021-07-09
Notes/Two.md 6 - [x] #task Buy Milk π
2021-07-09 β
2021-07-09
Notes/Two.md 7 - [x] #task Pay Bills π
2021-07-09 β
2021-07-12
Github Actions Workflow
- I store the SAS token with the URI in Github secrets, do not store any secret directly in the workflow. You will need to make this secret with the SAS token. This also uses PowerShell core as I wanted the script to be cross platform.
- I am trimming out the path to the files so it is relative to the root.
- This is scheduled to run once a hour.
name: Pull and Publish Tasks
on:
schedule:
- cron: "0 * * * *"
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Export tasks and upload
shell: pwsh
env:
BLOBSTORAGE_URI_WITH_SAS_TOKEN: ${{ secrets.BLOBSTORAGE_URI_WITH_SAS_TOKEN }}
run: |
$exportFileName = "tasks-export.md"
$allMarkdownTasks = Get-ChildItem -Path ./*.md -Recurse | Select-String -Pattern "^.*[-|\*]?\[[ |x]\].*#task.*"
$allMarkdownTasks -replace "$([Regex]::Escape($PWD.Path))[\\|/]", '' | Set-Content "./$exportFileName"
$uri = $env:BLOBSTORAGE_URI_WITH_SAS_TOKEN.replace("?","/tasks.md?")
$method = "Put"
$inFile = (Resolve-Path "./$exportFileName").Path
$headers = @{ "x-ms-blob-type" = "BlockBlob"; "Content-Type" = "text/html; charset=UTF-8"; "Content-Encoding" = "UTF-8" }
Invoke-RestMethod -Uri $uri -Method $method -InFile $inFile -Headers $headers