Mr.wiseguy
Mr.wiseguy

Reputation: 4252

Bitbucket pipelines - Run or skip step based on build output

We have a monorepo that contains about 5 projects. All these projects are build based on the git change history, so if project A is not changed, it is not build. This is al managed by Nx monorepo.

We are using Bitbucket Pipelines to build and deploy our projects. We want to split every deploy into it's own step so that we have more control over each project's deployment.

In order to achieve this we need to change our build step so that it only executes if the dist folder contains the portal it is ment to deploy. I've read about the condition configuration, but i cannot find anything about checking build artifacts in a condition instead of the git commit that triggerd the change. So is there a way to skip (or directly pass) a step if the portal is not in the build artifact?

Build step

- step: &build
    name: Build
    caches:
      - node
    script:
      - git fetch origin master:refs/remotes/master
      - npm run build
    artifacts:
      - dist/**

Example Dist artifact

.
├── dist/
│   ├── login-portal
│   ├── Portal-Y

Our deploy step

- step: &deployLoginPortal
    image: amazon/aws-cli:2.4.17
    deployment: test
    trigger: manual
    script:
      - aws s3 sync $LOGIN_OUTPUT_PATH s3://$LOGIN_S3_BUCKET/ --acl public-read # Sync the portal

# $LOGIN_OUTPUT_PATH = 'dist/login-portal'

Example condition (does not work)

condition:
  changesets:
    includePaths:
      - $LOGIN_OUTPUT_PATH/** # only run if dist contains changes in $LOGIN_BUILD_PATH

Am I missing something or is there an other way of only executing the step if the build artifact (dist/) contains the portal the step is ment to deploy?

Upvotes: 4

Views: 4571

Answers (1)

esimonov
esimonov

Reputation: 634

You can manually check the presence of an artifact before doing aws s3 sync and gracefully exit the step if condition is not satisfied:

script:
  - [ ! -d "$LOGIN_OUTPUT_PATH" ] && echo "Directory $LOGIN_OUTPUT_PATH is not present; gracefully exiting" && exit 0
  - aws s3 sync $LOGIN_OUTPUT_PATH s3://$LOGIN_S3_BUCKET/ --acl public-read

Be advised that your step will still technically be triggered, so its startup time (usually about 40 seconds) will be affecting your quotas.

Upvotes: 4

Related Questions