촬리의늘솔길

Github Actions CI (gradle) 본문

✍️2023/Cloud

Github Actions CI (gradle)

리촬리 2023. 9. 9. 13:52

일단 build 시 jar 파일이 생성되는 과정만 만들어 둘 것이다.

(이외의 컨벤션은 아직 명확하게 정해둔게 없기 때문에 추후 수정 예정)

Github workflows!


맨 처음 텅 빈 코드부터 짜기에는 어려움이 있으니 깃헙측에서 친절하게 템플릿을 제공한다.

Java With Gradle 이라는 것을 이용하여 configure 해보겠다.

# 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: 
      - "develop" 
      - "main"
  pull_request:
    branches: 
      - "develop" 
      - "main"

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Set up JDK 20
      uses: actions/setup-java@v3
      with:
        java-version: '20'
        distribution: 'temurin'

    - name: Grant execute permission for gradlew
      run: chmod +x gradlew  

    - name: Build with Gradle
      uses: gradle/gradle-build-action@v2
      with:
        arguments: build
        cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/develop' }}

    - name: Execute Gradle build
      run: ./gradlew build

    - name: Upload Apk
      uses: actions/upload-artifact@v3
      with:
        name: build 파일
        path: build/libs/demo-0.0.1-SNAPSHOT-plain.jar

일단 main 과 develop 브랜치에 push 할때마다 build 되도록 CI 작업을 하고자 한다.

  • java 버전은 20이므로 set up jdk 20으로 설정해뒀다.
  • gradle 빌드 명령어(?)
    ```
  • name: Execute Gradle build```
  • run: ./gradlew build
  • 빌드된 파일을 artifact 로 올리는 코드
    ```
  • name: Upload Apk
    build/libs/demo-0.0.1-SNAPSHOT-plain.jar 는 실제로 인텔리제 내에서 빌드했을때 빌드파일이 저장되는 위치를 말한다.
  • uses: actions/upload-artifact@v3 with: name: build 파일 path: build/libs/demo-0.0.1-SNAPSHOT-plain.jar
  • jar 파일 이름 지정하는 법
    build.gradle 에 넣으면된다.
  • bootJar{ archivesBaseName = 'whatssue' archiveFileName = 'whatssue.jar' archiveVersion = "0.0.1" }

트러블 슈팅 (환경변수 설정)

하다보니 문제가 생겼다.

DB 접속 정보가 .gitignore 되어 build가 불가능한 상황

그래서 github 환경변수를 넣을 수 있게 코드를 변경해야함

깃헙 환경변수 설정

gradle build workflow 가 동작할때,microsoft/variable-substitution@v1 을 활용해서

RESOURCE_PATH: ./src/main/resources/application.yml

env 로 지정한 환경변수를 넣어줄 수 있다.

# 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:
      - "develop"
      - "main"
  pull_request:
    branches:
      - "develop"
      - "main"

jobs:
  build:

    runs-on: ubuntu-latest
    env:
      RESOURCE_PATH: ./src/main/resources/application.yml
    steps:
      - uses: actions/checkout@v3
      - name: Set up JDK 20
        uses: actions/setup-java@v3
        with:
          java-version: '20'
          distribution: 'temurin'

      - name: Set yml file
        uses: microsoft/variable-substitution@v1
        with:
          files: ${{ env.RESOURCE_PATH }}
        env:
          spring.datasource.url: ${{ secrets.DB_HOST }}
          spring.datasource.username: ${{ secrets.DB_USERNAME }}
          spring.datasource.password: ${{ secrets.DB_PASSWORD }}

      - name: Grant execute permission for gradlew
        run: chmod +x gradlew

      - name: Build with Gradle
        uses: gradle/gradle-build-action@v2
        with:
          arguments: build
          cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/develop' }}

      - name: Execute Gradle build
        run: ./gradlew build

      - name: Upload Apk
        uses: actions/upload-artifact@v3
        with:
          name: build File
          path: build/libs/whatssue.jar

application.yml 은 다음과 같이 구성했다.

spring:
  datasource:
    username: ${SPRING_DATASOURCE_USERNAME}
    url: ${SPRING_DATASOURCE_URL}
    password: ${SPRING_DATASOURCE_PASSWORD}
  profiles:
    include: mysql
  jpa:
    hibernate:
      ddl-auto: update
server:
  port: '8090'

애를 먹었던 부분이,

github actions 에서의 env 변수 값과

application.yml 변수 값을 어떻게 매칭 해야하는지에 대한 혼란이였는데,

github actions 에서는*

spring.datasource.username : ${{ secrets.깃헙시크릿변수이름 }}

로 선언해두고,

application.yaml 에서는

변수 값같은 경우에는 아무 값 ex) sadlfasd 이 써있어도 상관 없다고 한다.

spring.datasource.username 과 같이 변수 이름만 명시가 잘 되어있으면 된다고 한다.

완성된 빌드 파일은 Actions → workflow 누르면된다.


클릭하면 세부 화면 하단에 빌드 파일 다운가능

참고

https://goodgid.github.io/Github-Action-CI-CD-Workflows/

https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle

https://velog.io/@sontulip/github-actions-ci

https://kotlinworld.com/396

https://monny.tistory.com/273

https://goldenrabbit.co.kr/2023/07/05/github-actions%EC%9C%BC%EB%A1%9C-%EB%B0%B0%ED%8F%AC-%EC%9E%90%EB%8F%99%ED%99%94%ED%95%B4-%EB%B3%B4%EA%B8%B0a-k-a-ci-cd-2%ED%8E%B8/

https://github.com/gradle/gradle-build-action

https://scshim.tistory.com/236

https://aljjabaegi.tistory.com/638

https://velog.io/@marigold1/Github-action-환경변수-설정-해결

728x90