Search

GitHub Actions

GitHub Actions

์ฝ”๋“œ๋ฅผ ํ˜ธ์ŠคํŒ…ํ•˜๊ณ  ๋ฒ„์ „ ๊ด€๋ฆฌ๋ฅผ ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋™ํ™” ๋ฐ CI/CD(Continuous Integration/Continuous Deployment) ๋„๊ตฌ

Workflow (์›Œํฌํ”Œ๋กœ์šฐ)

GitHub Actions์—์„œ ์ˆ˜ํ–‰๋˜๋Š” ์ž๋™ํ™” ํ”„๋กœ์„ธ์Šค๋ฅผ ์ •์˜ํ•˜๋Š” ์š”์†Œ์ž…๋‹ˆ๋‹ค.
โ€ข
.github/workflows ๋””๋ ‰ํ† ๋ฆฌ์— YAML ํ˜•์‹์˜ ์›Œํฌํ”Œ๋กœ์šฐ ํŒŒ์ผ์„ ์ž‘์„ฑํ•˜์—ฌ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
โ€ข
์›Œํฌํ”Œ๋กœ์šฐ๋Š” ์ด๋ฒคํŠธ(์˜ˆ: ํ‘ธ์‹œ, ํ’€ ๋ฆฌํ€˜์ŠคํŠธ ๋“ฑ)๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ์‹œ์ž‘๋˜๋ฉฐ, ์ผ๋ จ์˜ ์ž‘์—… ๋‹จ๊ณ„๋ฅผ ์ •์˜ํ•˜์—ฌ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

Workflow ์˜ ๊ตฌ์„ฑ์š”์†Œ

GitHub Actions๋Š” ์•„๋ž˜์˜ ์š”์†Œ๋“ค์„ ์กฐํ•ฉํ•˜์—ฌ ์ฝ”๋“œ ๋นŒ๋“œ, ํ…Œ์ŠคํŠธ, ๋ฆด๋ฆฌ์Šค, ๋ฐฐํฌ ๋“ฑ์˜ ์ž‘์—…์„ ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Workflow

โ€ข
Job (์žก)
โ—ฆ
Step (์Šคํ…)
โ–ช
Actions (์•ก์…˜)

Job (์žก)

workflow(์›Œํฌํ”Œ๋กœ์šฐ) ๋‚ด์—์„œ ์‹คํ–‰๋˜๋Š” ๋…๋ฆฝ์ ์ธ ์ž‘์—… ๋‹จ์œ„
โ€ข
๊ฐ Job(์žก)์€ ๋ณ„๋„์˜ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๋ฉฐ, ๋ณ‘๋ ฌ ๋˜๋Š” ์ˆœ์ฐจ์ ์œผ๋กœ ์‹คํ–‰๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
๊ฐ Job(์žก)์€ ์—ฌ๋Ÿฌ ์Šคํ…์œผ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ํ•˜๋‚˜์˜ ์žก์ด ์‹คํŒจํ•˜๋ฉด ๋‹ค์Œ ์žก์ด ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

Step (์Šคํ…)

Job(์žก) ๋‚ด์—์„œ ์ˆ˜ํ–‰๋˜๋Š” ๊ฐœ๋ณ„์ ์ธ ์ž‘์—… ๋‹จ๊ณ„
โ€ข
๊ฐ ์Šคํ…์€ ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฉฐ, ์•ž์˜ ์Šคํ…์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋˜์–ด์•ผ ๋‹ค์Œ ์Šคํ…์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
โ€ข
์›ํ•˜๋Š” ์ž‘์—…์„ ์„ธ๋ถ„ํ™”ํ•˜์—ฌ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Actions (์•ก์…˜)

