Deploy build artifacts to Bitbucket Downloads
Bitbucket app passwords and the Bitbucket REST API allow you to deploy an artifact that has been produced by your pipeline to Bitbucket Downloads.
See also Test with databases in Bitbucket Pipelines.
Step 1: Create an API token
API tokens created to access Bitbucket APIs or perform Git commands must have scopes.
Select the Settings cog in the upper-right corner of the top navigation bar.
Under Personal settings, select Atlassian account settings.
Select the Security tab on the top navigation bar.
Select Create and manage API tokens.
Select Create API token with scopes.
Give the API token a name and an expiry date, usually related to the application that will use the token and select Next.
Select Bitbucket as the app and select Next.
Select the scopes (permissions) the API token needs and select Next. For detailed descriptions of each permission, see: API Token permissions. Note: This step is required for your API token to access Bitbucket APIs or perform Git commands.
Review your token and select the Create token button. The page will display the New API token.
Copy the generated API token and either record or paste it into the application you want to give access.
The token is only displayed once and can't be retrieved later.
Step 2: Create a Pipelines variable with the authentication token
In order to upload files to Bitbucket Downloads you will need to configure the following variables:
Parameter | Value |
---|---|
ATLASSIAN_ACCOUNT_EMAIL | Atlassian account email of the repository owner (and also the user who will upload the artifacts) |
ATLASSIAN_API_TOKEN | API token as generated by Atlassian |
You can define these variables for a specific deployment environment from the repository’s settings. Select Repository settings > select Repository variables under Pipelines.
Outcome
You should now have these two variables configured in your repository which means they are ready to be used from a pipeline.
Step 3a: Deploy your artifacts using the bitbucket-upload-file pipe
Use bitbucket-upload-file pipe to upload your artifact to Bitbucket Downloads. You just need to reference the file you want to upload from your build output.
Below is a simple example from an Android project built with Gradle. You can make your debug Android Package (APK) available in Bitbucket Downloads.
Bitbucket-pipelines.yml
image: maven:3.3.3
pipelines:
default:
- step:
script:
- pipe: atlassian/bitbucket-upload-file:0.1.2
variables:
ATLASSIAN_ACCOUNT_EMAIL: $ATLASSIAN_EMAIL
ATLASSIAN_API_TOKEN: $ATLASSIAN_API_TOKEN
FILENAME: "app/build/outputs/apk/debug/application-debug.apk"
Outcome
You should have a bitbucket_pipeline.yml file with the pipe configuration. When the step completes, your artifact will be uploaded to the Downloads section in your repository.
Step 3b: Deploy your artifacts using curl and the Bitbucket REST API
With the variable and app password in place, you can now use curl
in your build script to deploy artifacts to Bitbucket via the REST API:
curl -X POST
"https://${ATLASSIAN_ACCOUNT_EMAIL}:${ATLASSIAN_API_TOKEN}@api.bitbucket.org/2.0/repositories/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}/downloads" --form files=@"app/build/outputs/apk/debug/application.apk"
Below, there is a simple example from an Android project built with Gradle. You can make your debug Android Package (APK) available in Bitbucket Downloads.
Bitbucket-pipelines.yml
image: maven:3.3.3
pipelines:
default:
- step:
script:
- mvn -B clean install
- curl -X POST "https://${ATLASSIAN_ACCOUNT_EMAIL}:${ATLASSIAN_API_TOKEN}@api.bitbucket.org/2.0/repositories/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}/downloads" --form files=@"app/build/outputs/apk/debug/application.apk"
Outcome
You should have a bitbucket_pipeline.yml file with the pipe configuration. When the step completes, your artifact will be uploaded to the Downloads section in your repository.
You can check your bitbucket-pipelines.yml file with our online validator.
Was this helpful?