How do I use fromJSON with a matrix in GitHub Actions? - github

I'm trying to create a GitHub Action where a script outputs a list of sites that need to be deployed and then that list is used a the basis for matrix that does the deploying. Here's an example illustrating my point:
jobs:
setup:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.matrix.outputs.value }}
steps:
- id: matrix
run: |
echo '::set-output name=value::["site1", "site2", "site3"]'
build:
needs: [ setup ]
runs-on: ubuntu-latest
strategy:
matrix:
value: ${{fromJSON(needs.setup.outputs.matrix)}}
steps:
- run: |
echo "${{ matrix.value }}"
However, the editor underlines fromJSON and complains Invalid type found: array was expected but string was found.
If I copy/paste the fromJSON example from GitHub's documentation verbatim into my YAML file, I get a similar error and it doesn't work:
jobs:
job1:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- id: set-matrix
run: echo "::set-output name=matrix::{\"include\":[{\"project\":\"foo\",\"config\":\"Debug\"},{\"project\":\"bar\",\"config\":\"Release\"}]}"
job2:
needs: job1
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJSON(needs.job1.outputs.matrix) }}
steps:
- run: build
Any clues as to what I'm missing here?

Related

Environment variables in github actions

I want to pass maven image version as as env variable but when i am trying to access that env.MAVEN_VERSION variable getting error
Error- The workflow is not valid. .github/workflows/Merge.yaml (Line: 13 image:) Unrecognized named-value: 'env'. Located at position 1 within expression: env.MAVEN_VERSION
Yaml File ---
on:
push:
branches: [ master ]
env:
MAVEN_VERSION: maven:3.8.6-jdk-11
jobs:
build:
runs-on: ubuntu-latest
container:
image: ${{ env.MAVEN_VERSION }}
steps:
- name: Env Variable
run: echo ${{ env.MAVEN_VERSION }}
While env is not available, outputs from previous jobs are.
Consider the following example
on:
push:
branches: [ master ]
env:
MAVEN_VERSION: maven:3.8.6-jdk-11
jobs:
prepare-image:
runs-on: ubuntu-latest
outputs:
image: ${{ env.MAVEN_VERSION }}
build:
runs-on: ubuntu-latest
needs: [prepare-image]
container:
image: ${{ needs.prepare-image.outputs.image }}
steps:
- name: Echo output
run: echo ${{ needs.prepare-image.outputs.image }}
when i am trying to access that...
That's not what the error is telling you about. The error Unrecognized named-value: 'env' is telling you that GitHub is not recognizing the YAML you wrote at line 13. It's a syntax error.
In a GitHub workflow you can use env either in jobs.<job_id>.env or in jobs.<job_id>.steps[*].env. See here for details.
This YAML should work:
on:
push:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
container:
image: ${{ env.MAVEN_VERSION }}
steps:
- name: Env Variable
env:
MAVEN_VERSION: maven:3.8.6-jdk-11
run: echo ${{ env.MAVEN_VERSION }}
Also, note that when you only specify a container image, you can omit the image keyword.

Retrieve the error message from a github actions failed job

Assuming I have two jobs and the second depends on the first one:
jobs:
job-1:
runs-on: ubuntu-latest
steps:
- uses: somerepo/someaction#v1
- uses: anotherrepo/anotheraction#v1
- bash: shell
run: |
do-a-lot-of-stuff
job-2:
runs-on: ununtu-latest
needs: job-1
if: failure()
steps:
- shel: bash
run: echo ${{ needs.job-1.fail.message }}
Is there a way to capture the failure message from job-1?
(from whichever step failed of course)

How to set and change GitHub workflow output

I'm trying to figure out how to set an output, change the output, then use it it the following job. I've tried making this as simple as possible yet the last echo returns true. The documentation doesn't provide examples so any insight or advice would be great.
job-1:
runs-on: ubuntu-latest
outputs:
compare: true
steps:
- name: set output
run: |
echo "::set-output name=compare::false"
job-2:
runs-on: ubuntu-latest
# require the first job to have ran
needs: compare
steps:
- name: echo job-1 compare output
run: |
echo ${{ needs.job-1.outputs.compare }}
I learned you have to declare the value of compare in run and use an id
job-1:
runs-on: ubuntu-latest
outputs:
compare: ${{ steps.job_id.outputs.compare}}
steps:
- name: set output to false
id: job_id
run: |
echo "::set-output name=compare::false"
job-2:
runs-on: ubuntu-latest
# require the first job to have ran
needs: compare
steps:
- name: echo job-1 compare output
run: |
echo ${{ needs.job-1.outputs.compare }}

Custom label when using matrix in GitHub action

Given the following quite standard workflow:
name: Test
on: [push, pull_request]
jobs:
test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
ruby: [3.0, 3.1]
runs-on: ${{ matrix.os }}
steps:
- name: Check out
uses: actions/checkout#v2
- name: Set up Ruby ${{ matrix.ruby }}
uses: ruby/setup-ruby#v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run tests
run: SPEC_SCOPE=all bundle exec rake
The separate runs of test according to the matrix are labelled like so:
For the sake of better understanding, can the way the matrix is labelled be changed from "ubuntu-latest, 3.1" to something like "ubuntu-latest, ruby-3.1"?
#dhannanjay answered this on Github
You can change the name for a job by providing a name property. You can access all the metadata in the job configuration.
name: Run tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 13.x]
mongodb-version: [4.0, 4.2]
name: Node.js ${{ matrix.node-version }} - MongoDB ${{ matrix.mongodb-version }}
steps:
- name: …
(source: https://futurestud.io/tutorials/github-actions-customize-the-job-name)

GitHub PR checks showing jobs instead of workflows

I have a workflow that checks files on push and pull_request.
It has 2 jobs: One to list the changed files that match a pattern (Dockerfiles), and a second job with a matrix strategy to be executed for every file.
The jobs:
jobs:
get-files:
name: Get changed files
runs-on: ubuntu-latest
outputs:
dockerfiles: ${{ steps.filter.outputs.dockerfiles_files }}
steps:
- uses: dorny/paths-filter#v2
id: filter
with:
list-files: json
filters: |
dockerfiles:
- "**/Dockerfile"
check:
name: Check Dockerfiles
needs: get-files
strategy:
matrix:
dockerfile: ${{ fromJson(needs.get-files.outputs.dockerfiles) }}
runs-on: ubuntu-latest
steps:
- id: get-directory
# Remove last path segment to only keep the Dockerfile directory
run: |
directory=$(echo ${{matrix.dockerfile}} | sed -r 's/\/[^\/]+$//g')
echo "::set-output name=directory::$directory"
- run: echo "${{steps.get-directory.outputs.directory}}"
- uses: actions/checkout#v2
- name: Build Dockerfile ${{ matrix.dockerfile }}
run: docker build ${{steps.get-directory.outputs.directory}} -f ${{ matrix.dockerfile }}
My problem is, that the "get-files" ("Get changed files") job appears as a check in the pull requests:
Is there any way to hide it in the PR checks?
If not, is there a better way to have a check per modified file?