bitbucket pipelines variables in line - bitbucket

It is possible to declare variables inside the pipeline file, as in this GitHub example:
# ...
env:
NODE_VERSION: 16.3.1
FOLDER_PATH: Project
# ...
steps:
- name: Move to project folder
run: cd $FOLDER_PATH
# ...
Is it possible to do something similar in the bitbucket pipeline files? (How?)
Thanks any help : )

No.
There is a feature request for that https://jira.atlassian.com/browse/BCLOUD-17453 .
Still "gathering interest" though.
The nearest approximation is to write a YAML anchor that exports those vars and use it in every step.
definitions:
yaml-anchors:
- &setenv-script >-
export NODE_VERSION=16.3.1
&& export FOLDER_PATH=Project
pipelines:
default:
- step:
script:
- *setenv-script
- ...
- step:
script:
- *setenv-script
- ...

Related

CirclCI Pipeline Set a Variable within a Job and read it from Other with Condition evaluated with empty

Please I'm trying to run some steps in the CircleCI Pipeline with conditions happened in the previous step. I tried a lot of tricks like exposing the value from Step 1 to global vars and pickup it in Step 2, I can see and print the variables in Step 2 but using WHEN BLOCK forever evaluated with Empty. I searched a lot and I knew that logical conditions already evaluated before running the jobs, Please I need alternative way to execute steps in second job in case a condition happened in Step 1?
I pasted here the example that I'm trying to fix
version: 2.1
orbs:
workflows:
test-and-deploy:
jobs:
- set-data:
context: my-context
- read-data:
context: my-context
requires:
- set-data
definitions:
node_image: &node-image
docker:
- image: cimg/node:14.15.5
executors:
base-12-14-0:
description: |
Single Docker container with Node 12.14.0 and Cypress dependencies
see https://github.com/cypress-io/cypress-docker-images/tree/master/base.
Use example: `executor: cypress/base-12-14-0`.
docker:
- image: cypress/base:12.14.0
jobs:
set-data:
<<: *node-image
description: Sets the data
steps:
- run: echo "VAR=app" > global-vars
- persist_to_workspace:
root: .
paths:
- global-vars
read-data:
<<: *node-image
description: read the data
steps:
- attach_workspace:
at: .
- run: ls
- run: cat global-vars // I COULD HERE SEE THE CORRECT VAR inside global-vars
- run: cat global-vars >> $BASH_ENV
- run: echo "Test $VAR" // Successfully Printed
- when:
condition:
matches: {
pattern: "app",
value: $VAR
}
steps:
- run: echo "Condition Executed"
It's not possible to use environment variables in logic statements. The reason is that logic statements are evaluated at configuration compilation time, whereas environment variables are interpolated at run time.
The only workaround I know of is to use the CircleCI dynamic configuration functionality to set pipeline parameters' values in the "setup workflow" that you then pass to the "continuation" workflow.
And by the way, you're not using $BASH_ENV correctly (https://circleci.com/docs/env-vars#setting-an-environment-variable-in-a-shell-command). But again, even if you did, you wouldn't be able to use an environment variable in a logic statement.

Conditionally setting parameter in yml file (Azure pipeline): VAR not updating

Problem
I want to set a parameter conditionally based on which branch triggered the pipeline. If the triggered branch was feature/automated-testing, I would like to set a parameter equal to "True". See the code below.
Parts of my pipeline.yml file looks like so:
trigger:
branches:
include:
- feature/automated-testing
...
# Global variables for the pipeline
variables:
- name: "triggerRepoName"
value: "$(Build.SourceBranchName)"
stages:
# common stage. Docker build, tag and push
- stage: BuildDockerImage
displayName: "Build docker image"
variables:
...
jobs:
- template: /templates/pipelines/my-prject.yml#templates
parameters:
${{ if eq( variables.triggerRepoName, 'feature/automated-testing') }}:
runTests: "True"
${{ if ne(variables.triggerRepoName, 'feature/automated-testing') }}:
runTests: "False"
Question
When I push from branch feature/automated-testing and ´echo´ the variable runTests in the Dockerfile, it is blank. Is there something wrong with my syntax in the conditional statement?
I believe the error is in the way the variable is set conditionally, and I have therefore chosen not to supply the Dockerfile nor the other .yml template .yml used.
Please change variables.triggerRepoName to variables['triggerRepoName']. It should solve your issue.

When condition on Circleci 2.1 does not work

