Can you have multiple working directories with github actions? - go

So I have a repo with multiple directories for multiple go projects. Is there a way to run github actions on multiple working directories so I don't have to have redundant yaml with github actions? To automate an error check with golang, I currently have:
errcheck:
name: Errcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#master
- name: check
uses: grandcolline/golang-github-actions#v1.1.0
working-directory: ./app1
with:
run: errcheck
token: ${{ secrets.GITHUB_TOKEN }}
But I'd like to have:
errcheck:
name: Errcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#master
- name: check
uses: grandcolline/golang-github-actions#v1.1.0
working-directory: [./app1, ./app2]
with:
run: errcheck
token: ${{ secrets.GITHUB_TOKEN }}

In order to run something in more than one working directory, I believe you have two options:
Option 1: Matrix
Use GitHub Action's jobs.<job_id>.strategy.matrix option. This will create multiple jobs, each with its own matrix (directory) value.
Here is a sample workflow:
name: Test
on:
push: { branches: master }
jobs:
test:
name: Matrix test
runs-on: ubuntu-latest
strategy:
matrix: { dir: ['some-dir', 'other-dir'] }
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Do something with the matrix value
working-directory: ${{ matrix.dir }}
run: pwd
Running this will create two jobs:
Option 2: Custom Shell Script
If the matrix option is not suitable for your needs, a simple shell script that loops through and tests all your nested applications (directories) might be appropriate. In this case, you ignore the working-direcoty directive in the workflow YAML, and let the script cd to each of them.
For example:
#!/usr/bin/env bash
dirs=( some-dir other-dir )
for dir in "${dirs[#]}"; do
pushd "$dir"
pwd # Do something more significant here
popd
done

Related

Parametrise uses parameter in GitHub Actions

How I can pass parameter inputs.custom to this actions code:
jobs:
test-custom:
name: Test Custom
uses: ./.github/workflows/work4-${{ inputs.custom }}.yml
Fully working example:
First workflow work1-build.yml:
name: Start workflow fail
on: [push]
jobs:
build-fail:
name: Build with other workflow
uses: ./.github/workflows/work3-build-fail.yml
with:
custom: custom-name1
Second workflow work3-build-fail.yml:
name: Build fail with input test
on:
workflow_call:
inputs:
custom:
description: Some custom string
required: true
type: string
jobs:
test-custom:
name: Test Custom
uses: ./.github/workflows/work4-${{ inputs.custom }}.yml
Third workflow work4-custom-name1.yml
name: Custom 1
on:
workflow_call
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "IN CUSTOM 1"
Example above make GitHub respond with an error:
Invalid workflow file
error parsing called workflow
".github/workflows/work1-build.yml"
-> "./.github/workflows/work3-build-fail.yml" (source branch with sha:720087c8794e76f52277f9b1229b44ea65ab89d5)
--> "./.github/workflows/work4-${{ inputs.custom }}.yml"
: failed to fetch workflow: workflow was not found.
I can successfully add ${{ inputs.custom }} to:
test-print:
runs-on: ubuntu-latest
name: Print input
steps:
- name: Step print input
run: echo ${{ inputs.custom }}
Docs doesn't contain any examples with uses parametrisation:
https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsuses
I found documentation that proves this isn't possible. There is no supporting info stating that the uses key has access to any contexts.
See: https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability
I believe this is an architectural limitation of GitHub Actions, it appears they want to resolve all workflows/actions at the start of all jobs and thus dynamic resolution isn't possible.

GitHub Actions Matrix sharing the Same Code CheckOut

