Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Bitbucket
Bitbucket Cloud

Documentation

Use multiple SSH keys in your pipeline

Bitbucket Pipelines supports one SSH key per repository. However, you can use multiple keys with a pipeline by adding them as secured variables, and referencing them in thebitbucket-pipelines.yml file. Follow the steps below to set up and use multiple SSH keys in your pipeline.

Generate an SSH key (if necessary) 

Generate an key pairwithout a passphrase. On Linux or OS X, you can run the following in a terminal:

ssh-keygen -t rsa -b 4096 -N '' -f my_ssh_key

Encode the private key

Pipelines does not currently support line breaks in environment variables, so base-64 encode the private key by running:

Linux

base64 -w 0 < my_ssh_key

Mac OS X

base64 < my_ssh_key

Windows

[convert]::ToBase64String((Get-Content -path "~/.ssh/my_ssh_key" -Encoding byte))

Add the key as a secure variable

There are security risks associated with passing private SSH keys as repository variables:

  • Repository variables get copied to child processes that your pipelines build may spawn.

  • Secured variables can be retrieved by all users withwrite access to a repository.

We recommend that you never pass your ownpersonal SSH key as an repository variable, but instead generate a new SSH key-pair for Pipelines that easily be disabled if it is compromised. It may also be worth using deployment variables, which you can combine with deployment permissions to control access.

Copy the encoded key from the terminal and add it as a secured Bitbucket Pipelines environment variable for the repository:

  1. In the Bitbucket repository, selectRepository settings, then underPipelines, selectRepository variables.

  2. Copy the base64-encoded private key from the terminal.

  3. Paste the encoded key as the value for an environment variable. Make sure to checkSecured.

Secured highlighted with box checked

Install the public key on a remote host

Create the my_known_hosts file and add it to your repo

The known_hosts file contains the DSA host keys of SSH servers accessed by the user. It's important to verify that you're connecting to the correct remote host. Note that Bitbucket Pipelines automatically adds the fingerprint for the Bitbucket and GitHub sites to all pipelines.

  1. Create the my_known_hosts file that includes the public SSH key of the remote host. You can do this by executing thessh-keyscan command:

    $ ssh-keyscan -t rsa server.example.com > my_known_hosts
  2. Commit the my_known_hosts file to your repository from where your pipeline can access it.

Alternatively, you can copy an existing known_hosts file from the ~/.ssh directory of a user who has previously accessed the remote host via SSH. You can remove all unrelated lines.

Tie everything together in the bitbucket-pipelines.yml file

Pipelines spins up a new Docker container environment for every build. You can use the SSH key by referencing it in the bitbucket-pipelines.yml file. 

To reference the SSH key for Docker containers that run your pipelines:

image: node:6 # specify your Docker image herepipelines: default: - step: script: - mkdir -p ~/.ssh - cat my_known_hosts >> ~/.ssh/known_hosts - (umask 077 ; echo $MY_SSH_KEY | base64 --decode > ~/.ssh/id_rsa) - ssh <user>@<host> 'echo "connected to `host` as $USER"'

The example above just connects to the host and echoes "connected to 'host' as <user>".

Note that thessh command in the final line will use your default SSH identity. To specify a different key, use the-i option like this:

ssh -i ~/.ssh/my_other_ssh_key <user>@<host>

You can also modify the last line to use scp to transfer files or git to clone files from a remote server via SSH.

Was this helpful?

Still need help?

The Atlassian Community is here for you.

[8]ページ先頭

©2009-2025 Movatter.jp