Step(์Šคํ…) ๋‚ด์—์„œ ์‹คํ•ด์˜ค๋””๋Š” ์žฌ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ๋นŒ๋“œ ๋ธ”๋ก
์•ก์…˜์€ ์›Œํฌํ”Œ๋กœ์šฐ์—์„œ ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
GitHub Marketplace ๋ฐ ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ๋‹ค์–‘ํ•œ ์•ก์…˜์„ ์ฐพ๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
์›Œํฌํ”Œ๋กœ์šฐ ํŒŒ์ผ์—์„œ ์•ก์…˜์„ ํ˜ธ์ถœํ•˜์—ฌ ์ฝ”๋“œ ๋นŒ๋“œ, ํ…Œ์ŠคํŠธ, ๋ฐฐํฌ ๋“ฑ์˜ ์ž‘์—…์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

WorkFlow ์„ค์ •ํŒŒ์ผ

./gitbub/workflows/main.yml
./gitbub/workflows/main.yml ์€ workflow ์˜ ์„ค์ •ํŒŒ์ผ์ž…๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ, Job, Step, Actions ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

workflow ์„ค์ •ํŒŒ์ผ ์˜ˆ์‹œ

โ€ข
main.yml
name: CI/CD Pipeline # ์›Œํฌํ”Œ๋กœ์šฐ์˜ ์ด๋ฆ„ on: push: # ํ‘ธ์‹œ ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ branches: - main # main ๋ธŒ๋žœ์น˜์— ํ‘ธ์‹œ ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ ์›Œํฌํ”Œ๋กœ์šฐ ์‹คํ–‰ jobs: build: runs-on: ubuntu-latest # ๋Ÿฌ๋„ˆ ํ™˜๊ฒฝ์„ ubuntu-latest ์ง€์ •, # runs-on: self-hosted # ์ž์‹ ์˜ ์„œ๋ฒ„์—์„œ ์ง์ ‘ ํ˜ธ์ŠคํŒ…ํ•˜๋Š” ๊ฒฝ์šฐ steps: - name: Checkout Repository uses: actions/checkout@v2 # GitHub์—์„œ ์ œ๊ณตํ•˜๋Š” Checkout ์•ก์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์ฒดํฌ์•„์›ƒ - name: Setup Java uses: actions/setup-java@v2 # Java ํ™˜๊ฒฝ ์„ค์ • ์•ก์…˜ ์‚ฌ์šฉ with: distribution: 'adopt' # AdoptOpenJDK๋ฅผ ์‚ฌ์šฉ java-version: '11' # Java 11 ๋ฒ„์ „ ์‚ฌ์šฉ - name: Build with Gradle run: ./gradlew clean build # Gradle ๋นŒ๋“œ ๋ช…๋ น ์‹คํ–‰ - name: Archive Artifacts uses: actions/upload-artifact@v2 # GitHub์—์„œ ์ œ๊ณตํ•˜๋Š” ์•„ํ‹ฐํŒฉํŠธ ์—…๋กœ๋“œ ์•ก์…˜ ์‚ฌ์šฉ with: name: app-artifacts # ์—…๋กœ๋“œํ•œ ์•„ํ‹ฐํŒฉํŠธ์˜ ์ด๋ฆ„ path: build/libs # ์—…๋กœ๋“œํ•  ๋””๋ ‰ํ† ๋ฆฌ ์ง€์ • deploy: needs: build # deploy ์žก์€ build ์žก์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋˜์–ด์•ผ ํ•จ runs-on: ubuntu-latest # ๋ฐฐํฌ ์žก์ด ์‹คํ–‰๋  ๋Ÿฌ๋„ˆ ํ™˜๊ฒฝ์„ ubuntu-latest๋กœ ์ง€์ • steps: - name: Checkout Repository uses: actions/checkout@v2 # GitHub์—์„œ ์ œ๊ณตํ•˜๋Š” Checkout ์•ก์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ์ฒดํฌ์•„์›ƒ - name: Download Artifacts uses: actions/download-artifact@v2 # GitHub์—์„œ ์ œ๊ณตํ•˜๋Š” ์•„ํ‹ฐํŒฉํŠธ ๋‹ค์šด๋กœ๋“œ ์•ก์…˜ ์‚ฌ์šฉ with: name: app-artifacts # ๋‹ค์šด๋กœ๋“œํ•  ์•„ํ‹ฐํŒฉํŠธ์˜ ์ด๋ฆ„ - name: Deploy to Server run: | # ์—ฌ๊ธฐ์— ์„œ๋ฒ„ ๋ฐฐํฌ ์Šคํฌ๋ฆฝํŠธ ๋˜๋Š” ๋ช…๋ น์–ด ์ž‘์„ฑ # ๋‹ค์šด๋กœ๋“œํ•œ ์•„ํ‹ฐํŒฉํŠธ(build/libs ๋””๋ ‰ํ† ๋ฆฌ)๋ฅผ ํ™œ์šฉํ•œ ๋ฐฐํฌ ์ž‘์—… ์ˆ˜ํ–‰
Bash
๋ณต์‚ฌ

