Export VAR from code and pass it to another step, and deploy if directory dist exists

Hi,
I’m trying to implement a monorepo with Nx.dev and Lerna for versioning.

I’d like to tag my projects images with their versions with the following:

build:
title: “Running build”
type: “freestyle”
image: “node:14.9.0”
working_directory: “${{clone}}”
commands:
- “yarn nx affected --target=build --base=origin/master^ --prod --parallel”
# - “apt-get install jq”
# - “export VERSION=$(jq .version apps/advertiser-panel/package.json)”
# - “echo $VERSION”
stage: “test, lint and build monorepo”

build_advertiser_panel:
title: “Building advertiser-panel Docker image”
type: “build”
image_name: “advertiser-panel”
working_directory: “${{clone}}”
tag: $VERSION #“${{CF_BRANCH_TAG_NORMALIZED}}-${{CF_SHORT_REVISION}}”
dockerfile: “apps/advertiser-panel/Dockerfile”
stage: “build & deploy apps”

If the 1st commented code
# - “apt-get install jq”
# - “export VERSION=$(jq .version apps/advertiser-panel/package.json)”
# - “echo $VERSION”
will be on the build_advertiser_panel it will be perfect, as I need to tag several images with his own version (independent)

Another problem is I am trying to make build_advertiser_panel (second block of code) conditional.
Where the dir app/project1/dist exists. If dist inside project exists, then I must build the image and deplot on k8s.

Any help is welcome!

Hello and welcome to the Codefresh community

Regarding your first question you can use cf_export for passing a variable to all subsequent steps. This way you can calculate VERSION only once and then have all other steps use it. See details here Variables · Codefresh | Docs

You might find also this plugin very interesting Codefresh | kostis-codefresh/node-version step

For your second question just create a script that checks if your directory is there and then use a standard conditional like this Conditional Execution of Steps · Codefresh | Docs

However I think that instead of having conditional steps, it is easier if all steps execute all they time and they simply exit early if they don’t find what they need.

Let us know if that helps.