context 변수와 비슷한 default environment 가 있음
default environment : runner가 job을 실행하는 도중에만 존재
Contexts 대부분은 언제나 사용 가능.
github context를 사용시 github.token같은 민감한 정보는 조심해야 함
workflow를 작성했을 때, 다른 사람도 이거를 작동시킬 수 있다는 생각을 항상 갖고 있어야 함.
{
"token": "***",
"job": "dump_contexts_to_log",
"ref": "refs/heads/my_branch",
"sha": "c27d339ee6075c1f744c5d4b200f7901aad2c369",
"repository": "octocat/hello-world",
"repository_owner": "octocat",
"repositoryUrl": "git://github.com/octocat/hello-world.git",
"run_id": "1536140711",
"run_number": "314",
"retention_days": "90",
"run_attempt": "1",
"actor": "octocat",
"workflow": "Context testing",
"head_ref": "",
"base_ref": "",
"event_name": "push",
"event": {
...
},
"server_url": "https://github.com",
"api_url": "https://api.github.com",
"graphql_url": "https://api.github.com/graphql",
"ref_name": "my_branch",
"ref_protected": false,
"ref_type": "branch",
"secret_source": "Actions",
"workspace": "/home/runner/work/hello-world/hello-world",
"action": "github_step",
"event_path": "/home/runner/work/_temp/_github_workflow/event.json",
"action_repository": "",
"action_ref": "",
"path": "/home/runner/work/_temp/_runner_file_commands/add_path_b037e7b5-1c88-48e2-bf78-eaaab5e02602",
"env": "/home/runner/work/_temp/_runner_file_commands/set_env_b037e7b5-1c88-48e2-bf78-eaaab5e02602"
}
example github context.
name: Run CI
on: [push, pull_request]
jobs:
normal_ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run normal CI
run: ./run-tests
pull_request_ci:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'pull_request' }}
steps:
- uses: actions/checkout@v3
- name: Run PR CI
run: ./run-additional-pr-ci
사용 예제
on 으로 모든 pr, push에서 다 작동하지만, 내부에서 조금 더 세부적인 조건 설정 가능
name: Hi Mascot
on: push
env:
mascot: Mona
super_duper_var: totally_awesome
jobs:
windows_job:
runs-on: windows-latest
steps:
- run: echo 'Hi ${{ env.mascot }}' # Hi Mona
- run: echo 'Hi ${{ env.mascot }}' # Hi Octocat
env:
mascot: Octocat
linux_job:
runs-on: ubuntu-latest
env:
mascot: Tux
steps:
- run: echo 'Hi ${{ env.mascot }}' # Hi Tux
env도 이렇게 접근 가능하다 뭐 이런 느낌
secret에는 정말 주의 해야 함
{
"status": "success",
"container": {
"network": "github_network_53269bd575974817b43f4733536b200c"
},
"services": {
"postgres": {
"id": "60972d9aa486605e66b0dad4abb638dc3d9116f566579e418166eedb8abb9105",
"ports": {
"5432": "49153"
},
"network": "github_network_53269bd575974817b43f4733536b200c"
}
}
}
job context 는 이런 형태로 되어 있음
예제.
name: PostgreSQL Service Example
on: push
jobs:
postgres-job:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
ports:
# Maps TCP port 5432 in the service container to a randomly chosen available port on the host.
- 5432
steps:
- uses: actions/checkout@v3
- run: pg_isready -h localhost -p ${{ job.services.postgres.ports[5432] }}
- run: ./run-tests
사용 방법은
name: PostgreSQL Service Example
on: push
jobs:
postgres-job:
runs-on: ubuntu-latest
services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
ports:
# Maps TCP port 5432 in the service container to a randomly chosen available port on the host.
- 5432
steps:
- uses: actions/checkout@v3
- run: pg_isready -h localhost -p ${{ job.services.postgres.ports[5432] }}
- run: ./run-tests
5432 포트에서 시작된 랜덤 포트로 연결을 어디다가 해야 하는지 알 수 있음
{
"checkout": {
"outputs": {},
"outcome": "success",
"conclusion": "success"
},
"generate_number": {
"outputs": {
"random_number": "1"
},
"outcome": "success",
"conclusion": "success"
}
}
steps context의 예제,
사용 방법도
name: Generate random failure
on: push
jobs:
randomly-failing-job:
runs-on: ubuntu-latest
steps:
- id: checkout
uses: actions/checkout@v3
- name: Generate 0 or 1
id: generate_number
run: echo "::set-output name=random_number::$(($RANDOM % 2))"
- name: Pass or fail
run: |
if [[ ${{ steps.generate_number.outputs.random_number }} == 0 ]]; then exit 0; else exit 1; fi
이렇게 되는데, 중요한 점은 if, then, else, fi 로 처리되는 구문의 형태 정도...?
{
"os": "Linux",
"arch": "X64",
"name": "GitHub Actions 2",
"tool_cache": "/opt/hostedtoolcache",
"temp": "/home/runner/work/_temp"
}
runner context 는 hosted runner
working directory 세팅 하는 것 정도가 메인
name: Build
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build with logs
run: |
mkdir ${{ runner.temp }}/build_logs
./build.sh --log-path ${{ runner.temp }}/build_logs
- name: Upload logs on fail
if: ${{ failure() }}
uses: actions/upload-artifact@v3
with:
name: Build failure logs
path: ${{ runner.temp }}/build_logs
이렇게 된다.
가장 많이 쓰이는 secret부분은
{
"github_token": "***",
"NPM_TOKEN": "***",
"SUPERSECRET": "***"
}
이 토큰이 github 에 등록된다면 GITHUB_TOKEN으로 처리될 것
이런 코드가 있을 수 있고
name: Pull request labeler
on: [ pull_request_target ]
permissions:
contents: read
pull-requests: write
jobs:
triage:
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v4
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
이런 방식으로 처리할 수 있다. 여기서 repo-token을 통해 불러온다.
strategy
{
"fail-fast": true,
"job-index": 3,
"job-total": 4,
"max-parallel": 4
}
예제는 이렇고
name: Test matrix
on: push
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
test-group: [1, 2]
node: [14, 16]
steps:
- uses: actions/checkout@v3
- run: npm test > test-job-${{ strategy.job-index }}.txt
- name: Upload logs
uses: actions/upload-artifact@v3
with:
name: Build log for job ${{ strategy.job-index }}
path: test-job-${{ strategy.job-index }}.txt
사용은 이렇게 한다.
env, context는 나중에 심심할 때나 보면 될 듯
Workflow triggers
Event
1. workflow repository 에서 일어나는 경우
2. outside the github, trigger "repository_dispatch"
3. schedule
4. mannual
다양한 조건에 따라 시작할 수 있음
workflow trigger는 "on" key와 함께 정의됨
순서
1. event 발생. commit SHA 나 Git ref와 관계가 있음
2. .github/workflows 디렉토리를 찾고, 관계 있는 action 찾음
3. on: 키워드가 현재 조건에 맞는 모든 액션을 실행함.
심지어 workflow에서 workflow를 실행할 수도 있음
github_token 을 통해서 작업을 시작할 경우, 새로운 workflow를 만들지 않음. 순환으로 계속해서 액션 발생을 막음
새로운 workflow 작성을 원할 경우 personal token을 사용해야 함. secret으로 처리함.
on:
issues:
types:
- opened
jobs:
label_issue:
runs-on: ubuntu-latest
steps:
- env:
GITHUB_TOKEN: ${{ secrets.MY_TOKEN }}
ISSUE_URL: ${{ github.event.issue.html_url }}
run: |
gh issue edit $ISSUE_URL --add-label "triage"
생각보다 훨씬 다양한 이벤트 분기 처리가 가능하다.
label: 을 예로 들면 types로 created, edited, deleted같은 것도 다 처리가 가능함
issues도 opened, labeled 등등 처리 가능
on:
push:
branches:
- main
- 'releases/**'
같은 처리도 가능함
여러 처리들이 동시에 영향을 미친다면, 모두 만족하는 것에서만 돌아감. 당연하지만 git ignore과 비슷하게 돌아간다고 봐도 무방.
branch, tag ,같은 것들을 쭉 나열했을 경우 하나만 만족했을 때도 들어감
filter pattern같은 것들은 glob을 따름
A branch named main (refs/heads/main)
A branch named mona/octocat (refs/heads/mona/octocat)
A branch whose name starts with releases/, like releases/10 (refs/heads/releases/10)
on:
pull_request:
# Sequence of patterns matched against refs/heads
branches:
- main
- 'mona/octocat'
- 'releases/**'
- '!releases/**-alpha'
이렇게 되어 있다.