Runner (๋Ÿฌ๋„ˆ)

Github Actions(Workflows) ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋Š” ํ™˜๊ฒฝ

์ข…๋ฅ˜

โ€ข
Github Hosted Runner
โ€ข
Self-Hosted Runner

Github Hosted Runner

Github ์— ํ˜ธ์ŠคํŒ… ๋˜์–ด ์žˆ๋Š” Gtihub Actions ์‹คํ–‰ํ™˜๊ฒฝ์ž…๋‹ˆ๋‹ค.
Github Actions ์˜ ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ , main.yml ์— ๋ฏธ๋ฆฌ ์ •์˜ํ•œ ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์ƒ์„ฑํ•˜๊ธฐ

1.
Settings
2.
Actions
3.
Runners
a.
[New Runner]

Self-Hosted Runner

์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ํ˜ธ์ŠคํŒ…ํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” Github Actions ์‹คํ–‰ ํ™˜๊ฒฝ์ž…๋‹ˆ๋‹ค.
โ€ข
์‚ฌ์šฉ์ž๋Š” ์ž์ฒด ํ•˜๋“œ์›จ์–ด ๋˜๋Š” ํด๋ผ์šฐ๋“œ์—์„œ ๋Ÿฌ๋„ˆ๋ฅผ ์„ค์ •ํ•˜๊ณ  GitHub์— ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.
โ€ข
ํšŒ์‚ฌ ๋‚ด๋ถ€ ๋„คํŠธ์›Œํฌ์— ์žˆ๋Š” ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ฏผ๊ฐํ•œ ๋ฐ์ดํ„ฐ ๋˜๋Š” ํ™˜๊ฒฝ์—์„œ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
โ€ข
GitHub์ด ์ œ๊ณตํ•˜๋Š” ๋‹ค์–‘ํ•œ ์‹คํ–‰ ํ™˜๊ฒฝ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‚ฌ์šฉ์ž ์ •์˜ ํ™˜๊ฒฝ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ƒ์„ฑํ•˜๊ธฐ

1.
Settings
2.
Actions
3.
Runners
a.
[New Runner]
b.
[Create Self-hosted Runner]
i.
์šด์˜์ฒด์ œ ์ง€์ •
ii.
์•„ํ‚คํ…์ฒ˜ ์ง€์ •
iii.
Download, Configure ๋ช…๋ น์–ด ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰
(LINUX ๊ธฐ์ค€)

Download

# Create a folder $ mkdir actions-runner && cd actions-runner# Download the latest runner package $ curl -o actions-runner-linux-x64-2.311.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.311.0/actions-runner-linux-x64-2.311.0.tar.gz# Optional: Validate the hash $ echo "??????????????????????????? actions-runner-linux-x64-2.311.0.tar.gz" | shasum -a 256 -c# Extract the installer $ tar xzf ./actions-runner-linux-x64-2.311.0.tar.gz
Bash
๋ณต์‚ฌ

Configure

# Create the runner and start the configuration experience $ ./config.sh --url https://github.com/ALOHA-CLASS --token ???????????????????????? Last step, run it!
Bash
๋ณต์‚ฌ
Run (์‹คํ–‰)
./run.sh

๋“ฑ๋ก ํ™•์ธ

1.
My Git Repository
2.
Settings
3.
Actions
4.
Runners

Artifact (์•„ํ‹ฐํŒฉํŠธ)

