Bitbucket pipe failure

Hi,

We are trying to use the Bitbucket Sonarcloud pipe but it is failing because

docker container run \
   --volume=/opt/atlassian/pipelines/agent/build:/opt/atlassian/pipelines/agent/build \
   --volume=/opt/atlassian/pipelines/agent/ssh:/opt/atlassian/pipelines/agent/ssh:ro \
   --volume=/usr/local/bin/docker:/usr/local/bin/docker:ro \
   --volume=/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes:/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes \
   --volume=/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes/sonarsource/sonarcloud-scan:/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes/sonarsource/sonarcloud-scan \
   --workdir=$(pwd) \
   --label=org.bitbucket.pipelines.system=true \   
   --env=CI="$CI" \   
   --env=BITBUCKET_COMMIT="$BITBUCKET_COMMIT" \   
   --env=BITBUCKET_BUILD_NUMBER="$BITBUCKET_BUILD_NUMBER" \   
   --env=BITBUCKET_BRANCH="$BITBUCKET_BRANCH" \   
   --env=BITBUCKET_PR_DESTINATION_BRANCH="$BITBUCKET_PR_DESTINATION_BRANCH" \   
   --env=BITBUCKET_PR_DESTINATION_COMMIT="$BITBUCKET_PR_DESTINATION_COMMIT" \   
   --env=BITBUCKET_PR_ID="$BITBUCKET_PR_ID" \   
   --env=BITBUCKET_TAG="$BITBUCKET_TAG" \   
   --env=BITBUCKET_BOOKMARK="$BITBUCKET_BOOKMARK" \   
   --env=BITBUCKET_REPO_OWNER="$BITBUCKET_REPO_OWNER" \   
   --env=BITBUCKET_REPO_OWNER_UUID="$BITBUCKET_REPO_OWNER_UUID" \   
   --env=BITBUCKET_REPO_SLUG="$BITBUCKET_REPO_SLUG" \   
   --env=BITBUCKET_REPO_UUID="$BITBUCKET_REPO_UUID" \   
   --env=BITBUCKET_CLONE_DIR="$BITBUCKET_CLONE_DIR" \   
   --env=BITBUCKET_PARALLEL_STEP="$BITBUCKET_PARALLEL_STEP" \   
   --env=BITBUCKET_PARALLEL_STEP_COUNT="$BITBUCKET_PARALLEL_STEP_COUNT" \   
   --env=BITBUCKET_GIT_HTTP_ORIGIN="$BITBUCKET_GIT_HTTP_ORIGIN" \   
   --env=BITBUCKET_GIT_SSH_ORIGIN="$BITBUCKET_GIT_SSH_ORIGIN" \   
   --env=PIPELINES_JWT_TOKEN="$PIPELINES_JWT_TOKEN" \   
   --env=BITBUCKET_DOCKER_HOST_INTERNAL="$BITBUCKET_DOCKER_HOST_INTERNAL" \   
   --env=DOCKER_HOST="tcp://host.docker.internal:2375" \   
   --env=BITBUCKET_PIPE_SHARED_STORAGE_DIR="/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes" \   
   --env=BITBUCKET_PIPE_STORAGE_DIR="/opt/atlassian/pipelines/agent/build/.bitbucket/pipelines/generated/pipeline/pipes/sonarsource/sonarcloud-scan" \   
   --add-host="host.docker.internal:$BITBUCKET_DOCKER_HOST_INTERNAL" \   
   sonarsource/sonarcloud-scan:0.1.5

   bash: BITBUCKET_PR_DESTINATION_BRANCH: unbound variable

The issue is both on the master builds AND on pull-request builds.
BITBUCKET_PR_DESTINATION_BRANCH is not supposed to be set on the master builds but I don’t know why it is not available for pull-request builds.

A good improvement could be to provide a better error message but I don’t know what is possible in Bitbucket pipes.

Thanks,
Julien

Hi Julien,
Can you post your bitbucket-pipelines.yml content?

According to the Bitbucket Pipelines docs, BITBUCKET_PR_DESTINATION_BRANCH should always be available for pull requests and passed into the pipe container. For non-PR builds the setting is not passed at all and that’s perfectly fine.

