5

As I have repetitve steps in my Github Actions, I would like to create a template. Let's make a example

name: ci
on: ["push"]

jobs:
  build-and-test:
    strategy:
      matrix:
        os: [ubuntu-latest]

    runs-on: ${{ matrix.os }}
    steps:
      - name: checkout
        uses: actions/checkout@v1

      - name: do stuff
        run: |
          bash stuff

Is it possible to save only the steps in a separated file? And import afterwards?

Ramon Medeiros
  • 1,909
  • 1
  • 17
  • 35

2 Answers2

4

Unfortunately it does not look like github-actions supports reusing workflows. Not even YAML anchors are supported.

It looks like the only way to share steps (not setup) is to create actions.

Update: A storm brewing

I have also caught wind of the possibility of reusing actions. Follow the issue to stay up-to-date.

smac89
  • 32,960
  • 13
  • 112
  • 152
2

I mentioned in "Reuse portion of GitHub action across jobs" that reusing GitHub Worfflow is now (Oct. 2021) available.

The documentation "Reusing workflows" includes a section "Reusable workflows and workflow templates", which leads to "Creating workflow templates"

If you need to refer to a repository's default branch, you can use the $default-branch placeholder.

When a workflow is created using your template, the placeholder will be automatically replaced with the name of the repository's default branch.

For example, this file named octo-organization-ci.yml demonstrates a basic workflow.

name: Octo Organization CI

on:
  push:
    branches: [ $default-branch ]
  pull_request:
    branches: [ $default-branch ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Run a one-line script
        run: echo Hello from Octo Organization
VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755