GitHub Action not evaluating correctly - github-actions

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

Related

Github action actions/create-release#v1 newTag based on step from another job

I have a github action yml file that i'm having issues in 2 areas first is the newTag is no longer working when I broke everything out into separate job names.
These lines no longer work. I get release-v instead of the actual bumped version number.
tag_name: ${{env.TAG_PREXIX}}${{ steps.bumpVersion.outputs.newTag }}
release_name: ${{env.TAG_PREXIX}}${{ steps.bumpVersion.outputs.newTag }}
Second Issue: The if statement for docker build
if: github.ref == 'ref/head/release' || contains(github.ref, '/tags/release')
The above if does not work. If I add a branch called release/test or using a tag name release it does not run
name: Publish
on:
push:
branches:
- main
- release/*
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}-image
TAG_PREXIX: release-v
jobs:
Publish:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout#v3
- name: "Bump package version"
id: bumpVersion
uses: "phips28/gh-action-bump-version#master"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PACKAGEJSON_DIR: "./client"
tag-prefix: ${{env.TAG_PREXIX}}
major-wording: "MAJOR,BREAKING CHANGE:"
minor-wording: "feat"
patch-wording: "patch,fix,bugfix,chore"
Build-Docker-Image:
runs-on: ubuntu-latest
needs: Publish
if: github.ref == 'ref/head/release' || contains(github.ref, '/tags/release')
steps:
- name: Log into Container registry ${{ env.REGISTRY }}
uses: docker/login-action#v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action#v3
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{env.TAG_PREXIX}}${{steps.bumpVersion.outputs.newTag}}
Release:
runs-on: ubuntu-latest
needs: Publish
steps:
- name: Create Release
uses: actions/create-release#v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{env.TAG_PREXIX}}${{ steps.bumpVersion.outputs.newTag }}
release_name: ${{env.TAG_PREXIX}}${{ steps.bumpVersion.outputs.newTag }}

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 actions failed last step on result of next step

I am preety sure its not possible but still wanna try my luck. I have deploy-dev step and smoke-test-dev. So if smoke test for dev fails then I want to fail the last step which is deploy-dev.
deploy-dev:
if: ${{ github.event.workflow_run.conclusion == 'skipped' && github.actor == 'dependabot[bot]' }}
runs-on: ubuntu-latest
needs: [lint, test]
steps:
- uses: actions/checkout#v2
- name: Start deployment
uses: bobheadxi/deployments#v0.4.3
id: deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: dev
- name: Update deployment status
uses: bobheadxi/deployments#v0.4.3
if: always()
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
smoke-tests-dev:
if: ${{ github.event.workflow_run.conclusion == 'skipped' && github.actor == 'dependabot[bot]' }}
runs-on: ubuntu-latest
needs: deploy-dev
steps:
- uses: actions/checkout#v2
- name: Checkout GitHub Action Repo
uses: actions/checkout#v2
with:
repository: my/github-actions
ref: ${{ env.COMMON_ACTIONS_VERSION }}
token: ${{ secrets.REPO_READ_TOKEN }}
path: ${{ env.COMMON_ACTIONS_PATH }}
- name: install and smoke test
uses: ./.github/common/actions/yarn
with:
npm_token: ${{ env.NPM_TOKEN }}
env: dev
action: test:smoke

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

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