๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Trouble Shooting

GitHub Action - Build ์‹คํŒจ: Spring Boot profile

by ์ฃผ๋ฐœ2 2021. 6. 22.
๋ฐ˜์‘ํ˜•

๐Ÿ“Ž  GitHub Action Build ์‹คํŒจ : Spring Boot profile

 

ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์—์„œ ๊ฐœ๋ฐœ & ์šด์˜๊ณ„๋กœ properties ํŒŒ์ผ์„ ๋ถ„๋ฆฌํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๊ธฐ์กด์— application.properties ์— ๋ชจ๋‘ ํฌํ•จ๋˜์–ด ์žˆ๋˜ ํ™˜๊ฒฝ ๋ณ€์ˆ˜(DB์ •๋ณด)๋“ค์„ ๊ฐœ๋ฐœ(dev) & ์šด์˜(prod) ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ์— ๋”ฐ๋ผ ์ง€๋‚œ๋ฒˆ์— ์ ์šฉํ•œ GitHub Action์„ ํ†ตํ•ด Build๊ฐ€ ์‹คํŒจํ•˜๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

 

Database์˜ ์ •๋ณด๋ฅผ ์ฝ์–ด์˜ฌ ์ˆ˜ ์—†์œผ๋‹ˆ Build๋„ ๋‹น์—ฐํžˆ ์‹คํŒจํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ github-action.yml ํŒŒ์ผ์˜ Maven build ๊ณผ์ •์˜ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

 

 

 

-Pprod: profile๋กœ prod๋ฅผ ์‚ฌ์šฉ

์ฝ”๋“œ์— ์ž‘์„ฑํ•œ Maven ์˜ต์…˜์— ๋Œ€ํ•ด ๊ฐ„๋žตํžˆ ์„ค๋ช…๋“œ๋ฆฌ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • -B : ๋น„ ๋Œ€ํ™”์‹ (์ผ๊ด„ ์ฒ˜๋ฆฌ) ๋ชจ๋“œ์—์„œ ์‹คํ–‰
  • package : ์ปดํŒŒ์ผ ์ˆ˜ํ–‰ ๋ฐ ์ปดํŒŒ์ผํ•œ ์†Œ์Šค๋ฅผ packaging ์–‘์‹(war ํ˜น์€ jar)์— ๋งž์ถฐ ํ”„๋กœ์ ํŠธ ๋‚ด ์ง€์ •ํ•œ ๊ฒฝ๋กœ์˜ ๋””๋ ‰ํ† ๋ฆฌ์— ์ƒ์„ฑ
  • --file : POM ํŒŒ์ผ(๋˜๋Š” pom.xml ํŒŒ์ผ์ด ์กด์žฌํ•˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ)๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •

(mvnw๊ณผ ./mvnw ์˜ ๊ฒฝ๋กœ๋„ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๊ณ  ๋‚˜์™€์„œ mvn์œผ๋กœ ํ•ด๊ฒฐ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.. ๐Ÿ˜ญ)

 

 

ํ˜„์žฌ pom.xml ์€ back ๋””๋ ‰ํ† ๋ฆฌ์˜ ํ•˜์œ„์— ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— back/pom.xml ์œผ๋กœ ์„ค์ •์„ ์žก์•„์ค๋‹ˆ๋‹ค.

๊ทธ ํ›„ -Pprod ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด profile์„ prod๋กœ ์„ค์ •ํ•˜๋ฉด,

application-prod.properties ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋•Œ๋ฌธ์— ์ •์ƒ์ ์œผ๋กœ Build๊ฐ€ ์„ฑ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

 

 

 

์ „์ฒด ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

pom.xml 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.4.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.github.oneline</groupId>
	<artifactId>oneline-course</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>oneline-course</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>11</java.version>
	</properties>

	<dependencies>
        ... ์ƒ๋žต
	</dependencies>

	<!-- profile ์ถ”๊ฐ€: dev, prod -->
	<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<environment>dev</environment>
				<maven.test.skip>true</maven.test.skip>
			</properties>
		</profile>
		<profile>
			<id>prod</id>
			<properties>
				<environment>prod</environment>
				<maven.test.skip>true</maven.test.skip>
			</properties>
		</profile>
	</profiles>

	<!-- profile ์ˆ˜์ • -->
	<build>
		<sourceDirectory>src/main/java</sourceDirectory>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources-${environment}</directory>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

 

 

 

github-action.yml

# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Oneline-GitHub Action

on: # ์ด๋ฒคํŠธ ๊ฐ์ง€, ์›Œํฌํ”Œ๋กœ์šฐ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ์กฐ๊ฑด ์„ค์ •
  push:
    branches: [ main ] # push ๋˜๋ฉด ์‹คํ–‰๋  ๋ธŒ๋žœ์น˜ ์„ ํƒ(main)
  pull_request:
    branches: [ main ] # push ๋˜๋ฉด ์‹คํ–‰๋  ๋ธŒ๋žœ์น˜ ์„ ํƒ(main)

jobs: # job - ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค์—์„œ ์—ฌ๋Ÿฌ Step์„ ๊ทธ๋ฃน์‹œ์ผœ ์‹คํ–‰ํ•˜๋Š” ์—ญํ• , ๋ณ‘๋ ฌ๋กœ ์‹คํ–‰
  build:
    runs-on: ubuntu-latest

    steps: # ์ˆœ์ฐจ์ ์œผ๋กœ ๋ช…๋ น์–ด ์ˆ˜ํ–‰

      # Caching dependencies
      - name: Cache Maven packages
        uses: actions/cache@v2
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2

      - name: Checkout
        uses: actions/checkout@v2

      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'

      # build
      - name: Build with Maven
        run: mvn -B package --file back/pom.xml -Pprod

      # time
      - name: Get Current Time
        uses: 1466587594/get-current-time@v2
        id: current-time
        with:
          format: YYYY-MM-DDTHH:mm:ss
          utcOffset: "+09:00" # ๊ธฐ์ค€์ด UTC ์ด๋ฏ€๋กœ ํ•œ๊ตญ์‹œ๊ฐ„์ธ KST ๋กœ ๋งž์ถ”๊ธฐ ์œ„ํ•ด 9์‹œ๊ฐ„์„ ๋”ํ•ด์ค€๋‹ค

      # print time
      - name: Print Current Time
        run: echo "Current Time=${{steps.current-time.outputs.formattedTime}}" # current-time ์—์„œ ์ง€์ •ํ•œ ํฌ๋งท๋Œ€๋กœ ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅ
        shell: bash

 

 

References

 

 

 

 

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€