github repository 탭에 actions라는 것이 있다.
이것은 특정 이벤트가 발생했을 때 할 동작을 정의할 수 있게 해 준다.
예를 들어, release 브랜치에 push 이벤트가 발생될 경우
그 커밋을 테스트하고, 빌드하고, 배포하는 동작을 자동화할 수 있다.
action은 .github/workflows 에 yml파일을 작성하여 정의한다.
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: deploy
on:
push:
branches: [master]
jobs:
build:
runs-on: ubuntu-16.04
env:
CI: false
strategy:
matrix:
node-version: [14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- name: Install modules
run: npm install
- name: Run test codes
run: npm run test
- name: Build project
run: npm run build
- name: Deploy build outputs
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_PEM_KEY }}
port: ${{ secrets.PORT }}
source: "build/*"
strip_components: 1
target: "/var/www/html"
이것은 내가 사용중인 deploy.yml 파일을 약간 수정한 것이다.
(.github/workflows/deploy.yml)
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
# action 이름
name: deploy
# action 트리거 조건 (release 브랜치에 푸쉬될 때)
on:
push:
branches: [release]
# action 동작
jobs:
# OS 환경
build:
runs-on: ubuntu-16.04
# Nodejs 런타임 정보 설정
strategy:
matrix:
node-version: [14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
여기까지가 변수, 런타임 환경 설정을 포함한 사전준비라고 봐도 좋다.
간단하게 주석을 달아 두었으니 참고하면 이해하기에 무리가 없을 것이다.
이제 맨 아래에 테스트, 빌드, 배포 동작을 추가할 것이다.
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
# action 이름
name: deploy
# action 트리거 조건 (release 브랜치에 푸쉬될 때)
on:
push:
branches: [release]
# action 동작
jobs:
# OS 환경
build:
runs-on: ubuntu-16.04
# Nodejs 런타임 정보 설정
strategy:
matrix:
node-version: [14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
# 모듈 설치
- name: Install modules
run: npm install
# testcode 실행
- name: Run test codes
run: npm run test
# 빌드
- name: Build project
run: npm run build
# 배포
- name: Deploy build outputs
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_PEM_KEY }}
port: ${{ secrets.PORT }}
source: "build/*"
strip_components: 1
target: "/var/www/html"
이런식으로 작업들을 분리시켜 현재 어떤 동작을 하고 있는지, 어디에서 오류가 나는지를 파악하는데 도움이 된다.
# 배포 (scp)
- name: Deploy build outputs
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_PEM_KEY }}
port: ${{ secrets.PORT }}
source: "build/*"
strip_components: 1
target: "/var/www/html"
마지막에는 scp 커맨드를 실행하는 대신 다른 사람이 작성한 scp-action이라는 것을 쓰는데,
scp커맨드를 쓰기 위해 필요한 private key(pem) file을 출력하기가 까다롭기 때문이다.
만약 password를 사용한다면 직접 scp 명령을 사용해도 된다.
아무튼 pem키를 사용해 scp를 사용하도록 되어 있기 때문에 나는 추가적인 절차가 필요하다.
더 많은 scp-action 옵션은 아래에서 확인할 수 있다.
https://github.com/appleboy/scp-action
pem키와 같이 민감한 정보는 repository에 함께 업로드해서는 안된다.
그렇다면 이런 환경에서는 배포 자동화를 구성할 수 없을까?
secrets 값에 그 데이터를 넣으면 repository에 종속되지만 밖으로 노출시키지 않을 수 있다.
repository> settings> secrets> actions 로 가면 secrets에 값을 추가할 수 있다.
오른쪽 위에 New repository secret을 눌러 pem키의 값을 붙여넣는다.
이제 yml에서 ${{ secrets.[YOUR_SECRET_KEY_NAME] }} 으로 사용할 수 있다.
ex) secrets.SSH_PEM_KEY
# 배포 (scp)
- name: Deploy build outputs
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_PEM_KEY }}
port: ${{ secrets.PORT }}
source: "build/*"
strip_components: 1
target: "/var/www/html"
같은 방법으로 HOST, USERNAME, PORT 등의 정보를 추가한다.
노출되어도 상관 없다면 하드코딩으로 박아넣어도 되지만 SSH_PEM_KEY 만은 secrets에 넣는 것을 추천한다.
이제 개발 완료 후 직접 빌드, 테스트, 배포할 필요 없이
release 브랜치에 푸쉬하기만 하면 github action이 알아서 대신 해준다.
what is scp?
ssh 원격 접속 프로토콜을 기반으로 한 SecureCopy(scp)의 약자로서 원격지에 있는 파일과
디렉터리를 보내거나 가져올 때 사용하는 파일 전송 프로토콜입니다.
네트워크가 연결되어 있는 환경에서 ssh와 동일한 22번 포트와 identity file을 사용해서
파일을 송수신하기 때문에 보안적으로도 안정된 프로토콜이라고 할 수 있겠습니다.
위의 yml스크립트를 그대로 쓰면 빌드 과정에서 발생하는 warning도 error로 취급한다.
이를 원하지 않을 경우 env.CI에 false를 넣어줘야 한다.
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
# action 이름
name: deploy
# action 트리거 조건 (release 브랜치에 푸쉬될 때)
on:
push:
branches: [release]
# action 동작
jobs:
# OS 환경
build:
runs-on: ubuntu-16.04
# 경고 무시
env:
CI: false
# Nodejs 런타임 정보 설정
strategy:
matrix:
node-version: [14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
- name: Install modules
run: npm install
- name: Run test codes
run: npm run test
- name: Build project
run: npm run build
- name: Deploy build outputs
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_PEM_KEY }}
port: ${{ secrets.PORT }}
source: "build/*"
strip_components: 1
target: "/var/www/html"
https://satisfactoryplace.tistory.com/362
참조해서 현재 사용중인 workflow
# action 이름
name: deploy
# action 트리거 조건 (master 브랜치에 푸쉬될 때, PR될때)
on:
push:
branches: [master]
# action 동작
jobs:
# OS 환경
build:
name: hello Cozlin ?
runs-on: ubuntu-20.04
env:
CI: false
# Nodejs 런타임 정보 설정
strategy:
matrix:
node-version: [12.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
cache-dependency-path: ./package-lock.json
# 모듈 설치
- name: Install dependencies
run: npm install
# 빌드
- name: Build project
run: npm run build
# 배포
- name: Deploy build outputs
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
password: ${{ secrets.PASSWORD }}
port: ${{ secrets.PORT }}
source: "build/*"
strip_components: 1
target: "/home/cozlin/cozlin_fe"