課題
ステージング環境と本番環境のビルドを並行で行い、それぞれ別の資材をpersist_to_workspace
で保存したい場合があります。現状、persist_to_workspace
ではエイリアス等は付けられません。
対応法1
ワークフローを2つに分ける。
こういった場合はワークフローを分けてしまうのが良いかと思います。異なる環境へのデプロイにpersist_to_workspace
を2つのジョブで使用したい場合、おそらくそのワークフローは依存関係が複雑になりすぎています。
特に本番環境が絡む場合は事故の元なので、ここは多少冗長であってもワークフローを分けてしまうべきでしょう。ワークフローをまたいだpersist_to_workspace
による資材の受け渡しは不可能なので、ワークフローを分けることで依存関係は明示的となります。
workflows:
# 本番環境以外のワークフロー
staging-and-others:
jobs:
- lint-and-test
- build:
context: common_staging_deployment
# ステージング環境へのデプロイ
- deploy:
name: deploy-staging
<<: *only_master
context: common_staging_deployment
requires:
- lint-and-test
- build
# 本番環境のワークフロー
production:
jobs:
- lint-and-test:
name: lint-and-test-production
<<: *production_jobs
- build:
name: build-production
<<: *production_jobs
# 承認処理
- notify-to-slack-for-approval:
<<: *production_jobs
requires: &test-and-build-production
- lint-and-test-production
- build-production
- approval:
<<: *production_jobs
type: approval
requires: *test-and-build-production
- deploy:
name: deploy-production
<<: *production_jobs
requires:
- approval
対応法2
ワークフローを分けたくない場合はexecutor
のworking_directory
を分ける手もあります。
executors:
default:
working_directory: ~/repo
docker:
- image: circleci/node:10.15.3-browsers
production:
# 本番環境では、persist_to_workspaceで保存されるワークスペースを独自のパスとするためにworking_directoryを変えている
working_directory: ~/repo_production
docker:
- image: circleci/node:10.15.3-browsers
executor
を上記のように設定すれば、面倒なことを考えずにpersist_to_workspace
を使用できます。
build-staging:
executor:
name: default
steps:
- prepare-deployment
- run:
name: "Build Nuxt resources"
command: |
mkdir .nuxt
if [ $ALIS_APP_ID ]; then
yarn build
fi
- persist_to_workspace:
root: .
paths:
- .nuxt/*
build-production:
executor:
name: production
steps:
- prepare-deployment
- run: yarn build
- persist_to_workspace:
root: .
paths:
- .nuxt/*
メモ
- もっと良い方法がありましたらご教示ください