2019-06-07 08:00:41 +00:00
---
2020-05-29 18:08:26 +00:00
stage: Verify
2021-05-26 15:10:57 +00:00
group: Pipeline Execution
2020-11-26 06:09:20 +00:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2019-06-07 08:00:41 +00:00
type: howto
---
2019-07-08 08:50:38 +00:00
# Using GitLab CI/CD with a Bitbucket Cloud repository **(PREMIUM)**
2019-05-05 15:06:37 +00:00
2019-06-07 08:00:41 +00:00
GitLab CI/CD can be used with Bitbucket Cloud by:
2020-09-28 06:09:56 +00:00
1. Creating a [CI/CD project ](index.md ).
2019-06-07 08:00:41 +00:00
1. Connecting your Git repository via URL.
To use GitLab CI/CD with a Bitbucket Cloud repository:
2019-05-05 15:06:37 +00:00
2021-08-10 06:08:47 +00:00
1. In GitLab, create a project:
1. On the top menu, select **Projects > Create new project** .
1. Select **Run CI/CD for external repository** .
1. Select **Repo by URL** .
2019-05-05 15:06:37 +00:00
2019-08-01 02:29:16 +00:00
![Create project ](img/external_repository.png )
2019-05-05 15:06:37 +00:00
2021-07-15 09:09:35 +00:00
GitLab imports the repository and enables [Pull Mirroring ](../../user/project/repository/repository_mirroring.md#pull-from-a-remote-repository ).
2019-05-05 15:06:37 +00:00
2021-08-10 06:08:47 +00:00
1. In GitLab, create a
2019-05-05 15:06:37 +00:00
[Personal Access Token ](../../user/profile/personal_access_tokens.md )
2020-11-18 15:09:08 +00:00
with `api` scope. This is used to authenticate requests from the web
hook that is created in Bitbucket to notify GitLab of new commits.
2019-05-05 15:06:37 +00:00
2019-06-07 08:00:41 +00:00
1. In Bitbucket, from **Settings > Webhooks** , create a new web hook to notify
2019-05-05 15:06:37 +00:00
GitLab of new commits.
2019-08-01 02:29:16 +00:00
The web hook URL should be set to the GitLab API to trigger pull mirroring,
using the Personal Access Token we just generated for authentication.
2019-05-05 15:06:37 +00:00
2020-05-20 03:08:04 +00:00
```plaintext
2020-02-18 03:08:54 +00:00
https://gitlab.com/api/v4/projects/< PROJECT_ID > /mirror/pull?private_token=< PERSONAL_ACCESS_TOKEN >
2019-08-01 02:29:16 +00:00
```
2019-05-05 15:06:37 +00:00
2019-08-01 02:29:16 +00:00
The web hook Trigger should be set to 'Repository Push'.
2019-05-05 15:06:37 +00:00
2019-08-01 02:29:16 +00:00
![Bitbucket Cloud webhook ](img/bitbucket_webhook.png )
2019-05-05 15:06:37 +00:00
2019-08-01 02:29:16 +00:00
After saving, test the web hook by pushing a change to your Bitbucket
repository.
2019-05-05 15:06:37 +00:00
2019-06-07 08:00:41 +00:00
1. In Bitbucket, create an **App Password** from **Bitbucket Settings > App
2019-05-05 15:06:37 +00:00
Passwords** to authenticate the build status script setting commit build
statuses in Bitbucket. Repository write permissions are required.
2019-08-12 09:53:30 +00:00
![Bitbucket Cloud webhook ](img/bitbucket_app_password.png )
2019-05-05 15:06:37 +00:00
2021-02-16 18:09:24 +00:00
1. In GitLab, from **Settings > CI/CD > Variables** , add variables to allow
2019-06-07 08:00:41 +00:00
communication with Bitbucket via the Bitbucket API:
2019-05-05 15:06:37 +00:00
2019-08-01 02:29:16 +00:00
`BITBUCKET_ACCESS_TOKEN` : the Bitbucket app password created above.
2019-05-05 15:06:37 +00:00
2019-08-01 02:29:16 +00:00
`BITBUCKET_USERNAME` : the username of the Bitbucket account.
2019-05-05 15:06:37 +00:00
2019-08-01 02:29:16 +00:00
`BITBUCKET_NAMESPACE` : set this if your GitLab and Bitbucket namespaces differ.
2019-05-05 15:06:37 +00:00
2019-08-01 02:29:16 +00:00
`BITBUCKET_REPOSITORY` : set this if your GitLab and Bitbucket project names differ.
2019-05-05 15:06:37 +00:00
2019-06-07 08:00:41 +00:00
1. In Bitbucket, add a script to push the pipeline status to Bitbucket.
2019-05-05 15:06:37 +00:00
2020-12-16 03:09:46 +00:00
NOTE:
Changes made in GitLab are overwritten by any changes made
upstream in Bitbucket.
2019-08-01 02:29:16 +00:00
Create a file `build_status` and insert the script below and run
`chmod +x build_status` in your terminal to make the script executable.
2020-01-30 15:09:15 +00:00
```shell
2019-08-01 02:29:16 +00:00
#!/usr/bin/env bash
# Push GitLab CI/CD build status to Bitbucket Cloud
if [ -z "$BITBUCKET_ACCESS_TOKEN" ]; then
echo "ERROR: BITBUCKET_ACCESS_TOKEN is not set"
exit 1
fi
if [ -z "$BITBUCKET_USERNAME" ]; then
echo "ERROR: BITBUCKET_USERNAME is not set"
exit 1
fi
if [ -z "$BITBUCKET_NAMESPACE" ]; then
echo "Setting BITBUCKET_NAMESPACE to $CI_PROJECT_NAMESPACE"
BITBUCKET_NAMESPACE=$CI_PROJECT_NAMESPACE
fi
if [ -z "$BITBUCKET_REPOSITORY" ]; then
echo "Setting BITBUCKET_REPOSITORY to $CI_PROJECT_NAME"
BITBUCKET_REPOSITORY=$CI_PROJECT_NAME
fi
BITBUCKET_API_ROOT="https://api.bitbucket.org/2.0"
BITBUCKET_STATUS_API="$BITBUCKET_API_ROOT/repositories/$BITBUCKET_NAMESPACE/$BITBUCKET_REPOSITORY/commit/$CI_COMMIT_SHA/statuses/build"
BITBUCKET_KEY="ci/gitlab-ci/$CI_JOB_NAME"
case "$BUILD_STATUS" in
running)
BITBUCKET_STATE="INPROGRESS"
BITBUCKET_DESCRIPTION="The build is running!"
;;
passed)
BITBUCKET_STATE="SUCCESSFUL"
BITBUCKET_DESCRIPTION="The build passed!"
;;
failed)
BITBUCKET_STATE="FAILED"
BITBUCKET_DESCRIPTION="The build failed."
;;
esac
echo "Pushing status to $BITBUCKET_STATUS_API..."
2020-12-09 06:09:41 +00:00
curl --request POST "$BITBUCKET_STATUS_API" \
2019-08-01 02:29:16 +00:00
--user $BITBUCKET_USERNAME:$BITBUCKET_ACCESS_TOKEN \
--header "Content-Type:application/json" \
--silent \
--data "{ \"state\": \"$BITBUCKET_STATE\", \"key\": \"$BITBUCKET_KEY\", \"description\":
\"$BITBUCKET_DESCRIPTION\",\"url\": \"$CI_PROJECT_URL/-/jobs/$CI_JOB_ID\" }"
```
2019-05-05 15:06:37 +00:00
2021-08-10 06:08:47 +00:00
1. In Bitbucket, create a `.gitlab-ci.yml` file to use the script to push
2019-05-05 15:06:37 +00:00
pipeline success and failures to Bitbucket.
2019-08-01 02:29:16 +00:00
```yaml
stages:
- test
- ci_status
unit-tests:
script:
- echo "Success. Add your tests!"
success:
stage: ci_status
before_script:
- ""
after_script:
- ""
script:
- BUILD_STATUS=passed BUILD_KEY=push ./build_status
when: on_success
failure:
stage: ci_status
before_script:
- ""
after_script:
- ""
script:
- BUILD_STATUS=failed BUILD_KEY=push ./build_status
when: on_failure
```
2019-05-05 15:06:37 +00:00
GitLab is now configured to mirror changes from Bitbucket, run CI/CD pipelines
configured in `.gitlab-ci.yml` and push the status to Bitbucket.
2019-06-07 08:00:41 +00:00
<!-- ## Troubleshooting
Include any troubleshooting steps that you can foresee. If you know beforehand what issues
one might have when setting this up, or when something is changed, or on upgrading, it's
important to describe those, too. Think of things that may go wrong and include them here.
This is important to minimize requests for support, and to avoid doc comments with
questions that you know someone might ask.
Each scenario can be a third-level heading, e.g. `### Getting error message X` .
If you have none to add when creating a doc, leave this section in place
but commented out to help encourage others to add to it in the future. -->