회사에서 자동화를 위해 github actions 를 사용해야하는 일이 생겨서 기초 정도 알아보았다.
일단 actions란 workflow를 자동화 시켜준다고 하는데 와닿지 않아서 직접 작성해보면서 알아가기로 했다.
일단 내가 미리 좀 알고 있던 actions의 내용은,
이 정도인데, 나는 목표를 일단 단순하게 가상 머신에서 python 코드인
print("hi python")
를 출력해보는 것을 목표로 잡았다.
사용법은 작업환경에서 .github/workflows라는 폴더에 yml 파일을 작성해서 작동하게 되는데, 보통 이 폴더를 직접 만들지 않고,
actions를 클릭하게 되면 이 화면이 나오는데 일단, 여기서는 기초만 할 것이므로, set up a workflow yourself를 누른다.
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
pull_request:
branches: [ master ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Runs a single command using the runners shell
- name: Run a one-line script
run: echo Hello, world!
# Runs a set of commands using the runners shell
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
그러면 이런 난해한 코드를 접하게 되고
간단하게 설명을 했을 때, 저 코드상 크게 name, on, jobs으로 나누어서 보면
이렇게 생각하자.
이제 바꿔놓은 yml 파일을 봐보자.
# This is a basic workflow to help you get started with Actions
name: Hello World
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the main branch
push:
branches: [ main ]
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- name: Run pwd
run: pwd
- name: Run ls -al
run: ls -al
이 yml 파일을 해석해보면,
이 workflow의 이름은 Hello World이고
main 브랜치가 push일 때, job이 동작한다.
job은 빌드 할 때, 우분투(리눅스) 운영체제를 사용할 것이고,
steps에 맞게 작업을 수행할 것인데, 총 2가지 작업이 진행된다.
이 두 작업의 이름은 각각
Run pwd
Run ls -al
이라는 이름의 작업이고
각각 수행하는 작업은
pwd
ls -al
이다.
여기서 pwd와 ls -al은 리눅스에서 사용되는 CLI 명령어이다.(리눅스를 사용한다고 했으므로)
pwd : 현재디렉토리
ls -al : 현재 디렉토리에 어떤 파일들이 있는가?
이렇게 작성하고
start commit을 눌러서 Commit new file을 한다. 그리고 actions를 확인해보면 내가 main 브랜치에 push할 때 작동하도록 설정했으므로 작업이 진행되고 있는 것을 확인할 수 있다.
workflow 이름은 Hello World이고 내가 푸쉬한 커밋메시지가 적용됐다.
너무 간단해서 금방 끝났지만 저렇게 초록색은 완료가 된 것인데, 완료되기 전에는 노란 동그라미를 볼 수 있다.
저걸 클릭해보면
이런 식으로 나오게 되고, 자세한 workflow 과정을 보기위해서 build를 클릭한다.
보면 작성했던 작업 2개가 작동했고 각각 클릭해보면
정말 현재 디렉토리를 확인했고,
그리고 ls -al을 통해 디렉토리 안에 어떤 파일들이 있는지 확인할 수 있는데 지금은 없어서 ..만 나온다.
여기서 의문인게 분명 나는
라는 레포지토리에서 작업을 하고 있는데,
현재 디렉토리 파일들을 확인했을 때, .github 폴더 조차 보이지 않는다는 것이다. 이 것은 가상 머신에 내 코드를 아직 옮기지 않아서이다. 이를 위해서 actions/checkout@v2라는 기능을 추가해준다. 무슨 뜬금없는 코드냐고 생각할 수 있는데 github는 다양한 action들의 기능을 제공해준다.
즉, 내가 처음부터 리눅스 명령어를 다 작성하지 않아도, 어떤 기능을 사용한다고 했을 때, 제공해주는 기본적인 코드 모음집이 존재하는 것이다.
new workflow를 확인해보면,
이런 다양한 작성된 yml을 제공함을 알 수 있다. 맨 처음으로 가서 우리가 아무것도 하지 않았을 때, 기본으로 제공되는 yml 파일을 보면
# This is a basic workflow to help you get started with Actions
name: CI
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
pull_request:
branches: [ master ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Runs a single command using the runners shell
- name: Run a one-line script
run: echo Hello, world!
# Runs a set of commands using the runners shell
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
actions/checkout@v2가 기본적으로 있는 것을 확인할 수 있는데, 기본적으로 workflow를 작성할 때, 현재 작업하는 레포지토리를 사용하기에 기본적으로 있는게 아닌가 싶다.
다시 우리가 작업하는 곳에 actions/checkout@v2를 추가해서 확인한다.
# This is a basic workflow to help you get started with Actions
name: github checkout
# Controls when the workflow will run
on: [push]
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
build:
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Runs a single command using the runners shell
- name: Run pwd
run: pwd
# Runs a set of commands using the runners shell
- name: Run ls -al
run: ls -al
이렇게 작성하고 push 해서 확인해보면,
제대로 나왔다. 이제 내 목표인 파이썬 출력하기라는 목표를 진행할 것이다. vscode로
단순히 python 파일을 작성했고, push 한다.
그리고 git에 들어가서 New workflow -> set up a workflow yourself를 클릭한다. 그리고 기존 것들을 일부 지우고,
name: Run Python
on:
push:
branches: [ main ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
이렇게 시작할 것이다. 순서는 현재 작성 코드들을 작성하는 actions/checkout@v2를 진행하고,
python을 가상 머신에 다운받고, 코드를 실행한다. python을 다운 받으려면 actions/checkout@v2처럼 작성해야 하는데 하는 법은
오른쪽에 python을 검색하고 Setup Python을 클릭하면
어떻게 사용하라고 설명 되어 있다. 그리고 파이썬3로 설치된다는 설명이 있다.
name: Run Python
on:
push:
branches: [ main ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-python@v2.3.2
- name: Run python3
run: python3 test.py
전체 yml은 간단한데, 지금까지 해 왔던대로 해석해보면,
가상 머신을 리눅스로 설치하고,
현재 작업환경을 복사하고,
파이썬을 설치하고
파이썬 명령어를 실행한다.
commit으로 결과를 확인해보자
잘 실행된 것을 확인할 수 있다.
여기까지는 기초인데, 제공하는 option들이 많다. 예를 들어 코드에 동작방식으로 workflow_dispatch도 추가했는데, 이는 수동으로 작동한다.
여기서 Run workflow를 눌러서도 job을 실행할 수 있도록 한 것이다. 이렇게 다양한 옵션들이 있으니 공부가 많이 필요할 것 같다.