LoginSignup
0
0

More than 3 years have passed since last update.

CircleCIで環境変数(Environment Valuables, Context)が効かない

Posted at

CircleCIで環境変数が効かない、環境変数が反映されない、という地味な現象にハマりましたが、原因はconfig.yaml内でsudo利用時に環境変数引き継ぎオプション-Eを指定していなかったという残念なものでした。
検索しても引っかからなかったので自分のためにメモ。

修正前

sudo yarn deploy       

修正後

sudo -E yarn deploy       

config.yaml全体

version: 2.1

references:
  ignore_master: &ignore_master
    filters:
      branches:
        ignore:
          - master

orbs:
  aws-cli: circleci/aws-cli@0.1.13

executors:
  default:
    working_directory: ~/repo
    docker:
      - image: circleci/node:10.15.3-browsers

commands:
  prepare-aws-cli:
    description: 'AWS CLIを準備'
    steps:
      - aws-cli/install
      - aws-cli/configure

  prepare-resources:
    description: 'リソースを準備'
    steps:
      - checkout

      - run:
          name: Install Yarn
          command: sudo npm install -g yarn@1.6.0

      - restore_cache:
          keys:
            - v2-dependencies-{{ checksum "package.json" }}-{{ checksum "serverless.yml" }}-{{ checksum "yarn.lock" }}
            - v2-dependencies-

      - run:
          name: Install dependencies
          command: |
            # yarn.lockの内容をすべてインストール
            yarn install --frozen-lockfile

      - save_cache:
          paths:
            - node_modules
          key: v2-dependencies-{{ checksum "package.json" }}-{{ checksum "serverless.yml" }}-{{ checksum "yarn.lock" }}

jobs:
  lint-and-test:
    executor:
      name: default

    steps:
      - prepare-resources

      - run: yarn lint

      - run:
          name: Format checking
          command: |
            yarn format;
            git diff --exit-code --quiet;
            if [ $? = 1 ]; then
              echo 'Unformat file(s) found.';
              exit 1
            fi

      - run: yarn test:coverage

      - store_artifacts:
          path: ~/repo/coverage/lcov-report/

  build:
    executor:
      name: default

    steps:
      - prepare-resources

      - prepare-aws-cli

      - run:
          name: Build Nuxt resources
          command: |
            if [ $ALIS_APP_ID ]; then
              sudo -E yarn build
            fi

      - persist_to_workspace:
          root: .
          paths:
            - .nuxt/*

  deploy:
    executor:
      name: default

    steps:
      - prepare-resources

      - prepare-aws-cli

      - attach_workspace:
          at: .

      - deploy:
          name: Deployment
          command: |
            if [ $ALIS_APP_ID ]; then
              sudo npm install -g serverless
              sudo -E yarn deploy
            fi

workflows:
  main:
    jobs:
      - lint-and-test
      - build:
          <<: *ignore_master
      - deploy:
          # ステージング環境、本番環境へのデプロイはCodeBuildに委ねる
          <<: *ignore_master
          requires:
            - lint-and-test
            - build

0
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
0
0