If condition not executing in github actions - yaml

I want to run npm command based on environment. but it always execute if true section only.
workflow_dispatch:
inputs:
typeOfTesting:
type: choice
description: Select Type of Test
default: "stage-test-Uptime"
required: true
options:
- stage-test-Uptime
- prod-test-Uptime
schedule:
- cron: "30 3 * * *"
- cron: "30 13 * * *"
steps:
- name: Selection of Testing type
run: |
echo "Branch Name: ${{ github.event.inputs.typeOfTesting }}"
- name: Install
run: npm install
- name: Test
run: |
if [ ${{ github.event.inputs.typeOfTesting == '' || github.event.inputs.typeOfTesting == 'stage-test-Uptime' }} ]; then
echo "action_state=https://hooks.slack.com/services/XXX/XXX/XXX" >> $GITHUB_ENV
else
echo "action_state=https://hooks.slack.com/services/YYY/YYY/YYY" >> $GITHUB_ENV
fi
npm run ${{ github.event.inputs.typeOfTesting == '' && github.event.inputs.typeOfTesting || 'stage-test-Uptime' }}
here it always execute true part of if condition.

try to resolve only github variables instead of the whole conditions, like:
if [ ${{ github.event.inputs.typeOfTesting }} == '' || ${{ github.event.inputs.typeOfTesting}} == 'stage-test-Uptime' ]; then
echo "action_state=https://hooks.slack.com/services/XXX/XXX/XXX" >> $GITHUB_ENV
else
echo "action_state=https://hooks.slack.com/services/YYY/YYY/YYY" >> $GITHUB_ENV
fi

Related

Github composite action output can not be set from within a bash script