image: php:7.2

clone:
  depth: full

pipelines:
  default:
    - step:
        caches:
          - composer
          - sonarcloud
        script:
          - curl -sL https://bitbucket.org/ChargeMap/pipelines-utils/raw/master/pipelines.sh | bash -
          - . ~/.local/bin/install
          - docker-php-ext-install bcmath
          - pecl install xdebug
          - docker-php-ext-enable xdebug
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install
          - mkdir reports
          - php -dxdebug.coverage_enable=1 vendor/bin/phpunit --log-junit ./reports/phpunit.junit.xml --coverage-clover ./reports/phpunit.coverage.xml
          - vendor/bin/phpcs -q --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 --report=json --report-file=reports/codesniffer-result.json --standard=vendor/chargemap/coding-standard/ruleset.xml src/
          - curl -sL https://bitbucket.org/ChargeMap/pipelines-utils/raw/master/convert-results.php | php --
          # - php convert-results.php
          # - sonar-scanner -X -Dsonar.login=$SONAR_TOKEN
          - pipe: sonarsource/sonarcloud-scan:0.1.5
pull-requests:
  '**':
    - step:
        caches:
          - composer
          - sonarcloud
        script:
          - curl -sL https://bitbucket.org/ChargeMap/pipelines-utils/raw/master/pipelines.sh | bash -
          - . ~/.local/bin/install
          - docker-php-ext-install bcmath
          - pecl install xdebug
          - docker-php-ext-enable xdebug
          - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
          - composer install
          - mkdir reports
          - php -dxdebug.coverage_enable=1 vendor/bin/phpunit --log-junit ./reports/phpunit.junit.xml --coverage-clover ./reports/phpunit.coverage.xml
          - vendor/bin/phpcs -q --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 --report=json --report-file=reports/codesniffer-result.json --standard=vendor/chargemap/coding-standard/ruleset.xml src/
          - curl -sL https://bitbucket.org/ChargeMap/pipelines-utils/raw/master/convert-results.php | php --
          # - php convert-results.php
          # - sonar-scanner -X -Dsonar.login=$SONAR_TOKEN
          - pipe: sonarsource/sonarcloud-scan:0.1.5

definitions:
  caches:
    sonarcloud: ~/.sonar/cache

For non-PR builds the setting is not passed at all and that’s perfectly fine.

Right, but we have the same error message on builds of the master branch where BITBUCKET_PR_DESTINATION_BRANCH is not supposed to be used.

I’ve investigated it and the problem is not really specific to SonarCloud pipe (our pipe doesn’t even validate BITBUCKET_PR_DESTINATION_BRANCH). The problem is that the scripts which you execute before the pipe execution define “bash strict mode” (set -euo pipefail) which doesn’t seem to play nice with Bitbucket Pipes.

The quick way to fix this would be to not use strict mode or turn it off for the pipe execution like this:

...
- set +u
- pipe: sonarsource/sonarcloud-scan:0.1.5
  variables:
    SONAR_TOKEN: ${SONAR_TOKEN}
- set -u
...

BTW, I noticed that you don’t define the SONAR_TOKEN variable which is needed by the pipe (that one is actually validated). Also it seems like one of your scripts downloads and installs sonar scanner which is not needed since the pipe already contains the scanner.

Thank for the digging.

In fact, I copied/pasted strict mode from https://github.com/SonarSource/travis-utils/ :sweat_smile:

I suppose bash complains against missing env in strict mode. So, the question is: who is generating the docker container run command? IMO, it should not have the BITBUCKET_PR_* env for pull request builds and should work in strict mode too.

I noticed that you don’t define the SONAR_TOKEN variable which is needed by the pipe

Thank, we will update it! :+1:

it seems like one of your scripts downloads and installs sonar scanner which is not needed since the pipe already contains the scanner.

Right! It is our previous pipeline and we will remove it once the new pipe will work.

I agree it should work in “strict mode” but this happens on Bitbucket Pipelines side (it will affect all the other Pipes). I reported this to the Atlassian team that works on it.

Thank you for providing the logs and configuration!

Just an update: this issue was resolved on Bitbucket so the workaround is no longer needed.

1 Like