チームでもGitHub Actionsを使ってみようか、という動きがある。1
Dateオブジェクトに関連するテストケースを単体テストに組み込んでいる。実行環境に応じてテスト結果が変わってしまうため、タイムロケールをJSTに固定したい、という場面があった。
調査してみると、GitHub Actionsで実行環境を操作する場合、envを使えばよいとわかった。このenv設定、対象のステップ(steps)のみとjob全体に設定するパターンで結果が異なる。
対象のステップのみタイムロケールを変える場合
この場合だと、run dateのステップのみタイムロケールがJSTとして設定される(実行例)。
name: Node.js CI on: [push] jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: ["18.x"] steps: - uses: actions/checkout@v3 # ここではタイムゾーンUTCが適用される - name: Check Date run: date - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} cache: 'npm' # ↓はタイムゾーン JSTが適用される - run: date env: TZ: 'Asia/Tokyo' - run: npm ci - run: npm test
job全体にタイムロケールを設定したい場合
stepsの前(job設定)のタイミングで環境変数を設定する(実行例)。
name: Node.js CI on: [push] jobs: build: runs-on: ubuntu-latest strategy: matrix: node-version: ["18.x"] env: TZ: 'Asia/Tokyo' steps: - uses: actions/checkout@v3 # ここはタイムゾーン JSTが適用される - name: Check Date run: date - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} cache: 'npm' # ここもタイムゾーン JSTが適用される - run: date - run: npm ci - run: npm test
まとめ
- タイムロケールはGitHub Actionsでは環境変数として設定する
- 環境変数はenvで説明する
- envをjob全体に効かせるか、そうでないかで設定箇所が変わる
参考URL
- https://docs.github.com/en/actions/learn-github-actions/variables
- https://zenn.dev/blancpanda/articles/github-actions-cron-timezone-jst
- https://zenn.dev/hashito/articles/aef4de448f341b
- 検証したもの
- 今はCircleCIを使っている。↩