What is the best way to utilize Bitbucket pipeline repository variables in an npm run script?

Is there a way to pass a Bitbucket repository variable into my Cypress.io test script that is run with npm run in a pipeline?

While my pipeline and tests are functioning properly, I find myself unable to incorporate a Bitbucket variable directly into my test file. I can easily access the repository variable from bitbucket-pipeline.yml as per Bitbucket's guidelines on the repository variable page, however, the same variable remains out of reach within cypress/integration/example.js. My intention is to securely store credentials used by the test scripts as Bitbucket repository variables.

Shown below is part of my code...

bitbucket-pipeline.yml

image: cypress/included:3.2.0

pipelines:
  custom:
    robot:
      - step:
          script:
            - printenv
            - npm ci
            - npm run e2e
  • The provided image is sourced from Cypress
  • I can view my repository variables using printenv

package.json

{
  ...
  "scripts": {
    "e2e": "cypress run"
  },
  ...
  "dependencies": {
    "cypress": "^9.4.1"
  }
}

cypress/integration/example.js

describe('A', () => {
  it('should B', () => {
    cy.visit('https://google.com');
  });
});
  • I aim to utilize the Bitbucket repository variable within the it('should B' ...) method.

Your assistance on this matter would be greatly appreciated.

Answer №1

After browsing through the Cypress.io documentation, I came across some valuable information about environment variables: https://docs.cypress.io/guides/guides/environment-variables

bitbucket-pipeline.yml

image: cypress/included:3.2.0

pipelines:
  custom:
    robot:
      - step:
          script:
            - npm ci
            - export CYPRESS_example=$example
            - export CYPRESS_whatever=$whatever
            - npm run e2e

In this setup, example is meant to represent the Bitbucket repo variable. It's important to note that it may be case sensitive.

Remember to include a $ when referencing the Bitbucket repository variable in the bitbucket-pipeline.yml file.

For Cypress environment variables, prefix them with CYPRESS_. https://docs.cypress.io/guides/guides/environment-variables#Option-3-CYPRESS_

Once defined, you can access these variables in your test spec using Cypress.env("example")

I encountered issues with data types from Bitbucket, so I ended up typecasting like this...

cy.get('[name="password"]').clear().type(Cypress.env("example").toString());

Answer №2

Utilize the plugin to create individual profiles for each deployment. When running the script, you have the option to specify which environment to run the script in. For instance, the following commands would work if the plugin is configured correctly within the pipeline:

npx cypress run --env version="qa" npx cypress run --env version="prod"

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

An issue has been detected in the SQL syntax; it is advisable to refer to the manual that coincides with your MySQL server version to determine the correct syntax to be used around 'WHERE id = '1''

