diff --git a/README.md b/README.md index e2a278f..44bc6da 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_1055 TILs and counting..._ +_1056 TILs and counting..._ --- @@ -25,6 +25,7 @@ _1055 TILs and counting..._ * [Elixir](#elixir) * [Gatsby](#gatsby) * [Git](#git) +* [GitHub Actions](#github-actions) * [Go](#go) * [HTML](#html) * [HTTP](#http) @@ -298,6 +299,10 @@ _1055 TILs and counting..._ - [What Is The Current Branch?](git/what-is-the-current-branch.md) - [Whitespace Warnings](git/whitespace-warnings.md) +### GitHub Actions + +- [Capture An Output Value For Use In A Later Step](github-actions/capture-an-output-value-for-use-in-a-later-step.md) + ### Go - [Access Go Docs Offline](go/access-go-docs-offline.md) diff --git a/github-actions/capture-an-output-value-for-use-in-a-later-step.md b/github-actions/capture-an-output-value-for-use-in-a-later-step.md new file mode 100644 index 0000000..5efa979 --- /dev/null +++ b/github-actions/capture-an-output-value-for-use-in-a-later-step.md @@ -0,0 +1,48 @@ +# Capture An Output Value For Use In A Later Step + +GitHub Actions has a workflow command called `set-output`. This can be used to +capture the output from a shell command in step. That output value can then be +used in a later step. + +A useful example of this is reading the version of a tool from a dot-file to +tell a later step what version of that tool to install. + +Here's the `.tool-versions` file included in my repository: + +``` +postgres 13.1 +ruby 3.0.0 +nodejs 15.4.0 +``` + +Assuming I've already [checked out my +repo](https://github.com/actions/checkout), I can find and read the `nodejs` +version from my `.tool-versions` file with a step that uses `set-output`. + +```yaml + - name: Read Node.js version to install from `.tool-versions` + id: nodejs + run: >- + echo "::set-output name=NODE_VERSION::$( + cat .tool-versions | + grep nodejs | + sed 's/nodejs \(.*\)$/\1/' + )" +``` + +`echo` runs the command in the string which sets `NODE_VERSION` as an output +value to what ends up being `15.4.0`. + +This output value can be referenced in a later step. + +```yaml + - name: Install required Node.js version + uses: actions/setup-node@v1 + with: + node-version: "${{ steps.nodejs.outputs.NODE_VERSION }}" +``` + +`steps` has a reference to the `nodejs` step (note the `id` above) which then +has `outputs` like the `NODE_VERSION`. + +[source](https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#using-workflow-commands-to-access-toolkit-functions)