Jenkins から Bitbucket Pipelines に移行する方法

Bitbucket Cloud を使用すると、オンプレミスの複数の CI/CD ツールに柔軟に接続できます。ただし、Bitbucket Cloud を使用してコードを管理している場合、CI/CD パイプラインを Bitbucket Pipelines に移行すると、全体的な CI/CD エクスペリエンスが向上します。

Bitbucket Pipelines は Bitbucket Cloud に組み込まれているため、連携を管理したり、インストールが必要なプラグインはありません。アトラシアン クラウド ランナーを使用してオンデマンドでスケーリング (または独自のランナーに接続) し、パイプを使用してワークフローを簡単に拡張し、コンプライアンスのニーズに合わせてパイプラインを大規模に管理できます。Bitbucket Pipelines に移行するメリットの詳細をご確認ください。

構文の違いに関するガイドと、Jenkins からの移行プランに関するガイドラインをご紹介します。

構文例

以下では、Jenkins から Bitbucket Pipelines への移行に関するいくつかの例と構文比較を示します。

Hello World ビルドの実行

Jenkins (groovy)

パイプライン (yaml)

1 2 3 4 5 6 7 8 9 10 pipeline { agent any stages { stage('Example') { steps { echo 'Hello World' } } } }
1 2 3 4 5 6 7 image: atlassian/default-image:4 pipelines: default: - step: name: Example script: - echo 'Hello World'

並列ジョブの実行

Jenkins (groovy)

