2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CircleCIのpersist_to_workspaceで複数のジョブからファイルを保存したい

Last updated at Posted at 2019-07-21

課題

ステージング環境と本番環境のビルドを並行で行い、それぞれ別の資材を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

ワークフローを分けたくない場合はexecutorworking_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/*

メモ

  • もっと良い方法がありましたらご教示ください
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?