0
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 3 years have passed since last update.

Actions secretsをSpring Bootのapplication.propertiesまで渡す方法

0
Last updated at Posted at 2022-07-22

はじめに

GitHub Actions上でdockerコンテナを起動し、Spring Bootのアプリケーションを動かす場合があります。
そのとき、Actions secretsに登録しておいたバリューをSpring Bootのapplication.propertiesまで渡す方法について説明します。

secretを登録

レポジトリのSettingsでシークレットを登録します。
(例)
image.png
↓登録したシークレットはリストで確認できます。
中身は見えませんので、間違った値を登録してしまったら「Update」で更新しましょう。
image.png

ワークフローファイルにsecretを設定

登録したシークレットをワークフロー内で環境変数として設定します。
secretsコンテキストを使って、シークレットにアクセスすることができます。

 test:

    runs-on: ubuntu-20.04
    env:
      GOOGLE_CLIENT_SECRET: ${{ secrets.GOOGLE_CLIENT_SECRET }}
      GOOGLE_CLIENT_ID: ${{ secrets.GOOGLE_CLIENT_ID }}
      LINE_CLIENT_SECRET: ${{ secrets.LINE_CLIENT_SECRET }}
      LINE_CLIENT_ID: ${{ secrets.LINE_CLIENT_ID }}
    steps:
    - uses: actions/checkout@v2

    - run: docker-compose -f docker-compose-gitaction.yml build
    - run: docker-compose -f docker-compose-gitaction.yml up --abort-on-container-exit

docker-compose.ymlでenvironmentとして設定

ワークフローで設定した環境変数をdocker-compose.ymlのenvironmentに設定します。

version: '3.8'
services:
  web_test:
    container_name: 'web_test'
    build:
      context: .
    tty: true
    working_dir: '/gongbu/home'
    ports: 
      - "8080:8080"
    environment:
      - GOOGLE_CLIENT_SECRET=${GOOGLE_CLIENT_SECRET}
      - GOOGLE_CLIENT_ID=${GOOGLE_CLIENT_ID}
      - LINE_CLIENT_SECRET=${LINE_CLIENT_SECRET}
      - LINE_CLIENT_ID=${LINE_CLIENT_ID}

コンテナ内の環境変数をapplication.propertiesで利用

Spring Bootにて環境変数をapplication.propertiesに設定する方法は${環境変数名}です。
で、上記のdocker-compose実行時、設定した環境変数を書き込んだら終わりです。

spring.security.oauth2.client.registration.google.client-secret=${GOOGLE_CLIENT_SECRET}
spring.security.oauth2.client.registration.google.client-id=${GOOGLE_CLIENT_ID}
spring.security.oauth2.client.registration.line.client-secret=${LINE_CLIENT_SECRET}
spring.security.oauth2.client.registration.line.client-id=${LINE_CLIENT_ID}

参考サイト

私のGitHubリポジトリ
workflow ci_cd.yml
docker-compose-gitaction.yml
application-test.properties

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?