I would like to setup a workflow in github yml such that I have some default values for variables and also would like to be able to manually provide the values to these variables when running the workflow manually.
I understood that we can use workflow_dispatch to set some input variables when running manually. However, when the workflow is executed as part of a code push, these variables (runTests and uploadArtifacts) are coming as null.
name: Example
on:
workflow_dispatch:
inputs:
runTests:
description: run tests
required: true
default: true
type: Boolean
uploadArtifacts:
description: upload artifacts
required: true
default: false
type: Boolean
push:
branches:
- master
- main
- release/*
jobs:
Build_Job:
runs-on: [self-hosted, raya]
steps:
- name: Publish drop artifact
if: ${{ inputs.uploadArtifacts }}
uses: actions/upload-artifact#v2
with:
name: Installer
path: "${{ runner.temp }}/AppxPackages/"
It's the expected behavior, as the inputs will be set only if the workflow_dispacth event is used to trigger the workflow.
If you want the workflow to perform a default operation when the code is pushed, you would need to implement the if condition differently.
Example:
on:
push:
workflow_dispatch:
inputs:
test1:
description: test1
required: false
default: false
type: boolean
test2:
description: test2
required: false
default: true
type: boolean
jobs:
job1: # will always run
runs-on: ubuntu-latest
steps:
- run: |
echo ${{ inputs.test1 }}
echo ${{ inputs.test2 }}
echo ${{ github.event_name }}
job2: # will only run on a workflow_dispatch event, if test1 input is true
runs-on: ubuntu-latest
if: ${{ inputs.test1 }}
steps:
- run: |
echo ${{ inputs.test1 }}
echo ${{ inputs.test2 }}
echo ${{ github.event_name }}
job3: # will only run on a workflow_dispatch event, if test2 input is true
runs-on: ubuntu-latest
if: ${{ inputs.test2 }}
steps:
- run: |
echo ${{ inputs.test1 }}
echo ${{ inputs.test2 }}
echo ${{ github.event_name }}
job4: # will only run on a push event
runs-on: ubuntu-latest
if: ${{ github.event_name == 'push' }}
steps:
- run: |
echo ${{ inputs.test1 }}
echo ${{ inputs.test2 }}
echo ${{ github.event_name }}
job5: # will only run on a push event OR if inputs.test2 is true on a workflow_dispatch event
runs-on: ubuntu-latest
if: ${{ github.event_name == 'push' || inputs.test2 }}
steps:
- run: |
echo ${{ inputs.test1 }}
echo ${{ inputs.test2 }}
echo ${{ github.event_name }}
I understand that what you want to achieve is something similar to the job5 example above (you could even add a github.ref context variable to the expression if you only want a job to be executed if the branch name is something specific).
I made some tests if you want to have a look:
workflow file
workflow run (push event)
workflow run (workflow_dispatch event with default value)
Related
I'm currently trying to allow tags only on the main branch. But I can't seem to get it to work. This is what I currently have but it also triggers the production build if a tag is being put on a develop/release/hotfix branch.. Is there a way to check if the tag has been created on the main branch?
Current YAML:
name: Release to production
on:
push:
branches:
- 'hotfix/**'
tags:
- 'v*'
jobs:
get-version-data:
runs-on: ubuntu-latest
steps:
- name: Get production version from tag
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
id: get_version
run: echo "VERSION=$(echo ${{ github.ref }} | cut -d '/' -f 3 | cut -c2-)" >> $GITHUB_ENV
- name: Get hotfix version from tag
if: ${{ contains(github.ref, 'hotfix') }}
id: split
uses: jungwinter/split#v2
with:
separator: '/'
msg: ${{ github.ref }}
- name: Write hotfix version to env
if: ${{ contains(github.ref, 'hotfix') }}
run: echo "VERSION=${{ steps.split.outputs._3 }}" >> $GITHUB_ENV
- name: Exit if version could not be determined
if: ${{ env.VERSION == '' }}
run: exit -1
- name: Build Number
run: echo "BUILD_NUMBER=$(($GITHUB_RUN_NUMBER + 125))" >> $GITHUB_ENV
outputs:
version: ${{ env.VERSION }}
build-number: ${{ env.BUILD_NUMBER }}
So this is a weird one... I am trying to implement a CODEFREEZE option in release pipelines so I can implement a global freeze to any release with an organization secret:
name: test code freeze
on:
push:
jobs:
test:
runs-on: ubuntu-latest
env:
CODEFREEZE: ${{ secrets.CODEFREEZE }}
steps:
- name: test
if: ${{ env.CODEFREEZE }} == "true"
run: echo "code is frozen"
- name: test unfreeze
if: ${{ env.CODEFREEZE }} == "false"
run: echo "code is NOT frozen"
For some reason, both of these run. I've tried setting the secret to a number of different values. I've tried using quotes and not using quotes, but nothing I do seems to have an effect. Am I missing something extremely obvious?
This does appear to work but I don't understand why:
name: test code freeze
on:
push:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: test
env:
CODEFREEZE: ${{ secrets.CODEFREEZE }}
if: ${{ env.CODEFREEZE == 'true' }}
run: echo "code is frozen"
- name: test unfreeze
env:
CODEFREEZE: ${{ secrets.CODEFREEZE }}
if: ${{ env.CODEFREEZE == 'false' }}
run: echo "code is NOT frozen"
This also appears to work:
name: test code freeze
on:
push:
env:
CODEFREEZE: ${{ secrets.CODEFREEZE }}
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: test
if: ${{ env.CODEFREEZE == 'true' }}
run: echo "code is frozen"
- name: test unfreeze
if: ${{ env.CODEFREEZE == 'false' }}
run: echo "code is NOT frozen"
so the problem only exists when you set the env on the job level
Nevermind... I clearly have not had enough coffee... I found the syntax error:
name: test code freeze
on:
push:
jobs:
test:
runs-on: ubuntu-latest
env:
CODEFREEZE: ${{ secrets.CODEFREEZE }}
steps:
- name: test
if: ${{ env.CODEFREEZE == 'true' }}
run: echo "code is frozen"
- name: test unfreeze
if: ${{ env.CODEFREEZE == 'false' }}
run: echo "code is NOT frozen"
It requires single quotes in the evaluation and the eval has to be inside the curly braces...
I have a workflow which I want to run as a schedule job. But its also run based on another workflow is success or not. As now its not running as schedule but it works if the other workflow is success.
My workflow:
name: Security
on:
workflow_run:
workflows: ["My other workflow"]
types:
- completed
schedule:
- cron: '0 3 * * *'
env:
IMAGE: ghcr.io/${{ github.repository }}:${{ github.sha }}
GITHUB_USERNAME: x-access-token
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
jobs:
app-dependencies-vulnerabilities:
name: Scan for vulnerability
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Running snyk
uses: snyk/actions/gradle#master
with:
command: monitor
args: --org=myorg --project-name=${{ github.repository }} --remote-repo-url=https://github.com/${{ github.repository }}.git
json: true
I think this workflow is not running as excepted schedule job because I have this conditon? if: github.event.workflow_run.conclusion == 'success', am I right? How can I make it run as schedule AND based on the other workflow is success or not?
Thank you!
Your assumption is correct. To check if your workflow is being triggered via a cron job (scheduled event), you may do:
if: ${{ github.event_name == 'schedule' }}
In your case, your if condition should look:
if: ${{ (github.event.workflow_run.conclusion == 'success') || (github.event_name == 'schedule') }}
I am trying to figure out how to combine manual trigger and other trigers (push for example) in the same workflow
This is my manual action
on:
workflow_dispatch:
inputs:
environment:
type: environment
default: DEV
required: true
env:
ENVIRONMENT: ${{ github.event.inputs.environment }}
.
.
.
I want something like
on:
push:
branches:
- main
- dev
workflow_dispatch:
inputs:
environment:
type: environment
default: DEV
required: true
env:
ENVIRONMENT: ${{ github.event.inputs.environment }} or {{ DEV if dev }} or {{ PROD if main }}
.
.
.
Here's one way to do it:
name: Print environment variable
on:
push:
branches:
- master
- development
workflow_dispatch:
inputs:
environment:
type: string
default: DEV
required: true
jobs:
prod:
if: ${{ github.event_name == 'push' && github.ref_name == 'master' || github.event.inputs.environment == 'PROD' }}
env:
environment: PROD
runs-on: ubuntu-latest
steps:
- name: Print value
run: echo ${{ env.environment }}
dev:
if: ${{ github.event_name == 'push' && github.ref_name == 'development' || github.event.inputs.environment == 'DEV' }}
env:
environment: DEV
runs-on: ubuntu-latest
steps:
- name: Print value
run: echo ${{ env.environment }}
Of course, if you have the same steps for both environments and do not wish to repeat them then consider using reusable workflows.
UPDATE:
Just to clarify why I used type string for environment. Yes, I've noticed that you used environment as type for input, but the docs are not quite clear on that part. Here it says that inputs can only be of type boolean, number or string, yet here in the example it shows not only environment type, but also choice type.
according to docs, you can setup an env variable with:
echo "{environment_variable_name}={value}" >> $GITHUB_ENV
so in your case something like this should work (did not test):
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Set env
run: |-
echo "ENVIRONMENT=$(
if ${{ github.event.inputs.environment }}; then
echo ${{ github.event.inputs.environment }}
elif [ ${{ github.ref_name }} == dev]; then
echo DEV
elif [ ${{ github.ref_name }} == main]; then
echo PROD
fi
)" >> $GITHUB_ENV
- name: Test env
run: echo "woo!!" ${{ env.ENVIRONMENT }}
So I have the following workflow and its working perfectly. I now want to enhance it and when I am doing a PR to master, I want to set NETLIFY_DEPLOY_TO_PROD: false instead of it being true? Do I have to duplicate this all in a new workflow, or could do some inline if check of github.event_name === push ? true : false
name: 'Netlify Deploy'
on:
push:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: jsmrcaga/action-netlify-deploy#master
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.SITE_ID }}
NETLIFY_DEPLOY_MESSAGE: "${{ github.event.head_commit.message }}"
NETLIFY_DEPLOY_TO_PROD: true
You could set an environment variable to indicate if deploy to prod should happen, and change it depending on the event name:
name: Netlify Deploy
on:
push:
branches:
- master
pull_request:
branches:
- master
env:
DEPLOY: false
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Deploy on pushes
if: github.event_name == 'push'
run: echo 'DEPLOY=true' >> "$GITHUB_ENV"
- uses: jsmrcaga/action-netlify-deploy#master
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.SITE_ID }}
NETLIFY_DEPLOY_MESSAGE: ${{ github.event.head_commit.message }}
NETLIFY_DEPLOY_TO_PROD: ${{ env.DEPLOY }}
You want to use github action expressions for this as it's quicker and you don't need any other unnecessary steps. I would only use steps to run scripts when they are more complex in nature
Reference: https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions
Example 1: Trigger on push
- uses: jsmrcaga/action-netlify-deploy#master
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.SITE_ID }}
NETLIFY_DEPLOY_MESSAGE: "${{ github.event.head_commit.message }}"
NETLIFY_DEPLOY_TO_PROD: ${{ github.event_name == 'push' }}
Example 2 & Solution: Trigger on push and branch is master
NOTE: You only need to check for branch master if you are planning to let this workflow run on other branches. Otherwise you can just use example 1 above that sets variable to true if event name is push only.
- uses: jsmrcaga/action-netlify-deploy#master
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.SITE_ID }}
NETLIFY_DEPLOY_MESSAGE: "${{ github.event.head_commit.message }}"
NETLIFY_DEPLOY_TO_PROD: ${{ github.event_name == 'push' && contains(github.ref, 'master') }}