Conditional value in github workflow - github-actions

My current github workflow as quite repeating code to allow a conditionnement in what and where releases should be pushed depending on the event type :
name: Build LaTeX document & latexdiff to previous tagged version.
on:
push:
branches: [master]
pull_request:
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Build stuff
...
...
- name: Upload results (master latest)
uses: "marvinpinto/action-automatic-releases#latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: "latest"
prerelease: true
draft: true
title: "Build"
files: |
result
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
- name: Upload results (pull request)
uses: "marvinpinto/action-automatic-releases#latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: github.ref_name
prerelease: true
draft: true
title: "Build"
files: |
result
if: ${{ github.event_name == 'pull_request' }}
- name: Upload results (tag)
uses: "marvinpinto/action-automatic-releases#latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: github.ref_name
prerelease: false
draft: false
title: "Build"
files: |
result
if: ${{ github.ref_type == 'tag' }}
Is there a way to conditontionally set values so that i dont have to repeat 3 times the same thing ? Basically i want to deal with the three different cases :
Some commit lands on master, I releas it with the latest tag.
Some tag lands on master, I release it with its propper tag.
PRs are released under their names.
Not that it changes anything, but it really looks ugly to me.

You could set the params as an output of a step and then use them in the upload step:
name: Build LaTeX document & latexdiff to previous tagged version.
on:
push:
branches: [master]
pull_request:
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Build stuff
...
- name: Set params
id: params
run: |
if [[ "${{ github.event_name }}" -eq "push" && "${{ github.ref }}" -eq "refs/heads/master" ]]; then
echo "tag=latest" >> $GITHUB_OUTPUT
echo "prerelease=true" >> $GITHUB_OUTPUT
echo "draft=true" >> $GITHUB_OUTPUT
elif [[ "${{ github.event_name }}" -eq "pull_request" ]]; then
echo "tag=${{ github.ref_name }}" >> $GITHUB_OUTPUT
echo "prerelease=true" >> $GITHUB_OUTPUT
echo "draft=true" >> $GITHUB_OUTPUT
elif [[ "${{ github.ref_type }}" -eq "tag" ]]; then
echo "tag=${{ github.ref_name }}" >> $GITHUB_OUTPUT
echo "prerelease=false" >> $GITHUB_OUTPUT
echo "draft=false" >> $GITHUB_OUTPUT
fi
- name: Upload results
uses: "marvinpinto/action-automatic-releases#latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: ${{ steps.params.outputs.tag }}
prerelease: ${{ steps.params.outputs.prerelease }}
draft: ${{ steps.params.outputs.draft }}
title: "Build"
files: |
result
Is it better then having 3 types of the step? I don't know. I have my doubts.
Notes:
There is no default. In case it's possible that there is an additional GitHub event that doesn't match these cases, then either set it in the additional else Bash condition or introduce an if condition to the upload step to make sure that it runs only in one of the three scenarios.
I haven't tested the code and there might be some bug. However, I believe you understand the idea of setting the params.

Related

GitHub actions tag

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 }}

How to get inputs in github yml

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)

github action combine workflow_dispatch and push in the same workflow

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 }}

boolean env var in Github actions

I have a boolean env var TAG_EVENT and I update it in one of the steps to false (I also print it and I see it false) but for some reason, the last step is not executed although TAG_EVENT is false. I appreciate help with that,
on:
workflow_dispatch:
env:
TAG_EVENT: ${{ true }}
jobs:
push_images:
name: Push images
runs-on: ubuntu-latest
if: ${{ github.event_name != 'pull_request' }}
steps:
- id: version
name: Infer version
run: |
version="${GITHUB_REF#refs/tags/v}"
echo $version
if [[ $version == refs/* ]] ;
then
echo 'TAG_EVENT=false' >> $GITHUB_ENV
branch="${GITHUB_REF#refs/heads/}"
version=$branch
fi
echo ::set-output name=version::$version
- name: Publish latest image tag for release
if: github.event_name != 'pull_request' && TAG_EVENT == false
run: |
echo "printme!!!"
As you make use of a text-based shell like bash, you will have to deal with strings. Therefore you wont be able to do a check for false, but need to do it for 'false'. But I would suggest marshaling the boolean by using GitHub's built-in toJSON and fromJSON functions:
...
steps:
- name: I produce a boolean output
id: output_producer
shell: bash
run: |
if [[ $RANDOM > 100 ]]; then i=true; else i=false; fi
echo "::set-output name=boolean_output::${{ toJSON($i) }}"
echo "::set-output name=integer_output::${{ toJSON($i) }}"
- name: I run conditionally on that boolean
if: fromJSON(steps.output_producer.outputs.boolean_output)
shell: bash
run: |
echo "Ran successfully!"
- name: I run always
if: always()
shell: bash
run: |
echo ${{ fromJSON(steps.output_producer.outputs.integer_output) }}
There is some inconsistency between the input context and the way booleans are treated in GitHub Actions. I have a short write-up on this. Hope you find this helpful
GitHub Actions: Passing Boolean input variables to reusable workflow_call
Try to use in second if-statement ${{ env.TAG_EVENT == false }}

GitHub Action In-line if

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') }}