BitBucket Cheatsheet
BitBucket Cheatsheet
Bitbucket, developed by Atlassian, is a Git-based source code repository hosting service. It is designed for teams and provides strong integration with other Atlassian tools like Jira, Trello, and Confluence. This cheatsheet provides a detailed guide for mastering Bitbucket from basic operations to advanced features.
1. Introduction to Bitbucket
What is Bitbucket?
- Bitbucket is a Git-based platform for version control, CI/CD pipelines, and project collaboration.
- It supports both private and public repositories.
- Known for its seamless integration with Atlassian tools (e.g., Jira) and in-built CI/CD pipelines.
Key Features
- Git repository hosting
- In-built CI/CD via Bitbucket Pipelines
- Jira integration for issue tracking
- Branch permissions and code review tools
- Supports Mercurial (deprecated)
2. Getting Started
Creating a Bitbucket Account
- Go to Bitbucket and sign up for an account.
- Optionally, link your Atlassian account for better integration.
Setting Up SSH Keys
-
Generate an SSH key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
-
Add the public key to Bitbucket:
- Navigate to Personal Settings → SSH Keys → Add Key.
Creating a Repository
- Log in to Bitbucket.
- Go to Repositories → Create Repository.
- Configure:
- Repository Name
- Access level (Private/Public)
- Repository Type (Git)
3. Basic Operations
Cloning a Repository
git clone git@bitbucket.org:username/repository.git
Staging, Committing, and Pushing
# Stage changes git add . # Commit changes git commit -m "Initial commit" # Push changes git push origin main
Pulling Changes
git pull origin main
4. Branching and Merging
Creating and Switching Branches
# Create a new branch git checkout -b feature-branch # Switch to an existing branch git checkout main
Pushing a Branch
git push origin feature-branch
Creating a Pull Request (PR)
- Open Bitbucket and navigate to Pull Requests.
- Click Create Pull Request.
- Select branches, add reviewers, and provide a description.
Merging Pull Requests
- Approve the PR.
- Merge using the options:
- Merge Commit: Keeps all commits intact.
- Squash Merge: Combines all commits into one.
- Rebase: Rewrites commit history.
5. Bitbucket Pipelines (CI/CD)
Overview
Bitbucket Pipelines is an integrated CI/CD service to automate builds, tests, and deployments.
Enabling Pipelines
- Go to the repository settings → Pipelines.
- Enable Pipelines and configure the
.bitbucket-pipelines.yml
file.
Sample Pipeline Configuration
pipelines: default: - step: name: Build and Test image: node:14 script: - npm install - npm test - step: name: Deploy to Production script: - echo "Deploying to production..."
Key Triggers
- default: Runs on any branch when pushed.
- branches: Customizes triggers for specific branches.
- tags: Automates deployment for version tags.
Variables and Secrets
- Go to Repository Settings → Pipelines → Environment Variables.
- Add sensitive variables like
AWS_ACCESS_KEY
.
Using Variables in Pipelines
script: - echo "Using secret: $AWS_ACCESS_KEY"
6. Branch Permissions and Access Control
Branch Permissions
- Go to Repository Settings → Branch Permissions.
- Add rules such as:
- Prevent direct pushes to
main
. - Require at least 2 code reviews before merging.
- Prevent direct pushes to
User Roles
- Admin: Full control over repositories and permissions.
- Write: Can push and pull code.
- Read: Read-only access to repositories.
7. Integration with Jira
Linking a Repository to Jira
- Go to Repository Settings → Jira Settings.
- Connect the repository to a Jira project.
Automating Issue Tracking
-
Add Jira issue keys in commit messages:
PROJ-123: Fix login page bug
-
Jira automatically links commits, pull requests, and deployments.
8. Code Review and Quality
Using Pull Requests for Code Review
- Assign reviewers while creating a pull request.
- Add comments inline to highlight issues.
Integrating Code Quality Tools
-
Add tools like SonarCloud or CodeClimate to your pipelines for static code analysis.
-
Example: Adding SonarCloud to Bitbucket Pipelines:
- pipe: sonarsource/sonarcloud-scan:1.4.0 variables: SONAR_TOKEN: $SONAR_TOKEN
9. Bitbucket API
Authenticating
Generate a personal access token:
- Go to Personal Settings → Access Management → Create App Password.
Use the token in API calls:
curl -u username:app_password https://api.bitbucket.org/2.0/repositories
Common API Endpoints
-
List repositories:
curl -X GET https://api.bitbucket.org/2.0/repositories/{username}
-
Create an issue:
curl -X POST -u username:app_password \ -H "Content-Type: application/json" \ -d '{"title": "Bug in Login Page", "content": {"raw": "Description"}}' \ https://api.bitbucket.org/2.0/repositories/{username}/{repo}/issues
10. Advanced Features
Deployments with Bitbucket Pipelines
Track deployment environments:
- Go to Deployments → Configure environments (e.g., Dev, Staging, Prod).
Add deployment steps in .bitbucket-pipelines.yml
:
pipelines: branches: main: - step: name: Deploy to Staging deployment: staging script: - ./deploy.sh staging
Monorepo Support
Host multiple services in one repository:
-
Use Pipelines for individual service builds:
pipelines: default: - step: name: Build Service A script: - cd services/service-a && npm install && npm test
Mirror Repositories
Mirror a repository between Bitbucket and GitHub:
git remote add bitbucket git@bitbucket.org:username/repo.git git push bitbucket --mirror
11. Security and Best Practices
Enforcing Two-Factor Authentication (2FA)
- Go to Personal Settings → Security → Enable 2FA.
Secret Scanning
Bitbucket scans for hard-coded credentials and alerts users.
Dependency Scanning
Use Atlassian tools like Snyk or Dependabot to identify vulnerabilities.
12. Best Practices
-
Branch Naming Convention:
- Use prefixes like
feature/
,bugfix/
, andrelease/
.
feature/add-login-form bugfix/fix-authentication-error
- Use prefixes like
-
Commit Messages:
-
Follow a format like:
[PROJ-123] Fix bug in login functionality
-
Reference Jira issues in commit messages.
-
-
Automate Everything:
- Use Pipelines for CI/CD.
- Automate linting, testing, and deployment.
-
Use Pull Request Templates:
- Add
.bitbucket/pull_request_template.md
to standardize PR descriptions.
- Add
13. References and Resources
Follow me on : Medium Linkedin Researchgate