I have a workflow which needs to execute either on a push or a pull request with the exception of the last step which pushes a package to NuGet (I don't want this to occur on a pull request, even to master).
How can I prevent the Publish NuGet step from running if the workflow is triggered from a pull request?
name: .NET Core
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Setup .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: 3.1.101
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
- name: Publish NuGet
uses: brandedoutcast/publish-nuget#v2.5.2
with:
PROJECT_FILE_PATH: "Orleans.Sagas/Orleans.Sagas.csproj"
NUGET_KEY: ${{secrets.NUGET_KEY}}
You can inspect the github.event_name context variable which contains the name of the event that triggered the workflow run. (eg, pull_request or push)
In this case, you can run a step for all events whose name is not pull_request with a github.event_name != 'pull_request' conditional on your step.
For example:
- name: Publish NuGet
uses: brandedoutcast/publish-nuget#v2.5.2
with:
PROJECT_FILE_PATH: "Orleans.Sagas/Orleans.Sagas.csproj"
NUGET_KEY: ${{secrets.NUGET_KEY}}
if: github.event_name != 'pull_request'
For future travellers, I found this action that worked quite well. I just needed to do an if: needs.pr-check.outputs.number != 'null' in order to filter by things being a PR or not.
https://github.com/8BitJonny/gh-get-current-pr
github.event_name != 'pull_request' did not work for me because the on.pull_request trigger doesn't exist for workflows that aren't launched by a PR.
Related
I have created a github action workflow to deploy code but now i need to add the approval functionality before deploying, once build is done approver should get a mail notification to approve the deployment, how can i achieve this?
below is my workflow file :
name: DEV Workflow
on:
push:
branches: [ dev ]
pull_request:
branches: [ dev ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up JDK 1.8
uses: actions/setup-java#v1
with:
java-version: '1.8'
distribution: 'adopt'
cache: maven
- name: Build with Maven
run: mvn clean install
- name: Unit Testing
run: echo "Hello World!"
- name: Deploy
run: echo "Hi World!"
Deploy-dev:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up JDK 1.8
uses: actions/setup-java#v1
with:
java-version: '1.8'
distribution: 'adopt'
cache: maven
- name: Build with Maven
run: mvn clean install
- name: Deploy
run: echo "Hi World!"
unit-testing:
needs: Deploy-dev
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Set up JDK 1.8
uses: actions/setup-java#v1
with:
java-version: '1.8'
distribution: 'adopt'
cache: maven
- name: Unit Testing
run: echo "Hello World!"
On Github Actions you can use Environments to set required approvers (this will send emails to the users when needed).
You can list multiple teams / people that can approve the job, and only 1 person needs to approve it from that list for the workflow to continue, as explained to this other section of the documentation:
Use required reviewers to require a specific person or team to approve workflow jobs that reference the environment. You can list up to six users or teams as reviewers. The reviewers must have at least read access to the repository. Only one of the required reviewers needs to approve the job for it to proceed.
If you don't want to use this solution, a workaround could be to use this manual-approval action, which also allows you to inform more than 1 approver.
I am new to writing GH Actions. I am working on a task to remove common workflows and use the reusable workflow feature available. I am now able to get my workflows to run sequentially which is great. However, the 2nd workflow is resulting in an unexpected error message seemingly related to the yarn dependency workflow not saving to the cache as I would have expected it:
Run yarn lint
... snip
myPackage: /bin/sh: 1: concurrently: not found
Could you take a look at see if this looks ok? For now, my goal is to have a workflow for pull-request which calls yarn and lint as the resuable features:
name: pull-request
on:
pull_request:
branches:
- main
jobs:
yarn:
uses: ./.github/workflows/yarn.yml
validate_lint:
needs: yarn
uses: ./.github/workflows/validate_lint.yml
with:
name: Yarn
on:
workflow_call:
jobs:
yarn_and_deps:
name: Run Lint
runs-on: ubuntu-latest
steps:
- name: Checkout Git repository
uses: actions/checkout#v3
- name: Enable node
uses: actions/setup-node#v3
with:
node-version: 16
- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- uses: actions/cache#v3
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Install dependencies
run: yarn install --frozen-lockfile && yarn bootstrap
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
and
name: Validate Lint
on:
workflow_call:
jobs:
run_lint:
name: Run Lint
runs-on: ubuntu-latest
- name: Enable node
uses: actions/setup-node#v3
with:
node-version: 16
cache: 'yarn' # <<--- THIS CACHE IS NOT FOUND 🤷🏻♂️
# NOTE: if I add in all the "yarn cache/install" commands from above workflow, this passes.
steps:
- name: Validate Lint
run: yarn lint
The error happens here in the Validate Lint job because it appears that the cache is not found. I made the yarn job to avoid re-creating the wheel for each job.
What is wrong with my expectations on the cache v how it actually works? Having to Install dependencies step each job feels like overkill.
Turns out each workflow is its own docker container. Therefore, if I run yarn in workflow 1's container, workflow 2 has no knowledge/access to the cache.
The closest thing appears to be an upload/download "sharing of data", but this has it's own drawbacks - such as downloading a node_modules folder can be slower than just installing the dependencies.
Unfortunately, the solution seems to be that there is repetition of code when each workflow has a dependency to the output of prior item.
I have been trying to get this right for hours, and nothing I have managed to find has helped. I am trying to setup a github action that will run tests on every pull request into master and any changes to the master branch, but only run the deploy step when there are changes to the master branch.
Here is a simple reproduction of what I am trying to do.
name: Main
on:
push:
branches:
- "main"
pull_request:
branches:
- "main"
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Test
run: echo "running tests"
- name: Deploy
run: echo "Deploying"
if: github.head_ref == 'main'
I have tried multiple conditionals I have found here / on other forums, I have tried moving the if statement above and below run I am completely out of ideas. Everything I have tried either runs the deploy step on both pull request and merge or skips the deploy step on both pull request and merge.
Just use:
github.ref == 'refs/heads/master'
or:
github.ref == 'refs/heads/main'
Depending on which branch you want to check
This is what I do:
build:
runs-on: [...]
steps:
- name: Echo on merging to master
if: ${{ github.ref == 'refs/heads/master' }}
run: echo "I'm merging!"
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 }}
I am getting the following error message on github actions when msbuild is run for my xamarin.android project:
_ResolveAndroidTooling:
Found Java SDK version 14.0.2.
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Legacy.targets(248,5): error XA0030: Building with JDK version `14.0.2` is not supported. Please install JDK version `11.0`
my actions.yaml looks like this:
name: CI
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
pull_request:
branches: [ master ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
Android:
runs-on: macos-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-java#v2
with:
distribution: 'adopt' # See 'Supported distributions' for available options
java-version: '11'
#- run: java -cp java HelloWorldApp
- uses: taori/xamarinandroid-signedbuild-action#v10
with:
csproj_path: src/Droid.csproj
signing_keystore: ${{ secrets.ANDROID_KEYSTORE }}
keystore_password: ${{ secrets.ANDROID_KEYSTORE_PASS }}
signing_key_alias: ${{ secrets.ANDROID_KEY_ALIAS }}
signing_key_password: ${{ secrets.ANDROID_KEY_PASS }}
configuration: "Release"
mono_version: "stable"
xamarin_android_version: "stable"
- uses: actions/upload-artifact#v2
with:
name: ipa
path: src/*.Droid/bin/Android/Release/**Signed.apk
Does actions/setup-java#v2 not install 11.x? Or is this an issue with msbuild not picking up on the environment variables set by that action?
References
Known issue for azure pipelines
Does actions/setup-java#v2 not install 11.x?
It should, since PR 132 and v2.
Try and simplify your action, to only keep the java installation part, and validate it does work:
- name: setup-java
uses: actions/setup-java#v2-preview
with:
distribution: 'adopt'
java-version: '11'
Then add back the other elements of your original action, and see when the issue manifests itself again.