Git Ssh Key Generation Bitbucket

Jul 18, 2019  Options: If you don't have an existing public and private key pair, or don't wish to use any that are available to connect to BitBucket, then generate a new SSH key.; If you see an existing public and private key pair listed (for example idrsa.pub and idrsa) that you would like to use to connect to BitBucket, you can add your SSH key to the ssh-agent. Add key to BitBucket. On BitBucket, choose avatar Manage Account. Switch to the account you want to manage and click SSH keys; add a new key which should be named to match the server name (e.g., dev.foodomain.com). Paste the key into the Key field and click Add key. Clone BitBucket repo.

June, 2017 update: Setup SSH key for local dev box and use agent forwarding for servers

Create/setup the key on your server:

  1. Login to server using SSH/terminal.
  2. Depending on your setup, you may need to $ su - and enter the root user’s password (depending on the steps you take below, this will create files in that user’s home directory with root permissions).
  3. List the contents of .ssh directory: $ ls -a ~/.ssh and check for an existing id_rsa.pub; use that default identity in BitBucket (skip to next heading) or …
  4. At the command prompt, type: $ ssh-keygen.
  5. Follow the on-screen instructions (on a production machine, a password should be entered for security purposes).
  6. For comparison’s sake, list the contents of .ssh directory: $ ls -a ~/.ssh.
  7. Check to see if ssh-agent is running: $ ps -e | grep [s]sh-agent.
  8. If not (above command returns nothing) then run: ssh-agent /bin/bash.
  9. Add newly-created key to the ssh-agent: $ ssh-add ~/.ssh/id_rsa.
  10. View list of keys the ssh-agent is managing: $ ssh-add -l.
  11. Run $ cat ~/.ssh/id_rsa.pub and copy the output to your clipboard.

Reversing the above process is easy:

Add key to BitBucket

  1. On BitBucket, choose avatar > Manage Account.
  2. Switch to the account you want to manage and click SSH keys; add a new key which should be named to match the server name (e.g., dev.foodomain.com).
  3. Paste the key into the Key field and click Add key.

Clone BitBucket repo

  1. Go to your repo in BitBucket and copy the SSH URI (e.g., git@bitbucket.org:user-name/repo-name.git).
  2. SSH to your server and navigate to the location you want to clone the repo to.
  3. From the command line, run: $ git clone git@bitbucket.org:user-name/repo-name.git. Note: If you want to specify the folder name that the repo clones into, add that to the end of the command (e.g., git clone git@bitbucket.org:user-name/repo-name.git target-folder-name).

Done!

Pulling updates

  1. SSH to your server.
  2. Navigate to your repo: $ cd target-folder-name/.
  3. Run $ git remote update && git status$ git fetch.
  4. If there are changes to pull, then run $ git pull.
  5. Optionally re-run $ git remote update && git status$ git fetch && git pull to see if everything is clean.

Links

Clone this wiki locally

This is a starting point for using Bitbucket Pipelines in a Java project thatuses Maven. It does the following:

  • Automatic tasks:
    • All branches: Build the project (mvn clean verify)
    • master branch: Deploy the build artifact (mvn deploy)
  • Manual tasks (can be invoked within the Bitbucket UI):
    • Release major version
    • Release minor version
    • Release patch
Git Ssh Key Generation Bitbucket

The manual release tasks

  • Incrememnt the major, minor, or patch component of the version propertyin the project's pom.xml
  • Commit the change to pom.xml to develop
  • Tag develop with the updated version
  • Merge develop into master
Windows

For this to work, you should follow these rules:

  • Create a new branch for all new features, bugfixes, etc.
  • Merge branches into develop only. Don't merge directly into master.
  • Don't merge branches with broken builds into develop.
  • Only use semver versions in pom.xml, but stick toMAJOR.MINOR.PATCH (no suffixes, e.g. -beta) otherwise version bumpingwill fail.
  • Don't use snapshot versions.

To set up an Artifactory server and to configure Java projects for deploymentto Artifactory, seesteve-taylor/artifact-server-config.

Configuring Bitbucket Pipelines

bitbucket-pipelines.yml by itself isn't quite enough to fully configure yourproject for Bitbucket Pipelines. You will need to provide some additionalsettings in Bitbucket.

Bitbucket Ssh Setup

Environment variables

The following environment variables need to be set withing Bitbucket.Fortunately, you can set all of these at the team level and they will beapplied to all repositories within the team.

NameExampleDescription
DEPLOYER_NAMEDeploy BotDeployment script name (appears in git logs)
DEPLOYER_EMAILdeploybot@example.comDeployment script email address (appears in git logs)
MAVEN_REPO_URLhttps://artifactory.example.com/artifactoryMaven repository URL
MAVEN_REPO_RELEASES_KEYlibs-releasePublic releases cache repo ID
MAVEN_SETTINGS_PROFILEartifactoryProfile name
MAVEN_REPO_READER_USERNAMEmeCI/CD Maven read-only username
MAVEN_REPO_READER_PASSWORDswordfishCI/CD Maven read-only password
MAVEN_REPO_PUBLISHER_USERNAMEcicdCI/CD Maven read-write username
MAVEN_REPO_PUBLISHER_PASSWORDsuper-secretCI/CD Maven read-write password
MAVEN_REPO_RELEASES_LOCAL_KEYlibs-release-localPrivate releases repo ID

ssh

Unfortunately, the ssh keys provided by Bitbucket Pipelines don't allow tasksto push back to their git repository. You will need to generate a new ssh keypair that allows Bitbucket Pipelines to push to git.

  1. Go to Settings / Security / SSH keys in your Bitbucket team.
  2. Click Add key
  3. Generate an ssh key pair and paste the public key into into the Keyfield. (The dialog contains links to instructions to generate an ssh keypair.)
  4. Provide a label and click Add key to finish adding the team-level sshkey.
  5. Navigate to your repo and go to Settings / Pipelines / SSH keys.
  6. If there is already a key, delete it.
  7. Provide the private and public keys from step 2.

For additional projects, repeat steps 5 to 7.

Git Ssh Key Generation Bitbucket

Create Ssh Key For Git

Note: Bitbucket will log warnings each time it pushes using the team-levelssh key, as it is a deprecated feature and they unfortunately recommend usingan individual account's ssh key instead. You're quite welcome to follow thatrecommendation if it makes you sleep better at night.