Skip to content

Commit 5a70b42

Browse files
committed
Add Capture An Output Value For Use In A Later Step as a github actions til
1 parent 66a3ae1 commit 5a70b42

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
1212

13-
_1055 TILs and counting..._
13+
_1056 TILs and counting..._
1414

1515
---
1616

@@ -25,6 +25,7 @@ _1055 TILs and counting..._
2525
* [Elixir](#elixir)
2626
* [Gatsby](#gatsby)
2727
* [Git](#git)
28+
* [GitHub Actions](#github-actions)
2829
* [Go](#go)
2930
* [HTML](#html)
3031
* [HTTP](#http)
@@ -298,6 +299,10 @@ _1055 TILs and counting..._
298299
- [What Is The Current Branch?](git/what-is-the-current-branch.md)
299300
- [Whitespace Warnings](git/whitespace-warnings.md)
300301

302+
### GitHub Actions
303+
304+
- [Capture An Output Value For Use In A Later Step](github-actions/capture-an-output-value-for-use-in-a-later-step.md)
305+
301306
### Go
302307

303308
- [Access Go Docs Offline](go/access-go-docs-offline.md)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Capture An Output Value For Use In A Later Step
2+
3+
GitHub Actions has a workflow command called `set-output`. This can be used to
4+
capture the output from a shell command in step. That output value can then be
5+
used in a later step.
6+
7+
A useful example of this is reading the version of a tool from a dot-file to
8+
tell a later step what version of that tool to install.
9+
10+
Here's the `.tool-versions` file included in my repository:
11+
12+
```
13+
postgres 13.1
14+
ruby 3.0.0
15+
nodejs 15.4.0
16+
```
17+
18+
Assuming I've already [checked out my
19+
repo](https://github.com/actions/checkout), I can find and read the `nodejs`
20+
version from my `.tool-versions` file with a step that uses `set-output`.
21+
22+
```yaml
23+
- name: Read Node.js version to install from `.tool-versions`
24+
id: nodejs
25+
run: >-
26+
echo "::set-output name=NODE_VERSION::$(
27+
cat .tool-versions |
28+
grep nodejs |
29+
sed 's/nodejs \(.*\)$/\1/'
30+
)"
31+
```
32+
33+
`echo` runs the command in the string which sets `NODE_VERSION` as an output
34+
value to what ends up being `15.4.0`.
35+
36+
This output value can be referenced in a later step.
37+
38+
```yaml
39+
- name: Install required Node.js version
40+
uses: actions/setup-node@v1
41+
with:
42+
node-version: "${{ steps.nodejs.outputs.NODE_VERSION }}"
43+
```
44+
45+
`steps` has a reference to the `nodejs` step (note the `id` above) which then
46+
has `outputs` like the `NODE_VERSION`.
47+
48+
[source](https://docs.github.com/en/actions/reference/workflow-commands-for-github-actions#using-workflow-commands-to-access-toolkit-functions)

0 commit comments

Comments
 (0)