How to access reference in reusable workflow - github-actions

Context
A reusable workflow in public repos may be called by appending a reference which can be a SHA, a release tag, or a branch name, as for example:
{owner}/{repo}/.github/workflows/{filename}#{ref}
Githubs documentation states:
When a reusable workflow is triggered by a caller workflow, the github context is always associated with the caller workflow.
The problem
Since the github context is always associated with the caller workflow, the reusable workflow cannot access the reference, for example the tag v1.0.0. However, knowing the reference is important when the reusable workflow needs to checkout the repository in order to make use of composite actions.
Example
Assume that the caller workflow is being executed from within the main branch and calls the ref v1.0.0. of a reusable workflow:
name: Caller workflow
on:
workflow_dispatch:
jobs:
caller:
uses: owner/public-repo/.github/workflows/reusable-workflow.yml#v1.0.0
Here is the reusable workflow that uses a composite action:
name: reusable workflows
on:
workflow_call:
jobs:
first-job:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3.1.0
with:
repository: owner/public-repo
ref: ${{ github.ref_name }}
- name: composite action
uses: ./actions/my-composite-action
In the above code snippet, ${{ github.ref_name }} is main instead of v1.0.0 because github context is always associated with the caller workflow. Therefore, the composite actions code is based on main and not on v1.0.0. However, the caller wanted v1.0.0.
Hence my question: how is the reusable workflow able to access the reference given by the caller?

Related

Is it possible to loop a Github Actions step?

I have a Github Actions workflow dealing with new releases. Some last steps build the application for different platforms. Instead of creating multiple steps where each one builds for a different platform or creating a step running multiple commands I'm looking for a way to loop a step for each item in an array.
I know there is a matrix for jobs so this is my pseudo implementation to show what I'm looking for
jobs:
do_it:
runs-on: ubuntu-latest
steps:
- name: For each entry in the array...
strategy:
matrix:
target: [ this, that, something ]
run: echo ${{ matrix.target }}
Is it possible to create something similiar to a matrix so it will loop the step multiple times?
As a sidenote, I know there is a similiar question Using an array of values to repeat a step in GitHub Actions workflow but I don't want to split the job into multiple jobs because then I have to deal with all the build artifacts.
To loop the step multiple times, try something like this:
jobs:
do_it:
runs-on: ubuntu-latest
steps:
- name: For each entry in the array...
env:
PLATFORMS: [ linux, macOs, windows ]
run: |
platform_list=$(echo "${{ env.PLATFORMS }}" | tr -d '[],')
for target in $platform_list; do
echo "Building for $target platform..."
# make $target done
done
Here -- basically, the env step sets an environment variable PLATFORMS to an array of values, and the run step uses a shell for loop to iterate through the values in the array.

Github workflow with multiple types of same event

I'm having difficulties figuring out the syntax for triggering off of different event types.
For example the following gives me a "duplicated mapping key" error on the secod pull_request trigger.
on:
pull_request:
types: [opened, reopened]
branches:
- main
- develop
pull_request:
types [synchronize]
branches:
- main
- develop
paths: ['**.h', '**.cpp', '**.hpp', '**.yaml', '**CMakeLists.txt', '**Makefile', '**.spec', '**.py', '**Dockerfile', '**conanfile.txt']
I want the workflow to always run when first opened (or reopened) but subsequently when the branch is synchronized it should only run if the changes are in one of the specified file types.
To clarify, I already have on.push event hook that's not shown here for the sake of brevity.
I do believe I neeed to have a pull_request.synchronize event to handle updated.
Can't find anything in the documentation on how to do that. I tried combining the two pull_requests triggers but then I'm getting an error that the "types" key is being duplicated.
Any ideas?
The documentation does talk about triggering based on multiple events, but not multiple events of the same type, so it isn't entirely clear if this is possible (beyond the validation errors).
To make this work you need to define three different workflows, one with each varying type of event and its filters and another with the reusable workflow using a workflow_call event.
#workflow-1
on:
pull_request:
types: [opened, reopened]
branches:
- main
- develop
jobs:
job:
uses: ./.github/workflows/workflow-3.yml
#workflow-2
on:
pull_request:
types: [synchronize]
branches:
- main
- develop
paths: ['**.h', '**.cpp', '**.hpp', '**.yaml', '**CMakeLists.txt', '**Makefile', '**.spec', '**.py', '**Dockerfile', '**conanfile.txt']
jobs:
job:
uses: ./.github/workflows/workflow-3.yml
#workflow-3
on:
workflow_call:
jobs:
job:
steps:
- run: do stuff

GitHub Actions: Cannot inherit secrets using reusable workflows

The Github actions documentation on reusable workflows specifies the syntax to pass secrets from caller workflow to the called workflow using the secrets: inherit tag. However, when I copy this I get an error saying
Unexpected value 'inherit'
Does anyone know what I might be doing wrong?

How do I merge multiple lists using JSON ref?

I have the following YAML
- name: Core
description: Core functionality
- name: Artifact
description: Artifact management
# - $ref: "v1/publications.yml#/tags/"
v1/publications.yml has
tags:
- name: Publication
description: |
This defines the publication API.
I sort of want the result to be
I have the following YAML
- name: Core
description: Core functionality
- name: Artifact
description: Artifact management
- name: Publication
description: |
This defines the publication API.
# - $ref: "v1/publications.yml#/tags/"
I can do it one at a time like this...
- name: Core
description: Core functionality
- name: Artifact
description: Artifact management
- $ref: "v1/publications.yml#/tags/0"
But I want it to add multiple without updating my source.
This is not possible with the technologies you tagged. $ref is exactly that, a reference to an external subtree. You need sequence concatenation, which is something neither json-ref nor plain YAML or JSON provide.
You may be able to do this using some templating technology, which many YAML-based utilities provide. If you are in control of the loading code, you can also implement custom tags to do something like
- name: Core
description: Core functionality
- name: Artifact
description: Artifact management
- !append {$ref: "v1/publications.yml#/tags"}

Codeception coverage report 0% though the test runs

I cannot set up the code coverage configuration, the report is always 0%. I'm using codeception coverage with two projects, the first one with:
Yii2
WebDriver module
Weird stuff:
I have two codeception.yml:
/tests/codeception.yml
/codeception.yml
c3.php is not in root. It is on /vendor/codeception/codeception/tests/data/claypit/c3.php
I'm not sure where I have to include c3.php
Like I'm not sure which codeception.yml is the right file, I have the same configuration on both files.
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
colors: false
memory_limit: 1024M
modules:
config:
coverage:
enabled: true
remote: false
include:
- /controllers/*
c3_url: 'http://127.0.0.1/tmsO/#/'
I have the same problem with the second project, the differences are that I'm using:
Yii1
PhpBrowser module
Asserts module
REST module
Thank you in advance. I really need help.
When you use Codeception for acceptance Tests and using PHPBrowser/WebDriver you need remote coverage (remote: true). Therefore it will always say 0% in the console but will be saved in the _output directory.
On the remote site, the c3.php will collect all the needed data. So you need to include it in every call to your application. (for more information see https://github.com/Codeception/c3).
You can find the documentation for setting up the code coverage here: http://codeception.com/docs/11-Codecoverage