Bitbucket ダウンロードにビルド アーティファクトをデプロイする
Bitbucket API トークンと Bitbucket REST API を使用すると、パイプラインによって生成されたアーティファクトを Bitbucket Downloads にデプロイできます。
See also Test with databases in Bitbucket Pipelines.
ステップ 1: API トークンを作成する
Bitbucket API へのアクセスや Git コマンドの実行のために作成される API トークンにはスコープが必要です。
上部ナビゲーション バーの右上隅にある [設定] 歯車アイコンを選択します。
[個人設定] で [Atlassian アカウント設定] を選択します。
上部のナビゲーション バーの [セキュリティ] タブを選択します。
[API トークンの作成と管理] を選択します。
[スコープ付き API トークンを作成] を選択します。
API トークンに名前と有効期限を指定し (通常は、トークンを使用するアプリに関連するものを指定する)、[次へ] を選択します。
アプリとして [Bitbucket] を選択し、[次へ] を選択します。
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.
トークンを確認し、[トークンを作成] ボタンを選択します。ページに新しい API トークンが表示されます。
生成された API トークンをコピーして、記録するか、アクセス権を付与するアプリに貼り付けます。
トークンは一度だけ表示され、後から取得することはできません。
手順 2: 認証トークンを使用して Pipelines 変数を作成する
Bitbucket Downloads にファイルをアップロードするには、以下の変数を設定する必要があります。
パラメーター | 値 |
|---|---|
ATLASSIAN_ACCOUNT_EMAIL | リポジトリ所有者 (および成果物をアップロードするユーザー) の Atlassian アカウントのメール アドレス |
ATLASSIAN_API_TOKEN | アトラシアンが生成する API トークン |
You can define these variables for a specific deployment environment from the repository’s settings. Next to the name of your repository in the left sidebar, select More actions (…), then select Settings, and then select Repository variables under Pipelines.
結果
リポジトリでこれら 2 つの変数が構成され、パイプラインから使用する準備ができました。
ステップ 3a: bitbucket-upload-file パイプを使用したアーティファクトのデプロイ
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.
hello world Go アプリを作成し、コンパイルされたバイナリを構築してアップロードする一例をご紹介します。
bitbucket-pipelines.yml
image: golang:1.25.0
pipelines:
default:
- step:
name: Create hello world go app
artifacts:
- main.go
script:
- |
cat > main.go << 'EOF'
package main
func main() { println("Hello, World!") }
EOF
- step:
name: Build
artifacts:
- compiled_app
script:
- go build -o compiled_app main.go
- step:
name: Upload
script:
- pipe: atlassian/bitbucket-upload-file:0.7.5
variables:
ATLASSIAN_ACCOUNT_EMAIL: $ATLASSIAN_ACCOUNT_EMAIL
ATLASSIAN_API_TOKEN: $ATLASSIAN_API_TOKEN
FILENAME: "compiled_app"結果
上記の構成の bitbucket-pipelines.yml ファイルをリポジトリのルートに配置する必要があります。ステップが完了すると、アーティファクトはリポジトリの Downloads セクションにアップロードされます。
ステップ 3b: curl および Bitbucket REST API を使用したアーティファクトのデプロイ
ビルド スクリプトで curl を使用して、REST API 経由でアーティファクトを Bitbucket にデプロイできます。
curl -X POST "$UPLOAD_URL" --user "$ATLASSIAN_ACCOUNT_EMAIL:$ATLASSIAN_API_TOKEN" --form files=@"/path/to/artifact.exe"hello world Go アプリを作成して、curl によってコンパイルされたバイナリを構築してアップロードする一例をご紹介します。
bitbucket-pipelines.yml
image: golang:1.25.0
pipelines:
default:
- step:
name: Create hello world go app
artifacts:
- main.go
script:
- |
cat > main.go << 'EOF'
package main
func main() { println("Hello, World!") }
EOF
- step:
name: Build
artifacts:
- compiled_app
script:
- go build -o compiled_app main.go
- step:
name: Upload
script:
- FILENAME="compiled_app"
- UPLOAD_URL="https://api.bitbucket.org/2.0/repositories/$BITBUCKET_REPO_OWNER/$BITBUCKET_REPO_SLUG/downloads"
- curl -X POST "$UPLOAD_URL" --user "$ATLASSIAN_ACCOUNT_EMAIL:$ATLASSIAN_API_TOKEN" --form files=@$FILENAME結果
上記の構成の bitbucket-pipelines.yml ファイルをリポジトリのルートに配置する必要があります。ステップが完了すると、アーティファクトはリポジトリの Downloads セクションにアップロードされます。
You can check your bitbucket-pipelines.yml file with our online validator.
この内容はお役に立ちましたか?