パイプライン (yaml)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 pipeline { agent any stages { stage('Parallel Steps') { parallel { stage('Build and Test') { steps { echo "Your build and test goes here..." } } stage('Lint') { steps { echo "Your linting goes here..." } } stage('Security scan') { steps { echo "Your security scan goes here..." } } } } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 image: atlassian/default-image:4 pipelines: default: - parallel: - step: name: 'Build and Test' script: - echo "Your build and test goes here..." - step: name: 'Lint' script: - echo "Your linting goes here..." - step: name: 'Security scan' script: - echo "Your security scan goes here..."

環境変数の参照

Jenkins (groovy)

パイプライン (yaml)

1 2 3 4 5 6 7 8 9 10 pipeline { agent any stages { stage('Example') { steps { echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}" } } } }
1 2 3 4 5 6 7 image: atlassian/default-image:4 pipelines: default: - step: name: Example script: - echo "Running $BITBUCKET_BUILD_NUMBER on $BITBUCKET_GIT_HTTP_ORIGIN"

資格情報の処理

次のパイプライン コードは、シークレット テキストの資格情報に環境変数を使用してパイプラインを作成する方法の例を示しています。

Jenkins (groovy)

パイプライン (yaml)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 pipeline { agent any environment { AWS_ACCESS_KEY_ID = credentials('jenkins-aws-secret-key-id') AWS_SECRET_ACCESS_KEY = credentials('jenkins-aws-secret-access-key') } stages { stage('Example stage 1') { steps { // logic required credentials } } } }
1 2 3 4 5 6 7 8 9 10 image: atlassian/default-image:4 pipelines: default: - step: name: Example script: - pipe: atlassian/bitbucket-upload-file:0.7.1 variables: BITBUCKET_ACCESS_TOKEN: $BITBUCKET_ACCESS_TOKEN FILENAME: 'package.json'

タイムアウトを長くする

Jenkins (groovy)

パイプライン (yaml)

1 2 3 4 5 6 7 8 9 10 11 12 13 pipeline { agent any stages { stage('Example') { options { timeout(time: 1, unit: 'HOURS') } steps { echo 'Hello World' } } } }
1 2 3 4 5 6 7 8 image: atlassian/default-image:4 pipelines: default: - step: name: Example script: - echo 'Hello World' max-time: 60 # Timeout in minutes

cron 式

Jenkins (groovy)

Pipelines

1 2 3 4 5 6 7 8 9 10 11 12 13 pipeline { agent any triggers { cron('H 0 * * *') // This cron expression triggers the pipeline once every day at midnight } stages { stage('Example') { steps { echo 'Hello World' } } } }

Bitbucket Pipelines のスケジュールされたビルドは、ユーザー インターフェイスで構成されます。ビルド スケジュールを簡単に設定および管理し、ビルドの頻度とタイミングを指定できます。

ビルド、テスト、デプロイ

Jenkins (groovy)

パイプライン (yaml)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 pipeline { agent any tools {nodejs "nodejs"} environment { HEROKU_API_KEY = credentials('jenkins-heroku-api-key') HEROKU_APP_NAME = credentials('jenkins-heroku-app-name') } stages { stage('Build') { steps { sh 'npm install' sh 'npm run build' sh 'zip -r build.zip build/ } } stage('Test') { steps { sh 'npm test' } } stage('Deploy to Heroku') { steps { script { def deploymentUrl = "https://api.heroku.com/sources" def response = sh(script: """ curl -X POST \ -H "Content-Type: application/json" \ -H "Accept: application/vnd.heroku+json; version=3" \ -H "Authorization: Bearer ${HEROKU_API_KEY}" \ "${deploymentUrl}" """, returnStdout: true).trim() def jsonSlurper = new groovy.json.JsonSlurper() def parsedResponse = jsonSlurper.parseText(response) def putUrl = parsedResponse.source_blob.put_url def getUrl = parsedResponse.source_blob.get_url sh """ curl "${putUrl}" \ -X PUT \ -H "Content-Type:" \ --data-binary @build.zip """ sh """ curl -X POST \ -H "Content-Type: application/json" \ -H "Accept: application/vnd.heroku+json; version=3" \ -H "Authorization: Bearer ${HEROKU_API_KEY}" \ -d '{"source_blob":{"url":"${getUrl}","version":"${BUILD_NUMBER}"}}' \ "https://api.heroku.com/apps/${HEROKU_APP_NAME}/builds" """ } } } } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 image: atlassian/default-image:4 pipelines: default: - step: name: Build script: - npm install - npm run build - zip -r build.zip build/ artifacts: - build.zip - step: name: Test caches: - node script: - npm install - npm test - step: name: Deploy to Heroku services: - docker script: - pipe: atlassian/heroku-deploy:2.4.0 variables: HEROKU_API_KEY: $HEROKU_API_KEY HEROKU_APP_NAME: $HEROKU_APP_NAME ZIP_FILE: 'build.zip' WAIT: 'true'

移行のベスト プラクティス

ミッション クリティカルなワークフローの場合、Jenkins から Bitbucket Pipelines への移行を段階的にロールアウトすることをお勧めします。この戦略では、必要に応じて Jenkins を活用しながら、既存のリポジトリを徐々に Bitbucket Pipelines に移行できるため、Bitbucket Pipelines へのスムーズかつ効率的な移行を確実に行うことができます。

まず、重要度の低いリポジトリまたはビルド プロセスの一部を Bitbucket Pipelines に移行することで、構成をテストして調整できます。この移行期間中は、jenkins-job-trigger パイプを使用して Jenkins ジョブをトリガーするように Bitbucket Pipelines を設定できます。このハイブリッド アプローチにより、チームは徐々に Bitbucket Pipelines に慣れ、進行中の開発プロセスを中断することなく移行の課題に対処できます。

以下は、Pipelines から基本的な Jenkins ジョブをトリガーする方法の例です。

1 2 3 4 5 6 7 script: - pipe: atlassian/jenkins-job-trigger:0.8.0 variables: JENKINS_URL: 'http://my-jenkinsio-host:8080/job' JENKINS_USER: $JENKINS_USER JENKINS_TOKEN: $JENKINS_TOKEN JOB_NAME: 'awesome-project-job'

以下の例では、Jenkins ジョブ ビルドをトリガーし、ビルドの完了を待機します。

ワークフローには十分なWAIT_MAX_TIME時間を設定してください。

1 2 3 4 5 6 7 8 9 script: - pipe: atlassian/jenkins-job-trigger:0.8.0 variables: JENKINS_URL: 'http://my-jenkinsio-host/job' JENKINS_USER: $JENKINS_USER JENKINS_TOKEN: $JENKINS_TOKEN JOB_NAME: 'staging-awesome-project-job' WAIT: 'true' WAIT_MAX_TIME: 120

さらに、パイプと呼ばれる強力な CI/CD 統合により、CI/CD ワークフローを強化してボイラープレート コードを最小限に抑えることをお勧めします。厳選された 100+ 本のパイプのリストから選択するか、組織内で独自のカスタム ソリューションを作成して共有します。

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

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