I am trying to set a simple environment variable based on the branch name, but I cannot get it to work. Based on the logs, the env var seems to be set correctly, but it is still empty afterwards:
TF_VAR_project_name=staging >> /home/runner/work/_temp/_runner_file_commands/set_env_51dc1c8d-60b9-47bd-b91e-f315cd9349a7
--
name: "Terraform"
on:
push:
branches:
- main
- staging
pull_request:
jobs:
terraform:
name: "Terraform"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Get branch names
id: branch-name
uses: tj-actions/branch-names#v4
- name: For PR, get base ref branch name
if: github.event_name == 'pull_request'
run: |
echo "TF_VAR_project_name=${{ steps.branch-name.outputs.base_ref_branch }} >> $GITHUB_ENV"
- name: For push, get current branch name
if: github.event_name == 'push'
run: |
echo "TF_VAR_project_name=${{ steps.branch-name.outputs.current_branch }} >> $GITHUB_ENV"
- name: Test1
run: |
echo "$TF_VAR_project_name"
- name: Set is prod based on the branch
run: |
echo "TF_VAR_is_prod=$(( $TF_VAR_project_name == prod ? true : false ))" >> $GITHUB_ENV
Complete Log:
Thanks!
There are two syntax problems with your workflow:
FIRST:
The syntax used to set variables has " incorrectly used:
run: |
echo "TF_VAR_project_name=${{ steps.branch-name.outputs.base_ref_branch }} >> $GITHUB_ENV"
Should be:
run: |
echo "TF_VAR_project_name=${{ steps.branch-name.outputs.base_ref_branch }}" >> $GITHUB_ENV
Before the >> $GITHUB_ENV (documentation reference)
SECOND:
The syntax used at the last step uses $(( ... )) instead of ${{ .. }}:
- name: Set is prod based on the branch
run: |
echo "TF_VAR_is_prod=$(( $TF_VAR_project_name == prod ? true : false ))" >> $GITHUB_ENV
Should be:
- name: Set is prod based on the branch
run: |
echo "TF_VAR_is_prod=${{ $TF_VAR_project_name == prod ? true : false }}" >> $GITHUB_ENV
As you used on other steps.
Related
Q1: I have a single job in ADO YAML pipeline which have 4-5 tasks under steps. I want to trigger those tasks based on few conditions so I've used ADO Expressions. But now i want to skip the taskB when taskA fails. By default it should happen but seems like my task condition is overriding the continueonerror:false
Can you suggest how to do this ?
tried alternative workaround: Multi Job Pipeline setup with 1 task in 1 job & it works fine for us, but want to know how we can setup for multiple tasks in a single job ?
Q2 : Even if i can get taskA status as a variable using REST API & set variable concepts, how can i add multiple conditions in task.something like " variable1=succeeded & variable2 = p1 or variable3 = p2 "
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops
Sample yaml
parameters:
variables:
trigger:
steps:
task: A
continueonerror:false
condition: or(eq(v1, p3), eq(v2, 3), eq(v4, 5))
task: B
continueonerror:false
condition: or(eq(v1, p1), eq(v2, 6))
task: C
continueonerror:false
condition: or(eq(v1, p2), eq(v2, 8))
According to your expectation to skip TaskB if TaskA fails, you may consider adding succeeded() as part of the condition in TaskB together with your other evaluations. Kindly take the following sample YAML for a reference.
trigger: none
parameters:
- name: p1
default: 1
- name: p2
default: 2
- name: p3
default: 3
variables:
v1: 2
v2: 6
v3: 2
v4: 5
steps:
- script: |
echo A
echo "##vso[task.logissue type=error]Simulate task failure."
exit 1
name: TaskA
# continueOnError: true
condition: or(eq(variables['v1'], ${{ parameters.p3 }}), eq(variables['v2'], 3), eq(variables['v4'], 5))
- script: echo B
name: TaskB
condition: and(succeeded(), or(eq(variables['v1'], ${{ parameters.p1 }}), eq(variables['v2'], 6)))
- script: echo C
name: TaskC
condition: and(succeededOrFailed(), or(eq(variables['v1'], ${{ parameters.p2 }}), eq(${{ parameters.p2 }}, 8)))
When TaskA failed the condition evaluation of succeeded() for TaskB would be False; hence TaskB was skipped.
TaskC uses succeededOrFailed() in its condition and will continue to run, even if a previous dependency has failed, unless the run was canceled.
See more information on Specify conditions.
Hi Please note that the if condition gets executed in the below case even though the variable "highPriority" is zero.
- name: Polaris Result
id: polaris_result
run: |
highPriority=$(cat sample.json | jq '.issueSummary.high')
echo "$highPriority"
echo "::set-output name=highPriority::$highPriority"
- name: fail If crosses threshold for high priority issues
if: ${{ steps.polaris_result.outputs.highPriority }} > 0
run: exit 1
note that echo "$highPriority" outputs "0". However the if condition gets executed in this case which should not be
You should surround all the expression with the { } bracet.
please try:
if: ${{ steps.polaris_result.outputs.highPriority > 0 }}
instead of:
if: ${{ steps.polaris_result.outputs.highPriority }} > 0
See expression section in the doc for some context
*please ignore formatting issue
trigger:
- none
parameters:
- name: stg
displayName: "Environment Names"
type: string
pool:
vmImage: ubuntu-latest
stages:
- stage: stage1
displayName: 'Stage C1'
jobs:
- job: jobabc
strategy:
maxParallel: 2
matrix:
env_1:
EnvironmentX: E1
Browser: IE
env_2:
EnvironmentX: E2
Browser: Firefox
env_3:
EnvironmentX: E3
Browser: Chrome
displayName: 'Job 1'
condition: contains('${{parameters.stg}}', $(EnvironmentX))}}
steps:
- task: CmdLine#2
displayName: 'My Task '
inputs:
script: 'echo Stage C1 $(Browser)'
I pass stg parameter as E1E3E4.
In Matrix I have defined EnvironmentX whose value can be E1, E2, or E3.
Below code runs Job 3 times with matrix values defined.
I do NOT want to run all 3 jobs but stg parameter value should decide JOb would run for which matrix values.
Job Condition is NOT working? How to fix this please
You could build the matrix conditionally. Not ideal but that will work:
trigger:
- none
parameters:
- name: stg
displayName: Environment Names
type: string
pool:
vmImage: ubuntu-latest
stages:
- stage: stage1
displayName: 'Stage C1'
jobs:
- job: jobabc
strategy:
maxParallel: 2
matrix:
${{ if contains(parameters.stg, 'E1') }}:
env_1:
EnvironmentX: E1
Browser: IE
${{ if contains(parameters.stg, 'E2') }}:
env_2:
EnvironmentX: E2
Browser: Firefox
${{ if contains(parameters.stg, 'E3') }}:
env_3:
EnvironmentX: E3
Browser: Chrome
displayName: 'Job 1'
steps:
- task: CmdLine#2
displayName: 'My Task '
inputs:
script: 'echo Stage C1 $(Browser)'
I am trying to run a conditional check before running the run command.
And the docker runs on ubuntu.
runs-on: ubuntu-latest
- name: 'Running checks with test'
if: ${{grep -c 'ApexClass' package/package.xml > 0}}
run: |
echo "Found ApexClass"
- name: 'Running checks without test'
if: ${{grep -c 'ApexClass' package/package.xml < 0}}
run: |
echo "No Apex Found"
And throws an error :
The workflow is not valid. .github/workflows/preDeployCheckQA.yml (Line: 32, Col: 13): Unrecognized named-value: 'grep'. Located at position 1 within expression: grep -c 'ApexClass' package/package.xml > 0 .github/workflows/preDeployCheckQA.yml (Line: 36, Col: 13): Unrecognized named-value: 'grep'. Located at position 1 within expression: grep -c 'ApexClass' package/package.xml < 0
How do I add a check like grep
If this is not supported, I would try and:
define a variable apexcl=$(grep -c 'ApexClass' package/package.xml)
use if in the if conditional
That is:
if: ${{ apexcl != '' }}
run...
if: ${{ apexcl == '' }}
run...
is it possible in Kubernetes values to refer to value in the same file? I'm doing a for loop and most env vars are fine but this one depends on some other val and need to duplicate/refer to it somehow inside values;
image:
repository: nginx
tag: stable
someCustomVal:
- name: x
value: xx
- name: y
value: yy
- name: z
value: {{ .Values.image.tag }}
btw above config doesn't work but looking for equivalent; I could just do the z value outside of for loop in the deployment but that wouldn't look nice so looking for alternative of referecnce
This isn't Kubernetes-specific, you can do this with YAML anchors:
$ cat example.yaml
image:
repository: nginx
tag: &imagetag stable
someCustomVal:
- name: x
value: xx
- name: y
value: yy
- name: z
value: *imagetag
$ ruby -ryaml -rpp -e'pp YAML.load_file("example.yaml")'
{"image"=>{"repository"=>"nginx", "tag"=>"stable"},
"someCustomVal"=>
[{"name"=>"x", "value"=>"xx"},
{"name"=>"y", "value"=>"yy"},
{"name"=>"z", "value"=>"stable"}]}