How to persist a file after halting a job

I have a conditional halt in one of my jobs. Just before the halt I write out to a file, and I would like to persist the file.

echo “Job halted” > ~/workspace/file.txt
circleci-agent step halt

I then try to persist the file.

- persist_to_workspace:
    root: ~/workspace
    paths:
    - file.txt
    when: always

However the persist step never runs. How can I save the file? Thanks.

You can use the pre and post steps

 https://circleci.com/docs/2.0/reusing-config/#using-pre-and-post-steps

These are placed within the workflows: section of the script rather than during the defining of a job or commands.