์›Œํฌํ”Œ๋กœ์šฐ์—์„œ ์ƒ์„ฑ๋œ ํŒŒ์ผ ๋˜๋Š” ๋นŒ๋“œ ๊ฒฐ๊ณผ๋ฌผ์„ ์ €์žฅํ•˜๊ณ  ๋‹ค๋ฅธ ์žก์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๋Š” ์š”์†Œ
์›Œํฌํ”Œ๋กœ์šฐ์˜ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„ ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

์‹ค์Šต

1.
.gitbub/workflows/main.yml ํŒŒ์ผ ์ƒ์„ฑ
2.
์›Œํฌ ํ”Œ๋กœ์šฐ ์ •์˜ํ•˜๊ธฐ
3.
start.sh ๋ฐฐํฌ ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ
4.
๋ฆฌ๋ˆ…์Šค ์„œ๋ฒ„์— self-hosted runner ์„ค์น˜ ๋ฐ ์‹คํ–‰
5.
์„œ๋ฒ„์— git clone ํ•˜๊ธฐ
6.
๊ฐœ๋ฐœ PC ์— git clone ํ•˜๊ธฐ
7.
๊ฐœ๋ฐœ PC ์—์„œ ์ˆ˜์ •ํ•œ ์ฝ”๋“œ git push ํ•˜๊ธฐ
8.
์„œ๋ฒ„์—์„œ ์ž๋™ ๋นŒ๋“œ ๋ฐ ๋ฐฐํฌ ํ™•์ธํ•˜๊ธฐ

.gitbub/workflows/main.yml ํŒŒ์ผ ์ƒ์„ฑ

1.
Repository
2.
Actions
3.
New workflow
4.
set up a workflow yourself

์›Œํฌ ํ”Œ๋กœ์šฐ ์ •์˜ํ•˜๊ธฐ

โ€ข
main.yml ๋งŒ์œผ๋กœ ๋™์ž‘ํ•˜๋Š” ๋ฒ„์ „
โ€ข
main.yml ์™€ start.sh ๋กœ ๋™์ž‘ํ•˜๋Š” ๋ฒ„์ „

main.yml (build & deploy)

name: CI/CD Pipeline on: push: branches: - main jobs: build: runs-on: self-hosted steps: - name: Checkout Repository uses: actions/checkout@v2 - name: permission check run: chmod -R 777 . - name: Build with Gradle run: ./gradlew clean build deploy: needs: build runs-on: self-hosted steps: - name: Checkout Repository uses: actions/checkout@v2 - name: Deploy to Server run: | echo "> ์ƒˆ๋กœ์šด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐฐํฌ" # cd $GITHUB_WORKSPACE/build/libs cd /home/joeun/workspace/HelloSpringBoot/build/libs # ์„œ๋ฒ„ ์‹คํ–‰ echo "> ์„œ๋ฒ„ ์‹คํ–‰" nohup java -jar "hello-application.war" &
Bash
๋ณต์‚ฌ

main.yml (with start.sh)

name: CI/CD Pipeline on: push: branches: - main jobs: build: runs-on: self-hosted steps: - name: Checkout Repository uses: actions/checkout@v2 # - name: deploy to server - name: deploy to server run: | sudo ./start.sh sleep 5
Bash
๋ณต์‚ฌ

start.sh ๋ฐฐํฌ ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ

start.sh

