user989988
user989988

Reputation: 3746

Nested templates (calling a yaml file from another yaml file) in GitHub Actions

Does GitHub action support nested templates? For example, here is an example of Azure Pipeline yaml where it calls another yaml file:

- job: BuildFunctions
    
  steps:
  - ${{ each func in parameters.functionApps }}:
    - template: yaml/build-functionapps.yml
      parameters:

Is it possible to call a yaml file from another yaml file in GitHub actions?

Upvotes: 12

Views: 16044

Answers (4)

ccoutinho
ccoutinho

Reputation: 4576

You can also use reusable workflows.

Upvotes: 0

Munim
Munim

Reputation: 2778

I think using the composite action pattern, you can achieve what you want.

You need to define the steps which you think will be reused in other places, and make it parameterized, by providing inputs. In my opinion, it's more powerful than how templates work in gitlab or in other similar platforms.

This way, you are defining a function, which can take inputs, and get stuff done for you, based on those inputs.

Also, even though the docs suggest that, you should create your leaf action as a separate public repo, and use it in your base action- it's not necessary, you can simply have a structure like below(taken the example from one of our live workflow), and use those leaf actions in your workflow-

.github
  - actions
    - deploy-to-k8s
      - action.yaml
    - publish-image
      - action.yaml
  - workflows
    - deploy-from-pr.yaml <-- this will make use of all the actions defined

Here's how the deploy-from-pr.yaml workflow looks like-

name: deploy-from-pr

on:
  pull_request:
    branches:
      - master

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: ${{ github.repository }}

jobs:
  deploy-from-pr:
    name: Deploy from PR to Development
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Set version
        id: release
        run: echo ::set-output name=version::$(git describe --always)

      # custom action to build and push image
      - name: Build & publish image
        uses: ./.github/actions/publish-image # see how it's referred from same repo directly
        with:
          registry: ${{ env.REGISTRY }}
          registry_username: ${{ github.REGISTRY_USERNAME }}
          registry_password: ${{ secrets.REGISTRY_PASSWORD }}
          image_name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          tag: ${{ steps.release.outputs.version }}

      # custom action to deploy into kubernetes
      - name: Deploy to kubernetes
        uses: ./.github/actions/deploy-to-k8s # see how it's referred from same repo directly
        with:
          digitalocean_token: ${{ secrets.DIGITALOCEAN_TOKEN }}
          cluster_name: ${{ secrets.CLUSTER_NAME }}
          overlay_path: .k8s/overlays/dev
          image_name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
          tag: ${{ steps.release.outputs.version }}

Github Gist

You can check the deploy-to-k8s/action.yaml, to see how it's written.

Upvotes: 2

riQQ
riQQ

Reputation: 12863

You can use composite run steps actions. These are actions that are solely defined in YAML (documentation).

You can now specify containers, other composite actions (up to a depth of 9) and node actions in additional to the previously available run steps

node actions likely refers to leaf actions, i.e. actions that don't call any other actions.

Source: https://github.com/actions/runner/issues/646#issuecomment-901336347

Workflow

[...]

jobs:
  job:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: ./.github/workflows/composite-action

[...]

Composite run steps action
.github/workflows/composite-action/action.yml (same repository as the workflow)

name: "My composite action"
description: "Checks out the repository and does something"
runs:
  using: "composite"
  steps:
  - uses: actions/checkout@v2
  - uses: actions/setup-node@v2
    with:
      node-version: 12
  - run: npm test
    shell: bash
  - run: |
      echo "Executing action"
    shell: bash

Old limitations:

What does composite run steps currently support?

For each run step in a composite action, we support:

  • name
  • id
  • run
  • env
  • shell
  • working-directory

In addition, we support mapping input and outputs throughout the action.

See docs for more info.

What does Composite Run Steps Not Support

We don't support setting conditionals, continue-on-error, timeout-minutes, "uses" [remark: i.e. using other actions], and secrets on individual steps within a composite action right now.

(Note: we do support these attributes being set in workflows for a step that uses a composite run steps action)

Source: https://github.com/actions/runner/issues/646

Upvotes: 12

Meiswjn
Meiswjn

Reputation: 915

No, it is not. I asked the exact same question in the GitHub Forum:

Is it possible to create / publish Actions without Docker or JS by having the code in the Workflow Syntax / YML?

As mentioned in the document: Currently, types of actions only lists Docker container and JavaScript, so there is no such feature to achieve your requirement. Source: https://github.community/t/how-to-create-ready-to-use-action-without-docker-js/124889/2

This would have eased creating templates for users as system administrator.

Upvotes: 0

Related Questions