For one of my projects, I am setting an action output from within a bash script that is executed inside a composite action. I found that GitHub has excellent documentation on how to create a GitHub composite action output. It states that this can be done using the following action.yml file.
name: 'Hello World'
description: 'Greet someone'
inputs:
who-to-greet: # id of input
description: 'Who to greet'
required: true
default: 'World'
outputs:
random-number:
description: "Random number"
value: ${{ steps.random-number-generator.outputs.random-number }}
runs:
using: "composite"
steps:
- run: echo Hello ${{ inputs.who-to-greet }}.
shell: bash
- id: random-number-generator
run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
shell: bash
- run: echo "${{ github.action_path }}" >> $GITHUB_PATH
shell: bash
- run: goodbye.sh
shell: bash
I checked the results using the following action workflow, and it works.
on: [push]
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- uses: actions/checkout#v3
- id: foo
uses: actions/hello-world-composite-action#v1
with:
who-to-greet: 'Mona the Octocat'
- run: echo random-number ${{ steps.foo.outputs.random-number }}
shell: bash
My use case, however, differs from the example above in that I have to set the output variable inside the goodbye.sh script. According to the documentation, this should be done using the GITHUB_OUTPUT variable:
echo "{name}={value}" >> $GITHUB_OUTPUT
After some testing, this method is not working for composite actions. As this could also be a bug or not supported, I created a bug report at https://github.com/orgs/community/discussions/47775. However, I quickly wanted to double-check if there may be something wrong with my syntax.
Steps to reproduce
Fork this repository.
Enable GitHub actions on the fork.
Push a commit to your fork.
See that only the random-number variable is set while the random-number-bash` is set (See this example workflow).
I found my issue using #benjamin-w's comment. The problem was that the goodbye.sh step should contain an id key for the created output to be referenced correctly. The correct syntax should be:
action.yml
name: 'Hello World'
description: 'Greet someone'
inputs:
who-to-greet: # id of input
description: 'Who to greet'
required: true
default: 'World'
outputs:
random-number:
description: "Random number"
value: ${{ steps.random-number-generator.outputs.random-number }}
random-number-bash:
description: "Random number bash"
value: ${{ steps.random-number-generator-bash.outputs.random-number-bash }}
runs:
using: "composite"
steps:
- run: echo Hello ${{ inputs.who-to-greet }}.
shell: bash
- id: random-number-generator
run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
shell: bash
- run: echo "${{ github.action_path }}" >> $GITHUB_PATH
shell: bash
- run: goodbye.sh
id: random-number-generator-bash
shell: bash
And the correct syntax for creating the output in the goodbye.sh script should be:
Goodbye.sh
echo "Goodbye"
echo "random-number-bash=$(echo 123)" >> $GITHUB_OUTPUT
Which then can be tested using the following workflow file:
Test workflow
on: [push]
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- uses: actions/checkout#v3
- id: foo
uses: rickstaa/hello-world-composite-action-output-bug#main
with:
who-to-greet: 'Mona the Octocat'
- run: echo random-number ${{ steps.foo.outputs.random-number }}
shell: bash
- run: echo random-number ${{ steps.foo.outputs.random-number-bash }}
shell: bash

Unable to read Github Actions job's outputs from another job

I have the following workflow in Github Actions, where I have a job that create some outputs and a dependant job that read those outputs, pretty similar to the example from the docs:
name: Sandbox
on:
push:
env:
POSTGRESQL_VERSION: "14.4.0-debian-11-r13"
jobs:
setup:
runs-on: ubuntu-latest
outputs:
prod_tag: "steps.prod_tag.outputs.prod_tag"
postgresql_version: "steps.postgresql_version.outputs.postgresql_version"
steps:
- uses: actions/checkout#v3
- id: prod_tag
run: |
if [[ ${{ github.ref_type }} == "tag" ]]; then
echo "::set-output name=prod_tag::${{github.ref_name}}"
else
echo "::set-output name=prod_tag::latest"
fi;
- id: postgresql_version
run: echo "::set-output name=postgresql_version::${POSTGRESQL_VERSION}"
- name: Show output variables
run: |
echo "PROD TAG: ${{steps.prod_tag.outputs.prod_tag}}"
echo "POSTGRESQL_VERSION: ${{steps.postgresql_version.outputs.postgresql_version}}"
show_outputs:
runs-on: ubuntu-latest
needs: setup
steps:
- run: |
echo "PROD TAG: ${{needs.setup.outputs.prod_tag}}"
echo "POSTGRESQL_VERSION: ${{needs.setup.outputs.postgresql_version}}"
However, in my example, it doesn't work as expected and show_outputs shows PROD TAG: steps.prod_tag.outputs.prod_tag and POSTGRESQL_VERSION: steps.postgresql_version.outputs.postgresql_versioninstead of the values set in the setup job, that should be latest and 14.4.0-debian-11-r13. In the step Show output variables of the setup job I can see that the values are properly set, and I've tried several different approaches (setting the variables from the same step, not taking the value from the environment variable, etc) but with no success.
Any idea what can be wrong with my example?
You should surround the variables with ${{ and }}
try with:
outputs:
prod_tag: ${{ steps.prod_tag.outputs.prod_tag }}
postgresql_version: ${{ steps.postgresql_version.outputs.postgresql_version }}
instead of:
outputs:
prod_tag: "steps.prod_tag.outputs.prod_tag"
postgresql_version: "steps.postgresql_version.outputs.postgresql_version"

GitHub Actions Job Based on Multiple Conditions from Inputs

I've been trying to create conditions for Jobs in GitHub Actions but I can't seem to get it working
I have the following Inputs:
on:
workflow_dispatch:
inputs:
env:
description: 'Select the Environment'
type: choice
required: true
options:
- SIT
- UAT
op:
description: 'Deploy or Delete Apps'
type: choice
required: true
options:
- Deploy
- Delete
ver:
description: 'Type the app version'
required: true
and the below jobs:
jobs:
create-sit-app:
runs-on: ubuntu-latest
name: 'Deploy App for SIT'
if: |
(${{ github.event.inputs.env }} == 'SIT' && ${{ github.event.inputs.op }} == 'Deploy')
steps:
........
........
........
I also tried this
(${{ github.event.inputs.env == 'SIT' }} && ${{ github.event.inputs.op == 'Deploy' }})
And this
${{ github.event.inputs.env == 'SIT' }} && ${{ github.event.inputs.op == 'Deploy' }}
Managed to do it like this:
if: (github.event.inputs.env == 'SIT' && github.event.inputs.op == 'Deploy')

trying to setup variables using from predefined variables from bash script in Azure pipelines

I am trying to setup variables from predefined variables through bash script using bash script. but could not get succedeed. below is my task in azure pipeline
resources:
pipelines:
- pipeline: pipeline1
project: appcom
source: pipeline-api
trigger:
branches:
- develop
- feat/*
- pipeline: pipeline2
project: appcom
source: pipeline2-api
trigger:
branches:
- develop
- feat/*
variables:
- name: alias
value: $(resources.triggeringAlias)
stages:
- stage: ScanImage
jobs:
- job: ScanImage
pool:
vmImage: 'ubuntu-16.04'
steps:
- script: echo $(alias)
- task: Bash#3
inputs:
targetType: 'inline'
script: |
if [ "$(alias)" == "pipeline1" ]; then
echo ("##vso[task.setvariable variable=apiname]$(resources.pipeline.pipeline1.pipelineName)")
echo ("##vso[task.setvariable variable=dockertag]$(resources.pipeline.pipeline1.sourceCommit) | cut -c -7")
echo ("##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline1-api")
elif [ "$(alias)" == "pipeline2" ]; then
echo ("##vso[task.setvariable variable=apiname]$(resources.pipeline.pipeline2.pipelineName)")
echo ("##vso[task.setvariable variable=dockertag]$(resources.pipeline.pipeline2.sourceCommit) | cut -c -7")
echo ("##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline2")
fi
- script: echo $(dockertag)
- script: echo $(helmpath)
- script: echo $(apiname)
it giving me error with ##[error]Bash exited with code '2
By reference to this doc: Set variables in scripts, below yaml should work as expected.
resources:
pipelines:
- pipeline: pipeline1
project: appcom
source: pipeline-api
trigger:
branches:
- develop
- feat/*
- pipeline: pipeline2
project: appcom
source: pipeline2-api
trigger:
branches:
- develop
- feat/*
variables:
- name: alias
value: $(resources.triggeringAlias)
stages:
- stage: ScanImage
jobs:
- job: ScanImage
pool:
vmImage: 'ubuntu-16.04'
steps:
- script: echo $(alias)
- task: Bash#3
inputs:
targetType: 'inline'
script: |
if [ "$(alias)" == "pipeline1" ]; then
echo "##vso[task.setvariable variable=apiname]$(resources.pipeline.pipeline1.pipelineName)"
echo "##vso[task.setvariable variable=dockertag]$(resources.pipeline.pipeline1.sourceCommit) | cut -c -7"
echo "##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline1-api"
elif [ "$(alias)" = "pipeline2" ]; then
echo "##vso[task.setvariable variable=apiname]$(resources.pipeline.pipeline2.pipelineName)"
echo "##vso[task.setvariable variable=dockertag]$(resources.pipeline.pipeline2.sourceCommit) | cut -c -7"
echo "##vso[task.setvariable variable=helmpath]P02565Mallorca/pipeline2-api"
fi
- script: echo $(dockertag)
- script: echo $(helmpath)
- script: echo $(apiname)
See:Resources: pipelines for more details.

Pass variable build args to docker build command

Here is the docker stage:
- template: ../docker-template.yml
parameters:
buildArgs:
- name: Arg1
value: $(arg1-value)
- name: Arg2
value: $(arg2-value)
docker-template.yml
parameters:
buildArgs: []
stages:
- stage: Docker
displayName: Docker Stage
jobs:
- job: Docker
steps:
# - ${{ each arg in parameters.buildArgs }}:
# - bash: |
# buildArgString+=' --build-arg ${{arg.name}}=${{arg.value}}'
# displayName: "Getting Params"
- bash: |
${{ each arg in parameters.buildArgs }}:
buildArgString+=' --build-arg ${{arg.name}}=${{arg.value}}'
echo 'buildstring=$buildArgString'
displayName: 'build string'
- bash: |
cd $(sourceDirectory)
docker build \
-t $(registryName)/$(imageName):$(imageTag) \
$buildArgString \
.
failOnStderr: true
displayName: 'docker build'
Here I want to construct buildArgString based on the parameters passed and pass it to docker build command as shown. When I use this I get error
The directive 'each' is not allowed in this context. Directives are not supported for expressions that are embedded within a string. Directives are only supported when the entire value is an expression.
Any suggestions?
As the message mentioned, you are using each in a wrong place. Try with below script sample:
azure-pipelines.yml:
extends:
template: docker-template.yml
parameters:
buildArgs:
Arg1 : $(arg1-value)
Arg2 : $(arg2-value)
docker-template.yml:
parameters:
- name: buildArgs
type: object
default: []
stages:
- stage: Docker
displayName: Docker Stage
jobs:
- job: Docker
steps:
- ${{ each arg in parameters.buildArgs }}:
- bash: |
echo ${{ arg.key }}
echo ${{ arg.value }}
echo "##vso[task.setvariable variable=buildArgStringOther]--build-arg ${{ arg.key }}=${{ arg.value }}"
displayName: ${{ arg.key }}
- bash: |
echo "buildstring=$(buildArgStringOther)"
displayName: ECHO-${{ arg.key }}

Resources