Search code examples
bitbucket-pipelines

Bitbucket Pipeline does not replace all "Deployments" variables


I'm having an issue with Pipeline deployment. This is my yml file:

pipelines:
  branches:
    staging:
      - step:
          name: Build an app
          image: node:22.0.0
          caches:
            - node
          size: 2x
          script:
            - npm install
            - export NODE_OPTIONS=--max-old-space-size=6144
            - npm run build:staging
          artifacts:
            - dist/**
      - step:
          name: Upload source maps to Sentry
          image: getsentry/sentry-cli:2.40.0
          script:
            - sentry-cli sourcemaps inject --org XXX --project $SENTRY_PROJECT_NAME ./dist
            - sentry-cli sourcemaps upload --org XXX --project $SENTRY_PROJECT_NAME --auth-token $SENTRY_AUTH_TOKEN ./dist
      - step:
          name: Deploy new build to staging droplet
          deployment: staging
          script:
            - rsync -rzO dist/skote/ $SSH_USER@$SSH_SERVER:/home/$WEBSITE/$URL/dist/ --exclude=bitbucket-pipelines.yml

As you can see, there are few variables in there and are defined in these scopes:

  • Deployments
    • staging
      • SSH_SERVER
      • WEBSITE
      • URL
    • Repository variables
      • SSH_USER
      • SENTRY_AUTH_TOKEN

The problem I'm having is that all work well, except the $SENTRY_PROJECT_NAME. The variable while executing pipeline is empty and the pipeline errors out, while ALL other do work. If I move the variable SENTRY_PROJECT_NAME into Repository variables, then it starts to work, but this way the variables are not that well structured, since I wanted to separate them per deployment. Right now I resolved by using two variables SENTRY_PROJECT_NAME and SENTRY_PROJECT_NAME_STAGING. With Deployments, I can have same variable name and two different strings in it, based on which environment is being used for deployment.

My question is: why is the SENTRY_PROJECT_NAME ignored when it's added into Deployment environment and how to properly utilize it?


Solution

  • Don't.

    Use a single Sentry project for all environments and configure your Sentry SDK to use a SENTRY_ENVIRONMENT

    https://docs.sentry.io/platforms/javascript/configuration/environments/

    Events and traces will be tagged accordingly and it is a much more powerful configuration to track releases, source maps, regressions, etc.


    Strictly answering your question:

    You can only use deployment variables in the steps explicitly marked with deployment: xxx in the yaml.

    Thus, both your sentry-cli and rsync commands should happen in the same step if you want to use deployment variables.

    Because they may need different container images featuring different commands, pipes are handy for this.

    pipelines:
      branches:
        staging:
          - step:
              name: Build an app
              image: node:22.0.0
              caches:
                - node
              size: 2x
              script:
                - npm install
                - export NODE_OPTIONS=--max-old-space-size=6144
                - npm run build:staging
              artifacts:
                - dist/**
          - step:
              name: Deploy staging
              image: getsentry/sentry-cli:2.40.0
              deployment: staging
              script:
                - sentry-cli sourcemaps inject --org XXX --project $SENTRY_PROJECT_NAME ./dist
                - sentry-cli sourcemaps upload --org XXX --project $SENTRY_PROJECT_NAME --auth-token $SENTRY_AUTH_TOKEN ./dist
                - pipe: atlassian/rsync-deploy
                  variables:
                    USER: $SSH_USER
                    SERVER: $SSH_SERVER
                    REMOTE_PATH: /home/$WEBSITE/$URL/dist/
                    LOCAL_PATH: dist/skote/
    

    Only a "Sentry code report" and a "Sentry new release" pipes exist in the Atlassian catalog https://bitbucket.org/product/features/pipelines/integrations?&search=sentry , I'd suggest you craft your own "Sentry sourcemap upload" if you ever need to avoid the getsentry/sentry-cli image in the step.


    Alternatively, create an additional custom deployment environment for each actual environment and use it to "Upload source maps to Sentry"

    pipelines:
      branches:
        staging:
          - step:
              name: Build an app
              image: node:22.0.0
              caches:
                - node
              size: 2x
              script:
                - npm install
                - export NODE_OPTIONS=--max-old-space-size=6144
                - npm run build:staging
              artifacts:
                - dist/**
          - step:
              name: Upload source maps to Sentry
              image: getsentry/sentry-cli:2.40.0
              deployment: staging-sentry  # <----------------------------
              script:
                - sentry-cli sourcemaps inject --org XXX --project $SENTRY_PROJECT_NAME ./dist
                - sentry-cli sourcemaps upload --org XXX --project $SENTRY_PROJECT_NAME --auth-token $SENTRY_AUTH_TOKEN ./dist
    
          - step:
              name: Deploy new build to staging droplet
              deployment: staging
              script:
                - pipe: atlassian/rsync-deploy
                  variables:
                    USER: $SSH_USER
                    SERVER: $SSH_SERVER
                    REMOTE_PATH: /home/$WEBSITE/$URL/dist/
                    LOCAL_PATH: dist/skote/