Issue: ER_PARSE_ERROR in SQL syntax; please refer to the MySQL server version manual for correct syntax near 'WHERE id = '1'' at line 1 at Query.Sequence._packetToError (D:\Softwares\usermana\Nodejs-UserManagement-Exp ...

npm installs an incorrect package version

Currently attempting to set up karma, and encountering issues while using the command npm i karma@^1.0.0. Instead of the latest version, npm is installing <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="167d77645b72562738263826" ...

Finding the current HTML page URL in Ionic 2: A step-by-step guide

Can the URL of the current HTML page be obtained and then converted to PDF using the cordova-pdf-generator package? ...

Errors encountered while running `npm install discord.js`

I am currently facing an issue while trying to install discord.js. Unfortunately, I keep encountering the following errors: npm ERR! cb() never called! npm ERR! This is an error with npm itself. Please report this error at: npm ERR! <https://npm.co ...

Can you please explain the reason behind the "npm WARN EBADENGINE" notification?

After running npm install and creating a package-lock.json file, I encountered the following error: npm WARN EBADENGINE Unsupported engine { npm WARN EBADENGINE package: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="07 ...

Encountering "gyp ERR! find VS" error while installing npm in react-native

I had to transfer my react-native code from another PC, and when I attempted to run npm install, an error occurred. For a possible solution, you can check out this related answer: How can I solve the gyp error regarding msvs_version not being set? Please ...

The engine for integrating [email protected] with node-red-contrib-amqp is not compatible

Is the latest version of node-red installed on Ubuntu 20.04? I'm attempting to include the node-red-contrib-amqp plugin according to the instructions on the GitHub page. https://github.com/abreits/node-red-contrib-amqp#installation $ sudo npm instal ...

I am experiencing a problem with installing node-sass through npm

I've been struggling to get node-sass installed, but I keep encountering the same error. I've checked answers on stackoverflow, but none of them have solved my issue. It's frustrating because I can't pinpoint where exactly the problem l ...

What is the process to install a npm module from Github and compile it?

When it comes to Github repositories containing node modules, there are a variety of options available for installation. Some are published as NPM packages and can be easily installed using npm install <module>. Other times, the repository only inclu ...

Creating an offline private registry with verdaccio can be achieved by following these steps

One of my recent projects involved setting up a private npm registry using verdaccio. I needed to ensure that running npm install --registry="http://localhost:4873" would fetch all dependencies from the private registry. To achieve this, I had to publish ...

Make sure to install nodejs version 4.2.0 or higher

When setting up my Ubuntu machine, I installed node and npm by running the command: node agent/vsoagent, I encountered an error indicating that the node version should be at least 4.2.0.

How can I update to the required version while ensuring any e ...

Setting up the necessary dependencies for a React JS project

I'm currently in the process of duplicating this project: git clone <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3d5a54497d5a544955485f135e5250">[email protected]</a>:davidzas/react-chat.git After cloning, ...

Encountering a 403 error while trying to retrieve my identity with npm-automation-token

Whenever I try the npm whoami command with an npm automation token, it returns a 403 error. Here is my npmrc configuration: //registry.npmjs.org/:_authToken=${NPM_TOKEN} Executing the following command results in the error: NPM_TOKEN=xxxxxx-xxxxx-xxxxxx-x ...

Angular 2/NPM: setting up a new directory with all the necessary files for running the application

I'm feeling a bit frustrated, but I'm giving it a shot anyway. Our goal is to create a TypeScript Angular 2 hello world app that we can use as the front end for a Spring app. Currently, we're using the Angular 2 quickstart as our foundation ...

Issue arises when attempting to run npm start on Windows 10 due to node-gyp failing

After setting up Node.js version 12.18.1 and Python v3.8.1 on my Windows 10 system, I encountered an issue when trying to run npm install in a project: gyp ERR! configure error gyp ERR! stack Error: Command failed: C:\Program Files (x86)\Python&b ...

Variations of a particular software package are necessary

My current project requires Expo, React, and React-Native as dependencies. The configuration in the package.jason file looks like this: "main": "node_modules/expo/AppEntry.js", "private": true, "dependencies": { "expo": "^28.0.0", "expo-three": "^ ...

The versions of Node and NPM are not in sync

I am facing an issue where the node and npm versions on my Mac do not match. What is the best way to downgrade npm to align with the same version as node? Node: $ node -v $ v0.12.7 NPM: $ npm -v $ 2.14.4 ...

I'm unsure if I have the correct version of Node.js. Can someone confirm this for me?

I've been struggling to update my Node.js version for quite some time now. Whenever I check using node -v, it shows me as running: v0.10.40. However, when I visit nodejs.org, it displays the latest version as v7.4.0. I have already tried cleaning the ...

What are the steps to configure npm install at the system level?

Each time I develop a new app for Angular 6, running npm install becomes quite time-consuming. Presently, I have uploaded my Angular app to TFS without including the node_modules folder. Consequently, when my team members update the codebase, they must als ...

Steps to include a registry in npm installation for a scoped package

I am attempting to use npm install with a registry and scope. An example would be @test:registry=url After trying various methods, such as: npm install --registry @test url npm install --registry@test url I have not been successful in making it work. ...