Recently I've made some configuration on my team's github circleci. I needed to use a when statement to devide ci logics. I referenced this document(https://circleci.com/docs/2.0/configuration-reference/#logic-statements) but it seems the document not correct.
Below is my step definition:
...
image_build_step:
executor: golang_executor
steps:
- checkout
- setup_remote_docker:
version: 18.09.3
docker_layer_caching: true
- define_svc_name:
jobname: ${CIRCLE_JOB} # On this step set $SVC variable
- when:
conditon:
equal: ["${SVC}", "SVC_A" ]
- aws-ecr/build-and-push-image:
repo: SVC_A_REPO
dockerfile: ./Dockerfile
tag: "latest,${CIRCLE_SHA1},build-${CIRCLE_BUILD_NUM}"
...
Also I already tried this.
...
image_build_step:
executor: golang_executor
steps:
- checkout
- setup_remote_docker:
version: 18.09.3
docker_layer_caching: true
- define_svc_name:
jobname: ${CIRCLE_JOB} # On this step set $SVC variable
- when:
equal: ["${SVC}", "SVC_A" ]
- aws-ecr/build-and-push-image:
repo: SVC_A_REPO
dockerfile: ./Dockerfile
tag: "latest,${CIRCLE_SHA1},build-${CIRCLE_BUILD_NUM}"
...
I cannot figure out my mistake using when statement on circleci. Additionaly, I already passed circleci config validate .circleci/config.yaml command before I pushed this commit.
What is the correct usage of when statement in circleci? Joining circleci forum is also annoying me using github account, so I leave my question on stakeoverflow.
It's not possible to use environment variables in logic statements. The reason is that logic statements are evaluated at configuration compilation time, whereas environment variables are interpolated at run time.
The only workaround I know of is to use the CircleCI dynamic configuration functionality to set pipeline parameters' values in the "setup workflow" that you then pass to the "continuation" workflow.

SonarCloud quality check doesn't work after running scan

I am trying to run sonarcloud-quality-gate check after performing sonarcloud-scan. I am doing this because I want bitbucket build pipeline should fail if the quality gate check is failed.
Doing this I get some error like this
Quality Gate failed: Could not get scanner report: [Errno 2] No such file or directory: '/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes/sonarsource/sonarcloud-scan/sonarcloud-scan.log'
This is how my bitbucket.yml looks.
image: node:10.15.3
clone:
depth: full # SonarCloud scanner needs the full history to assign issues properly
definitions:
caches:
sonar: ~/.sonar/cache # Caching SonarCloud artifacts will speed up your build
steps:
- step: &build-test-sonarcloud
name: Build, test and analyze on SonarCloud
caches:
- node
- sonar
script:
- npm install --quiet
- npm run test:coverage
- pipe: sonarsource/sonarcloud-scan:0.1.5
variables:
SONAR_TOKEN: ${SONAR_TOKEN}
EXTRA_ARGS: '-Dsonar.sources=src -Dsonar.tests=src -Dsonar.test.inclusions="**.test.jsx" -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info'
- pipe: sonarsource/sonarcloud-quality-gate:0.1.1
variables:
SONAR_TOKEN: ${SONAR_TOKEN}
pipelines:
default:
- step: *build-test-sonarcloud
Although solarcloud-scan pipe runs successfully.
The problem is that the sonarsource/sonarcloud-quality-gate pipe requires a newer version of the sonarsource/sonarcloud-scan pipe. (This was the case ever since the first release of the sonarsource/sonarcloud-quality-gate pipe.)
Change your pipeline configuration like this:
- pipe: sonarsource/sonarcloud-scan:1.0.1
variables:
SONAR_TOKEN: ${SONAR_TOKEN}
EXTRA_ARGS: '-Dsonar.sources=src -Dsonar.tests=src -Dsonar.test.inclusions="**.test.jsx" -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info'
- pipe: sonarsource/sonarcloud-quality-gate:0.1.3
variables:
SONAR_TOKEN: ${SONAR_TOKEN}
An easy way to see the latest versions is in the pipeline editor.
When you edit the bitbucket-pipelines.yml file, a sidebar like this opens,
where you can filter the list by entering "sonar":
And then, click on a pipe to see details, and note the version used.

Jenkins Job-Builder: How to correctly include job-templates from external file?

I am investigating using Jenkins Job-Builder (from OpenStack) as our means of managing jenkins job configurations. In doing so I am trying to figure out the right (best?) way to include a job-template from an external file using the !include custom tag.
In the current use case we will basically have one template that is going to be used by a LOT of job. Each job is going to need to exist in its own file for reason that are out of scope here.
So far I have gotten this to work:
job-template.yml
name: 'pre-build-{proj}-{repo}'
project-type: freestyle
... etc ...
job-1.yml
- job-template:
!include job-template.yml
- project:
name: job-1
proj: my-proj
repo: my-repo
jobs:
- 'build-config-{proj}-{repo}'
This seem wrong because the template definition gets split across both files and require needless duplication of the -job-template: line in every job file. I would like to get the following to work instead:
job-template.yml
- job-template:
name: 'pre-build-{proj}-{repo}'
project-type: freestyle
... etc ...
job-1.yml
!include job-template.yml
- project:
name: job-1
proj: my-proj
repo: my-repo
jobs:
- 'build-config-{proj}-{repo}'
The latter unfortunately results in a yaml parse error on the - project: line:
yaml.scanner.ScannerError: mapping values are not allowed here
in "job-1.yml", line 3, column 10
Is there way to get the entire template definition into the template file? This will become particularly annoying if ever we need to pull in multiple templates from multiple files.
Jenkins-jobs takes a path argument which can be a directory holding your files (job-template.yaml, job-1.yaml and job-2.yaml. It will assemble them as a single YAML document, so you do not need to use !include. So you can write:
job-template.yaml
- job-template:
name: 'pre-build-{proj}-{repo}'
builders:
- shell: 'echo building {proj} for {repo}'
job1.yaml
- project:
name: job-1
proj: my-proj
repo: my-repo
jobs:
- 'pre-build-{proj}-{repo}'
job2.yaml
- project:
name: job-2
proj: my-other-proj
repo: my-other-repo
jobs:
- 'pre-build-{proj}-{repo}'
That will generates two jobs with the following shell commands:
pre-build-my-other-proj-my-other-repo:
<command>echo building my-other-proj for my-other-repo</command>
pre-build-my-proj-my-repo:
<command>echo building my-proj for my-repo</command>
Assuming the files are in a directory config/ you can generate them all with:
jenkins-jobs test config/ -o /tmp/myjobs
Or use the name argument to filter the jobs that will be realized:
jenkins-jobs test config/ -o /tmp/myjobs '*my-proj*'
# Creates pre-build-my-proj-my-repo
# Skips pre-build-my-other-proj-my-other-repo

Resources