Bitbucket Pre-Receive Hook
A Bitbucket pre-receive hook prevents software developers from pushing commits to Gitlab if Code Security found security issues in the code. This feature is supported only for a Gitlab instance on a Linux server.
Prerequisites
-
The Gitlab instance must be installed on a Linux server.
-
Code Security must be installed on the same Linux server as the Gitlab instance. See Getting Started with Code Security.
-
To load a custom Code Security configuration from the repository, you must have a Bitbucket service account with read-only personal access. If you do not have this type of Bitbucket service account, custom configurations from the repository do not apply.
Step 1: In Code Security, configure environment variables
Configure the Bitbucket pre-receive hook in Code Security by defining environment variables.
Environment variable |
Description |
Example value |
---|---|---|
|
Path to the Code Security binary. |
|
|
DSN from Code Security (SpectralOps), in Settings > Organization. |
|
|
Code Security command line arguments. |
|
|
The URL of the Bitbucket server. |
|
|
The personal access token for Bitbucket (optional functionality to load spectral.yaml from repo). |
|
|
The severity of findings allowed before the push is rejected. These values are supported:
|
|
|
A custom text to append to the rejection message. |
|
Example configuration
SPECTRAL_DSN="https://spu-xxxx@get.spectralops.io"
SPECTRAL_CHECK_POLICY="fail on high and above"
BITBUCKET_SERVER_BASE_URL="https://mydomain.com/bitbucket"
BITBUCKET_PAT="TmljZSB0cnkhIEJ1dCB0aGlzIGlzbid0IGEgcmVhbCBvbmUu"
SPECTRAL_COMMAND="scan --include-tags base,iac"
SPECTRAL_BIN="~/.spectral/spectral"
Step 2: On the Bitbucket server, install the pre-receive hook
-
Download the Code Security plugin JAR file to your computer.
-
In the Bitbucket server UI, open the Administration settings.
-
Click Manage apps > Upload app.
-
Upload the Spectral plugin JAR file to the Bitbucket server.
-
Do one of these:
-
Enable the hook for the entire Bitbucket server:
-
by going to the project -> Project settings -> Hooks and enabling "Spectral Pre Receive Hook"
-
-
You can also enable the hook for specific repository by going to the specific repository -> Repository settings -> Hooks and enabling "Spectral Pre Receive Hook"
-