Bitbucket is getting a new navigation

We’re rolling out these changes, so the documentation may not match your experience in the Bitbucket Cloud app. Read about the new Bitbucket navigation

リポジトリにプッシュして戻す

パイプラインからリポジトリを変更する必要がある場合、変更をプッシュ バックすることができます。既定で設定されている http git origin の使用をおすすめします。http は事前構成済みのため、http を使用したプッシュバックはシームレスに機能します。

リポジトリにプッシュする際にコミット メッセージで [skip ci] を使用して、新しいコミットでパイプラインがトリガーされて無限ループが発生するのを防ぎます。

Origin 変数

There are 2 default variables in pipelines, to contain the HTTP and ssh origin URLs:

  • ${BITBUCKET_GIT_HTTP_ORIGIN}

  • ${BITBUCKET_GIT_SSH_ORIGIN}

これらは、スクリプトや、パイプラインで呼び出される他のツールに origin を渡す必要がある場合に使用できます。

プッシュの例

ブランチへの変更のコミット

パイプライン内からリポジトリを動的に変更する必要がある場合、次のようなスクリプトを使用できます。

pipelines: default: - step: script: - echo "Made a change in build ${BITBUCKET_BUILD_NUMBER}" >> changes.txt - git add changes.txt - git commit -m "[skip ci] Updating changes.txt with latest build number." - git push

You can see this in action in the committing changes example repository.

新しいタグのプッシュ

バージョン管理にタグを使用する場合、次のようなスクリプトを使用できます。

pipelines: default: - step: script: - echo "Made a change in build ${BITBUCKET_BUILD_NUMBER}" >> changes.txt - git add changes.txt - git commit -m "Updating changes.txt with latest build number." - git tag -am "Tagging for release ${BITBUCKET_BUILD_NUMBER}" release-${BITBUCKET_BUILD_NUMBER} - git push origin release-${BITBUCKET_BUILD_NUMBER}

This is demonstrated in pushing a new tag example repository.

代替 Git クライアントの構成

パイプラインで提供される Git クライアントを使用していない場合、以下を構成する必要があります。

  • ${BITBUCKET_GIT_HTTP_ORIGIN}  変数を使用する origin

  • git クライアントでプロキシを使用 (プロキシ URL は http://localhost:29418)

認証方法によるプッシュ バック

リポジトリでブランチ権限が有効になっていて、初期設定で構成された HTTP origin によってコミット バックできない、または「Bot」アカウントまたは別の認証方法によってコミットする場合は、いくつかのオプションがあります。

OAuth、アプリのシークレット キー、または SSH をアカウントに構成する場合、これらの資格情報を使用したアクセスを許可するリポジトリを制限することはできません。

OAuth

OAuth 認証情報を使用して、ブランチ制限のあるブランチに変更をプッシュすることはできません。

  1. In Bitbucket, select the Settings cog on the top navigation bar, then elect Workspace settings.

  2. Select OAuth consumers, then Add consumer.

  3. 新しいクライアント/コンシューマーを作成し、自身のアカウントのリポジトリへの読み取りおよび書き込み権限を付与します。

  4. Provide a name, optional description, and set the callback URL to https://bitbucket.org.

  5. [これは非公開コンシューマーです] チェックボックスを選択します。

  6. Under Permissions, go to Repositories. Tick both Read and Write.

  7. [保存] を選択します。

  8. 新しく作成されたコンシューマーに移動してキーを記録します。

  9. Add the keys as pipelines variables with the names CLIENT_ID and CLIENT_SECRET.

  10. bitbucket-pipelines.yml ファイルのスクリプト セクションに以下を追加します。

# Get an oauth access token using the client credentials, parsing out the token with jq. - apt-get update && apt-get install -y curl jq - > export access_token=$(curl -s -X POST -u "${CLIENT_ID}:${CLIENT_SECRET}" \ https://bitbucket.org/site/oauth2/access_token \ -d grant_type=client_credentials -d scopes="repository"| jq --raw-output '.access_token') # Configure git to use the oauth token. - git remote set-url origin "https://x-token-auth:${access_token}@bitbucket.org/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}" # Make changes and commit back. - echo "Made a change in build ${BITBUCKET_BUILD_NUMBER}" >> changes.txt - git add changes.txt - git commit -m "[skip ci] Updating changes.txt with latest build number." - git push

You can see this used in the OAuth example repo.

Bitbucket Pipelines で管理される SSH キー ペア

  • In Bitbucket, from your repository select Repository settings

  • Under Pipelines, select SSH Keys, then Generate keys

  • 公開キーをクリップボードにコピーします。

  • Select the Settings cog on the top navigation bar. Select Personal Bitbucket settings, then SSH Keys

  • Select Add Key, and paste the public key, providing a useful name and description

bitbucket-pipeline.yml ファイルで ssh origin を使用するように git を構成する必要があります。次のコマンドを実行します。

git remote set-url origin ${BITBUCKET_GIT_SSH_ORIGIN}

これで、追加構成を行うことなく、パイプライン内で SSH 操作を実行できるようになりました。

https://bitbucket.org/bitbucketpipelines/git-auth-ssh-configured-in-ui/

変数で管理される SSH キー ペア

この方法は、これまでの手順で SSH キーを構成していない場合にのみ、すぐに使用できる状態で提供されます。

  • コマンド ラインで、公開/非公開のキー ペアを生成します。

ssh-keygen
  • Add the public key to your account in Bitbucket UI, select the Settings cog on the top navigation bar. Select Personal Bitbucket settings, then SSH Keys

  • Base64 encode the private key, and add it as a pipelines variable:

MacOS

cat <keyfile> | base64 | pbcopy

Linux

cat <keyfile> | base64 -w0 | xclip -selection clipboard

bitbucket-pipeline.yml スクリプトで次のコマンドを実行し、ssh origin を使用するように git を構成して、ssh 非公開キーを適切な場所にコピーします。

- git remote set-url origin ${BITBUCKET_GIT_SSH_ORIGIN} - echo $PRIVATE_KEY > ~/.ssh/id_rsa.tmp - base64 -d ~/.ssh/id_rsa.tmp > ~/.ssh/id_rsa - chmod 600 ~/.ssh/id_rsa

Example repo: https://bitbucket.org/bitbucketpipelines/git-auth-ssh-using-variables

apiToken

  • In Bitbucket, select the Setting cog on the top navigation bar and then Atlassian account settings. Select Security, then Create and manage API tokens.

  • Generate an API token with read:repository:bitbucket and write:repository:bitbucket scopes. Define API_TOKEN as a secure variable in Pipelines settings with the previously generated API token as value.

  • bitbucket-pipelines.yml ファイルで、Bitbucket のユーザー名と API トークンで認証するようにリモート URL を変更し、git で API トークンを使用するように構成します。

git remote set-url origin https://<your username>:${API_TOKEN}@bitbucket.org/${BITBUCKET_REPO_OWNER}/${BITBUCKET_REPO_SLUG}

Example repository: https://bitbucket.org/bitbucketpipelines/git-auth-appsecret

 

さらにヘルプが必要ですか?

アトラシアン コミュニティをご利用ください。