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?
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.
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
Related
Currently, my GitHub workflow looks as follows:
name: learn-github-actions
run-name: ${{ github.actor }} is learning GitHub Actions
on:
push:
branches:
- main
jobs:
update-x:
runs-on: ubuntu-latest
steps:
- name: Git checkout
uses: actions/checkout#v2
with:
# fetch depth is needed since we will be taking git diff with HEAD^1
fetch-depth: 2
- name: Run script
run: scripts/x/x-version-bump.sh
Instead of using git diff in my x-version-bump.sh script I would like to get the changes of the commit pushed via a GitHub action and pass it on to the script. I cannot find a way currently in Github actions to do the same.
Any to pass a variable (of sorts) for the version of a reusable workflow
For example ,if I have this workflow:
name: caller-workflow
on:
pull_request:
push:
branches:
- master
# 2
jobs:
call-the-workflow:
uses: action-foobar/action-testing/.github/workflows/called_workflow.yml#${{github.ref_name}}
with:
TRIGGER_EVENT : ${{ github.event_name }}
from the example above
`uses: action-foobar/action-testing/.github/workflows/called_workflow.yml#${{github.ref_name}}` using a `${{github.ref_name}}`
instead of a pinned reference.
Would like to be able to call the reusable workflow, using a reference (Thereby I could do easy automation on pull requests or any branch (for pipeline or CI development)
No - it's impossible unfortunately.
I have a publish workflow that is supposed to push the dists to PyPi if a commit on main is tagged with either frontend-v* or backend-v* (both are separate packages)
However, if a commit has changes on both the front- and backend and I add two tags to the commit, the workflow is triggered twice.
I understand that I could simply split the workflow into two, but I have another job that should run if either frontend or backend or both were updated which should only be ran once, thus I want to keep this in one workflow.
Can I somehow circumvent this to run this only once?
Thank you very much!
on:
workflow_dispatch:
push:
branches: [ main ]
tags:
- frontend*
- backend*
jobs:
backend:
name: Publish backend
if: ${{contains(github.ref_name, 'backend') }}
runs-on: ubuntu-latest
steps:
...
frontend:
name: Publish frontend
if: ${{contains(github.ref_name, 'frontend') }}
runs-on: ubuntu-latest
steps:
...
housekeeping: # this should only be ran once for the commit
name: House Keeping
runs-on: ubuntu-latest
steps:
...
I'm trying to organize my workflow artifacts (on a self-hosted runner) using a structure similar to this:
c:\github\artifacts\{org}\{repo}\{runid}
Different organizations in our enterprise COULD have a repository with the same name, so I wanted to be able to organize by organization name.
I have this so far:
c:\github\artifacts\{org}\{${{ github.event.repository.name }}\${{ github.run_id }}\
How can I determine the organization name?
You can get that from github context. Here are few that might be of use:
${{ github.repository }}
${{ github.repository_owner }}
If you don't know what are the values and which env variables are available to runner, you can just run env in a step to print it out, like so:
name: Print environment variables
on:
workflow_dispatch:
jobs:
debug:
runs-on: ubuntu-latest
steps:
- name: Print
run: env | sort
I have 2 conditions under which I want to run my workflows in GH actions on a go project
run unit tests on every PR (I have already taken care of this with the following workflow.yaml
name: unit-tests
on: [ pull_request ]
jobs:
unit-tests:
runs-on: Linux
steps:
- name: checkout project
uses: actions/checkout#v2
- name: dynamically retrieve go version
uses: arnested/go-version-action#v1
id: go-version
- name: setup go ${{ steps.go-version.outputs.minimal }}
uses: actions/setup-go#v2
with:
go-version: ${{ steps.go-version.outputs.minimal }}
- name: run unit tests
run: go test -race -cover -v ./... -run Unit
My second requirement is the following:
run unit tests AND proceed to creating a tag + release if the tests succeed and the target branch is master
My question is the following: Since the on clause is unique per workflow, is there a way I can leverage the above workflow (without the need to write a more or less clone) that can be integrated in the second use case?
First, reusing workflows is available since Oct. 2021
That means you can make a workflow on master branch, which would "call" (reuse) your second (existing) workflow, itself with its on: directive
The call would look like:
jobs:
call-workflow-passing-data:
uses: octo-org/example-repo/.github/workflows/reusable-workflow.yml#main
with:
username: mona
secrets:
envPAT: ${{ secrets.envPAT }}