I tried to perform step actions/checkout#v3 once on chained jobs, but it seems like the "build" job does not get the code. I'm getting an error "can't find the project".
Can I call actions/checkout # v3 once for two jobs?
It works when I call the code checkout twice.
name: publish-nuget
on:
push:
branches:
- main
jobs:
prepare:
runs-on: ubuntu-latest
- name: Checkout code
uses: actions/checkout#v3
- name: Get package version
id: get_package_version
uses: kzrnm/get-net-sdk-project-versions-action#v1.3.0
with:
proj-path: ProjectOne.csproj
build:
needs: prepare
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v3
# Add the projects path below
strategy:
matrix:
projects: [
'ProjectOne.csproj',
'ProjectTwo.csproj',
]
steps:
- name: Pack NuGet
run: dotnet pack ${{ matrix.projects }} -p:PackageVersion=${{ env.PACKAGE_VERSION }} --configuration Release
It does not work when I call the code checkout once (on the 'prepare' job).
name: publish-nuget
on:
push:
branches:
- main
jobs:
prepare:
runs-on: ubuntu-latest
- name: Checkout code
uses: actions/checkout#v3
- name: Get package version
id: get_package_version
uses: kzrnm/get-net-sdk-project-versions-action#v1.3.0
with:
proj-path: ProjectOne.csproj
build:
needs: prepare
runs-on: ubuntu-latest
steps:
# Add the projects path below
strategy:
matrix:
projects: [
'ProjectOne.csproj',
'ProjectTwo.csproj',
]
steps:
- name: Pack NuGet
run: dotnet pack ${{ matrix.projects }} -p:PackageVersion=${{ env.PACKAGE_VERSION }} --configuration Release
Having a job being dependent on another job, is just for logical purposes and not state or artifact dependency sharing. You are actually runing the 2 jobs on 2 different agents. If you want to share something from the prepare job, you can use the cache or artifact API. E.g. using the cache API to cache the path 'somePath'. Same step for downloading the cache.
- name: Cached build artifacts
uses: actions/cache#v2
id: artifactcache
with:
path: somePath
key: buildArtifacts${{ github.run_number}}
As you are not gaining anything form splitting this up into 2 jobs, I would run everything in a single job instead.

GitHub Actions restrict job with tag validation using if condition or github ref

