8

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
  • 2,272
  • 2
  • 24
  • 41

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
  • 39,374
  • 15
  • 132
  • 179
3

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,262,500
  • 529
  • 4,410
  • 5,250