나의 Spring Boot project를 빌드/테스트 과정과 배포과정을 자동화 하기 위해 GitHub Action을 선택했다. Github Action을 통한 자동화는 너무 크게 어렵진 않았다. 하지만 고민거리가 하나 생겼다. application.properties이 파일을 어떻게 관리 할건지에 대해서 고민이 생겼다.빌드하기 위해선 Repository에 꼭 있어야 하는데 properties안에 내용들은 민감정보를 포함하기 때문에 생각을 많이 해야했다.그래서 내린 결론은 민감정보들을 다루는 많은 방법이 있지만 나는 GitHub의 secrets안에 application.properties내용들을 한번에 담고 빌드할때 application.properties를 만들고 secrets안에 담은 내용을 properties파일안에 넣어주는 방식으로 진행했다.
name: Java CI with Gradle
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
- uses: actions/checkout@v3
- run: touch ./src/main/resources/application.properties
- run: echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application.properties
- run: cat ./src/main/resources/application.properties
- name: Build with Gradle
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
with:
arguments: build
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle
name: Java CI with Gradle
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
permissions:
contents: read
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
- uses: actions/checkout@v3
- run: touch ./src/main/resources/application.properties
- run: echo "${{ secrets.APPLICATION }}" > ./src/main/resources/application.properties
- run: cat ./src/main/resources/application.properties
# - uses: actions/upload-artifact@v3
# with:
# name: application.properties
# path: ./src/main/resources/application.properties
- name: Build with Gradle
uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
with:
arguments: build
- name: Docker build
run: |
docker login -u ${{ secrets.USERNAME }} -p ${{ secrets.PASSWORD }}
docker build -t spring-cicd .
docker tag spring-cicd ${{ secrets.USERNAME }}/spring-cicd:${GITHUB_SHA::7}
docker push ${{ secrets.USERNAME }}/spring-cicd:${GITHUB_SHA::7}
- name: Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ec2-user
key: ${{ secrets.PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
docker pull leejasun/spring-cicd:${GITHUB_SHA::7}
docker tag leejasun/spring-cicd:${GITHUB_SHA::7} spring-cicd
docker stop server
docker run -d --rm --name server -p 80:8080 spring-cicd
당신 천재군요