#!/bin/bash # ๋ณ€์ˆ˜ ์„ ์–ธ (=) ์•ž๋’ค๋กœ ๊ณต๋ฐฑโŒ PROJECT_NAME=hello-application WORKSPACE=/home/joeun/workspace REPOSITORY=/home/joeun/workspace/HelloSpringBoot DEPLOY_PATH=/home/joeun/workspace/HelloSpringBoot/build/libs # ํ”„๋กœ์ ํŠธ ๊ฒฝ๋กœ๋กœ ์ด๋™ echo "> ${REPOSITORY} ๋กœ ์ด๋™" cd $REPOSITORY # GIT PULL ์ง„ํ–‰ echo "> GIT PULL" if git pull; then echo "> git pull ์„ฑ๊ณต" else echo "> git pull ์‹คํŒจ" exit 1 fi # ๋นŒ๋“œ ์‹œ์ž‘ echo "> gradle clean build" ./gradlew clean build # ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ์„œ๋ฒ„ pid(ํ”„๋กœ์„ธ์Šค ์•„์ด๋””) ํ™•์ธ CURRENT_PID=$(pgrep -f "${PROJECT_NAME}.war") echo "> ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ์„œ๋ฒ„ pid : ${CURRENT_PID}" # ์‹คํ–‰ ์ค‘์ธ ์„œ๋ฒ„ ์žˆ๋Š”์ง€ ํ™•์ธ if [ -z "$CURRENT_PID" ]; then echo "> ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ์„œ๋ฒ„ ์—†์Œ" else echo "> ํ˜„์žฌ ์‹คํ–‰ ์ค‘์ธ ์„œ๋ฒ„ ์ข…๋ฃŒ" kill -15 $CURRENT_PID sleep 5 fi # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์žฌ๋ฐฐํฌ echo "> ์ƒˆ๋กœ์šด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ฐฐํฌ" cd $DEPLOY_PATH nohup java -jar "${PROJECT_NAME}.war" &
Bash
๋ณต์‚ฌ

๋ฆฌ๋ˆ…์Šค ์„œ๋ฒ„์— self-hosted runner ์„ค์น˜ ๋ฐ ์‹คํ–‰

runner ์„ค์น˜

1.
Settings
2.
Actions
3.
Runners
a.
[New Runner]
b.
[Create Self-hosted Runner]
i.
์šด์˜์ฒด์ œ ์ง€์ •
ii.
์•„ํ‚คํ…์ฒ˜ ์ง€์ •
iii.
Download, Configure ๋ช…๋ น์–ด ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰
(LINUX ๊ธฐ์ค€)

Download

# Create a folder $ mkdir actions-runner && cd actions-runner# Download the latest runner package $ curl -o actions-runner-linux-x64-2.311.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.311.0/actions-runner-linux-x64-2.311.0.tar.gz# Optional: Validate the hash $ echo "??????????????????????????? actions-runner-linux-x64-2.311.0.tar.gz" | shasum -a 256 -c# Extract the installer $ tar xzf ./actions-runner-linux-x64-2.311.0.tar.gz
Bash
๋ณต์‚ฌ

Configure

# Create the runner and start the configuration experience $ ./config.sh --url https://github.com/ALOHA-CLASS --token ???????????????????????? Last step, run it!
Bash
๋ณต์‚ฌ

runner ์‹คํ–‰

cd ~/workspace/actions-runner
Bash
๋ณต์‚ฌ
./start.sh
Bash
๋ณต์‚ฌ

์„œ๋ฒ„์— git clone ํ•˜๊ธฐ

cd ~/workspace
Bash
๋ณต์‚ฌ
git clone https://github.com/ALOHA-CLASS/HelloSpringBoot.git
Bash
๋ณต์‚ฌ

๊ฐœ๋ฐœ PC ์— git clone ํ•˜๊ธฐ

git clone https://github.com/ALOHA-CLASS/HelloSpringBoot.git
Bash
๋ณต์‚ฌ

๊ฐœ๋ฐœ PC ์—์„œ ์ˆ˜์ •ํ•œ ์ฝ”๋“œ git push ํ•˜๊ธฐ

์ฝ”๋“œ ์ˆ˜์ • ๋ฐ ํ‘ธ์‹œ
git add .
Bash
๋ณต์‚ฌ
git commit -m '์ปค๋ฐ‹'
Bash
๋ณต์‚ฌ
git push
Bash
๋ณต์‚ฌ

์„œ๋ฒ„์—์„œ ์ž๋™ ๋นŒ๋“œ ๋ฐ ๋ฐฐํฌ ํ™•์ธํ•˜๊ธฐ

๊ฐœ๋ฐœPC ์—์„œ PUSH
GitHub Actions Runner ์—์„œ ์ด๋ฒคํŠธ ๊ฐ์ง€
build
deploy