リポジトリにプッシュして戻す
パイプラインからリポジトリを変更する必要がある場合、変更をプッシュ バックすることができます。既定で設定されている http git origin の使用をおすすめします。http は事前構成済みのため、http を使用したプッシュバックはシームレスに機能します。
リポジトリにプッシュする際にコミット メッセージで [skip ci] を使用して、新しいコミットでパイプラインがトリガーされて無限ループが発生するのを防ぎます。
Origin 変数
パイプラインには、http と ssh の origin URL を含む、2 つの既定の変数があります。
${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「サンプル リポジトリへの変更のコミット」で実際の例を確認できます。
新しいタグのプッシュ
バージョン管理にタグを使用する場合、次のようなスクリプトを使用できます。
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}新しいタグのプッシュ用のサンプル リポジトリでデモを確認できます。
代替 Git クライアントの構成
パイプラインで提供される Git クライアントを使用していない場合、以下を構成する必要があります。
${BITBUCKET_GIT_HTTP_ORIGIN} 変数を使用する origin
git クライアントでプロキシを使用 (プロキシ URL は http://localhost:29418)
認証方法によるプッシュ バック
リポジトリでブランチ権限が有効になっていて、初期設定で構成された HTTP origin によってコミット バックできない、または「Bot」アカウントまたは別の認証方法によってコミットする場合は、いくつかのオプションがあります。
OAuth、アプリのシークレット キー、または SSH をアカウントに構成する場合、これらの資格情報を使用したアクセスを許可するリポジトリを制限することはできません。
OAuth
OAuth 認証情報を使用して、ブランチ制限のあるブランチに変更をプッシュすることはできません。
In Bitbucket, select the Settings cog on the top navigation bar, then elect Workspace settings.
Select OAuth consumers, then Add consumer.
新しいクライアント/コンシューマーを作成し、自身のアカウントのリポジトリへの読み取りおよび書き込み権限を付与します。
名前と任意の説明を入力して、コールバック URL を https://bitbucket.org に設定します。
[これは非公開コンシューマーです] チェックボックスを選択します。
Under Permissions, go to Repositories. Tick both Read and Write.
[保存] を選択します。
新しく作成されたコンシューマーに移動してキーを記録します。
CLIENT_ID および CLIENT_SECRET という名前のパイプライン変数としてキーを追加します。
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使用例を OAuth のサンプル リポジトリで確認できます。
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-keygenAdd 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 でエンコードし、パイプライン変数として追加します。
MacOS
cat <keyfile> | base64 | pbcopyLinux
cat <keyfile> | base64 -w0 | xclip -selection clipboardbitbucket-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サンプル リポジトリ: 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.
read:repository:bitbucketとwrite:repository:bitbucketのスコープを持つ API トークンを生成します。以前に生成された API トークンを値として、API_TOKENを Pipelines 設定で安全な変数として定義します。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}サンプル リポジトリ: https://bitbucket.org/bitbucketpipelines/git-auth-appsecret
この内容はお役に立ちましたか?