There are often steps which need to be repeated with different inputs. These steps can be extracted and reused, just like in software development the dry principle.
.github/actions/extracted-step/action.yaml
name: Greet and create
description: 'Greet someone and create a random number as output'
inputs:
who-to-greet:
description: 'Who to greet'
required: true
outputs:
random-number:
description: "Random generated number"
value: ${{ steps.random-number-generator.outputs.random-number }}
runs:
using: "composite"
steps:
- name: Set Greeting
run: echo "Hello $INPUT_WHO_TO_GREET."
shell: bash
env:
INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}
- name: Random Number Generator
id: random-number-generator
run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
shell: bash
.github/workflows/example.yaml
jobs:
greet-randomize:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- id: greet-and-randomize
uses: ./.github/actions/greet-and-randomize
with:
who-to-greet: 'Hack&Heat'
- run: echo random-number "$RANDOM_NUMBER"
shell: bash
env:
RANDOM_NUMBER: ${{ steps.greet-and-randomize.outputs.random-number }}
-> https://github.com/senacor/workshop-github-action-action
greet-randomize-external:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- id: greet-and-randomize
uses: senacor/[email protected]
with:
who-to-greet: 'Hack&Heat'
- run: echo random-number "$RANDOM_NUMBER"
shell: bash
env:
RANDOM_NUMBER: ${{ steps.greet-and-randomize.outputs.random-number }}