https://cypress.io logo
How can I replace a value in my cypress.config.js ...
# i-need-help
f
This may be a question for devops or something but I'm lost here. I define a task to entablish a DB connection through SSH in my cypress.config.js like this:
Copy code
on('task', {
  executeSql (sql, ...args) {
    return new Promise(async (resolve, reject)) => {
      try {
        let connection = await mysqlssh.connect(
          {
          host: "thisismyhostthatImnotsharing"
          user: "supersecretuser"
          privateKey: fs.readFileSync('cypress/fixtures/bd-ssh-key/id_rsa')
          },
          {
          host: "mysqlhost"
          user: "mysqluser"
          password: "mysqlpassword"
          database: "mysqlDatabase"
          });
        let result = await connection.promise().query(sql, args);
        mysqlssh.close();
        resolve(result[0][0]);
      } catch (err) {
        reject(err);
      }
    });
  }),
})
This is working perfectly fine locally because I have my SSH key file in the provided location
privateKey: fs.readFileSync('cypress/fixtures/bd-ssh-key/id_rsa')
The problem is that I can't commit this key into the repo. And what it has been requested is that I create a github secret with this key, and call it from there using
${{  secret.SSH_KEY  }}
I have absolutely no idea how to do this. I though of storing the key in a variable, and passing it on as an argument in the execution command but it didn´t work. Any ideas?
e
Your options are to have the keys stored in a .env that is added to .gitignore that Cypress calls, but the limitation there is whoever is running locally would need to make sure they had that file. The other option is exactly like you said, in order to run in CI/CD DevOps will need to create a secret for you in your CI?CD env.
f
I created the secret already, but I didn't know how to call it. Apparently what I need is to create an env variable on my yml, to store that, and then cypress config file supports
process.env.<variable>
to get that value and use it. I'm closer thanks!
n
After you created a secret in the repo, you can access it in your yml file as
Copy code
env:
  SSH_KEY: ${{ secrets.SSH_KEY }}
then you could pass it to your cypress run using any of these methods https://docs.cypress.io/guides/guides/environment-variables
I would go for option 3, just name variable as
CYPRESS_SSH_KEY
7 Views