I have an application code which runs the maven builds we created snapshots and then create a tag created once the snapshot build is successful, and then we use the tag created in the snapshot build and then run the build using the tag for release using the GitHub actions workflows.
now I'm trying to run the release of the build workflow job only if the tag as matching and if the tag, not matches, skip the jobs or fails the jobs, let's say the tag will be created in the snapshot build and will use tag format as "<appname>-<app_build_type>-<github-actions-run_number>-<env>-origin/<branch>" and the actual tag looks like "java-maven-44-dev-origin/develop"
I was trying the below 2 methods workflows, but it is not at all working
1st Method is:
name: maven-release
on:
workflow_dispatch:
jobs:
checkout_code:
runs-on: [ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout#v3
with:
fetch-depth: 0
maven_build:
runs-on: [ubuntu-latest]
if: ${{ github.ref == 'refs/tags/*-origin/develop' }}
needs: checkout_code
steps:
- name: Run maven build
run: |
mvn clean install
2nd Method is:
name: maven-release
on:
workflow_dispatch:
jobs:
checkout_code:
runs-on: [ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout#v3
with:
fetch-depth: 0
maven_build:
runs-on: [ubuntu-latest]
if: contains(github.ref, 'refs/tags/*-origin/develop')
needs: checkout_code
steps:
- name: Run maven build
run: |
mvn clean install
I want to skip/fail the at job level only and no in run command, if anyone knows any solution to this or anyone can provide some suggestions on how to achieve this
Its it's possible for your use case, you could trigger your workflow on push and then apply your filter to the branch or tag
name: maven-release
on:
workflow_dispatch:
push:
tags:
- '*-origin/develop'
jobs:
checkout_code:
runs-on: [ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout#v3
with:
fetch-depth: 0
maven_build:
runs-on: [ubuntu-latest]
if: ${{ github.ref == 'refs/tags/*-origin/develop' }}
needs: checkout_code
steps:
- name: Run maven build
run: |
mvn clean install
The Github filter pattern cheat sheet
For the workflows for which each task needs to be executed only on a particular tag, then we can make use of below snipped
name: maven_build
on:
workflow_dispatch:
jobs:
checkout:
runs-on: [ubuntu-latest]
steps:
- name: Checkout
uses: actions/checkout#v3
with:
fetch-depth: 0
maven_build:
runs-on: [ubuntu-latest]
if: startsWith(github.ref, 'refs/tags/') && !contains(github.ref')
needs: checkout
steps:
- name: Run maven build command
run: |
mvn clean install
The above snippet will run only when the checkout branch matches a tag

Run command taking output from previous step on GitHub actions

I'm trying to make a GitHub action that builds a Hugo website, deploys it on Pinata and saves the output hash of this last step to a txt file. I managed to achieve the first and second steps. And, for the third one, I've been trying to do it by running an "echo" command. However, I get this message: "You have an error in your yaml syntax on line 36"
How do I run the script taking the output from the step identified as "ipfs-pin"?
Here's my code:
name: deploy
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#master
- uses: jakejarvis/hugo-build-action#master
with:
args: --minify --buildDrafts
- uses: anantaramdas/ipfs-pinata-deploy-action#v1.6.4
id: ipfs-pin
with:
pin-name: '[my-pin-name]'
path: './public'
pinata-api-key: [API Key]
pinata-secret-api-key: [secret API Key]
verbose: true
remove-old: true
saves-hash-on-file:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v2
with:
node-version: '14'
- run: echo ${{steps.build.ipfs-pin.hash}} > /.github/ipfs-hash.txt
First
It seems your indentation has a problem, I reproduced the workflow to correct it without returning error when pushing the workflow on the repository:
name: Deploy
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
outputs:
hash: ${{ steps.ipfs-pin.outputs.hash }}
steps:
- uses: actions/checkout#master
- uses: jakejarvis/hugo-build-action#master
with:
args: --minify --buildDrafts
- uses: anantaramdas/ipfs-pinata-deploy-action#v1.6.4
id: ipfs-pin
with:
pin-name: '[my-pin-name]'
path: './public'
pinata-api-key: '[API Key]'
pinata-secret-api-key: '[secret API Key]'
verbose: true
remove-old: true
saves-hash-on-file:
runs-on: ubuntu-latest
needs: [build]
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v2
with:
node-version: '14'
- run: echo ${{steps.build.outputs.hash}} > /.github/ipfs-hash.txt
Second
As you can see on the workflow above, I added the outputs field at the job1 (build) level, without this you can't share the output on other jobs.
Reference about outputs
Moreover, to share outputs between jobs, you will have to add the needs: [build] line at the job2 (saves-hash-on-file) level.
Note: I couldn't run it successfully as I don't have any credential to test, but it should work if you copy/paste the workflow I shared using your credentials.

GitHub Action unable to execute build executable

Try to use GitHub Actions but getting error when trying to run executable build. I can find executable when doing ls and also changing mod of file executable but didn't got anything. Attached log/output of GitHub action.
Here is my github action workflow file
on:
push:
branches:
- master
name: Build For platforms
jobs:
build:
# We want to define a strategy for our job
strategy:
# this will contain a matrix of all of the combinations
# we wish to test again:
matrix:
go-version: [1.14.x]
platform: [ubuntu-latest]
# Defines the platform for each test run
runs-on: ${{ matrix.platform }}
# the steps that will be run through for each version and platform
# combination
steps:
# sets up go based on the version
- name: Install Go
uses: actions/setup-go#v2
with:
go-version: ${{ matrix.go-version }}
# checks out our code locally so we can work with the files
- name: Checkout code
uses: actions/checkout#v2
- name: Download modules
run: go mod download
- name: Build
run: go build -o executable main.go
- name: Chmod
run: chmod +x executable
- name: List Files
run: ls && pwd
- name: Run
run: ./executable

Resources