From 8129ad0eaa019b375ecbc40fdbeaa0c6d4faeec4 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 7 Oct 2026 22:50:13 -0500 Subject: [PATCH] Add Set Repository Secret From CLI as a GitHub Actions TIL --- README.md | 3 +- .../set-repository-secret-from-cli.md | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 github-actions/set-repository-secret-from-cli.md diff --git a/README.md b/README.md index cc4360f..2779134 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). -_1887 TILs and counting..._ +_1888 TILs and counting..._ See some of the other learning resources I work on: @@ -495,6 +495,7 @@ If you've learned something here, support my efforts writing daily TILs by - [Disable A Workflow With The gh CLI](github-actions/disable-a-workflow-with-the-gh-cli.md) - [Reference An Encrypted Secret In An Action](github-actions/reference-an-encrypted-secret-in-an-action.md) - [Run Schedule Action To Commit Regular Updates](github-actions/run-scheduled-action-to-commit-regular-updates.md) +- [Set Repository Secret From CLI](github-actions/set-repository-secret-from-cli.md) - [Trigger A Workflow Via An API Call](github-actions/trigger-a-workflow-via-an-api-call.md) - [Use Labels To Block PR Merge](github-actions/use-labels-to-block-pr-merge.md) diff --git a/github-actions/set-repository-secret-from-cli.md b/github-actions/set-repository-secret-from-cli.md new file mode 100644 index 0000000..8d1857b --- /dev/null +++ b/github-actions/set-repository-secret-from-cli.md @@ -0,0 +1,31 @@ +# Set Repository Secret From CLI + +I set up a GitHub Action that will run daily and potentially push data up to a +3rd-party API. To be able to post to that API, the action must include a secret +as the bearer token in the requests. I need to securely include that secret in +the GitHub Actions environment for this repo. + +I can do that from the CLI with `gh` like so: + +```bash +❯ gh secret set TIL_SYNC_TOKEN +? Paste your secret: **************************************************************** +``` + +Notice that it prompts me for the secret and stars out what I paste in. That way +the secret doesn't end up in my bash history and isn't visible on my screen. + +After I've run this, I can see that the secret is set by opening up the repo in +the GitHub web UI, clicking on _Settings_ > _Secrets and Variables_ > _Actions_. +I should see `TIL_SYNC_TOKEN` listed under the _Repository Secrets_ section. + +Here is an excerpt from my workflow action sync script where that secret is +referenced -- notice the `${{ secrets.TIL_SYNC_TOKEN }}`. + +```yaml + - name: Sync posts + env: + TIL_SYNC_URL: ${{ vars.TIL_SYNC_URL }} + TIL_SYNC_TOKEN: ${{ secrets.TIL_SYNC_TOKEN }} + run: ruby script/til_sync/til_sync.rb --repo til ${{ inputs.all && '--all' || '--days 3' }} +```