어떤 상황에서 사용할 수 있는가?
Github Action으로 다양한 작업을 구성 및 수행하다보면 가끔 루트디렉터리가 아닌 특정 디렉터리에서 커맨드를 수행해야 하는 경우가 존재한다. 그러한 경우에 cd(change directory) 명령어를 앞에 수행해줘도 되지만, 스크립트가 깔끔해보이지 않는다. 뭔가 마음에 들지 않는다. 그럼 어떻게 해야할까?
step 에 working_directory 프로퍼티 이용하기
Github Action에서는 working_directory를 이용하여 특정 디렉터리에서 수행할 수 있도록 지원한다. 특정 디렉터리에서 원하는 step 수행하고자 할 때 working-directory 프로퍼티를 추가한다. 예시는 아래와 같다.
name: Working Directory Example
on:
push:
branches: [ master ]
jobs:
test:
name: Test working directory
runs-on: [ubuntu-latest]
steps:
...
- name: Echo hello world in working directory
run: echo "Hello world"
working-directory: ./directory_you_want_to_run_step
- name: Echo hello world in root
run: echo "Hello world"
위와 같은 방식은 휴먼에러가 발생하기 쉽다.(조금만 오타내면 잘못된 경로에서 step을 수행하려할테고, 그럼 에러가 발생할 것이다.) 좀 더 깔끔하게 working-directory 프로퍼티를 환경변수로 추출하여 보자.
name: Working Directory Example
on:
push:
branches: [ master ]
jobs:
test:
name: Test working directory
runs-on: [ubuntu-latest]
env:
working-directory: ./directory_you_want_to_run_step
steps:
...
- name: Echo hello world in working directory
run: echo "Hello world"
working-directory: ${{ env.working-directory }}
- name: Echo hello world in root
run: echo "Hello world"
defaults 에 working_directory 프로퍼티 이용하기
모든 step을 특정 디렉토리에서 수행하고자 할 때 위의 예시처럼 각 step마다 working-directory 프로퍼티를 추가하는 것은 매우 보기 좋지 않다. 스크립트 관리도 힘들 뿐더러 스텝이 추가될 시 보일러-플레이트 코드만 지속적으로 발생시킬 뿐이다. 그렇다면 어떻게 해결할 수 있을까?
이럴 때 defaults에 working-directory를 명시해준다.
name: Working Directory Example
on:
push:
branches: [ master ]
jobs:
test:
name: Test working directory
runs-on: [ubuntu-latest]
defaults:
run:
working-directory: ./directory_you_want_to_run_step
steps:
...
- name: Echo hello world in working directory
run: echo "Hello world"
...
위 처럼 defaults 로 working-direcroy를 정의해주면, job의 모든 step들을 해당 디렉토리에서 수행이 된다. 만약 원치 않다면 step 별로 working-directory 프로퍼티를 설정해 주는 것을 추천한다.
'ETC' 카테고리의 다른 글
ADsP (데이터분석 준전문가) 자격증 취득 후기 (0) | 2022.04.07 |
---|---|
[Jenkins] cert problem (0) | 2022.03.22 |
Ubuntu - terminator 설치 후 vertically split 문제 (0) | 2022.02.10 |
Github Action - GameCI 이용하여 Unity 프로젝트 빌드하기 (0) | 2022.02.05 |
Github Action #2 - 예제 분석하기 (0) | 2022.01.30 |