이전 commit 정보 수집하기
문제상황
github action을 수행하는동안 이전 커밋과 현재 커밋과 비교해서 바뀐 파일들의 리스트 추출이 필요함. 이전을 의미하는 Head~1을 작성했지만 못 가져오는 상황
기본 github에서 js실행
github action 중에 로직을 js 로 실행하고 싶을 때가 있다. 그럴 땐 아래와 같이 github action에서 실행시켜줄 js를 포함시켜 줄 수 있다. steps 의 run부분은 github 실행환경으로 설정한 ubuntu와 동일하기 때문에 npm으로 라이브러리를 받고 js를 node로 실행시킬 수 있다.
jobs:
build:
steps:
- name: Install gray-matter (for front matter parsing)
run: npm install gray-matter dayjs # 실행시킬 library는 미리 npm 으로 받는다.
- name: Update front matter
run: node scripts/update-frontmatter.js # 실행시킬 js 를 명령어으로 직접 호출
실행할 js 에서 bash 에서 쓰는 데이터를 가져오기 위해서 child_process 를 사용해서 내부 명령어를 shell에서 접근한 내용을 가져온다.
execSync
: 문자열 형태로 명령어를 실행하고 결과를 콜백으로 가져옴.
const childProcess = require('child_process');
// url을 인코딩하지 않고 그대로 사용하기 위해서 -c core.quotepath=false를 사용하였다.
const diffOutput = childProcess.execSync(
'git -c core.quotepath=false diff --name-only HEAD~1 HEAD',
{ encoding: 'utf8' }
);
오류상황
fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.
원래 나는 git diff --name-only HEAD~1 HEAD
명칭을 이렇게 사용했다. 그 결과 뽑히긴 했으나 한들이 인코딩 상태( 예 /123/512
) 식으로 출력이됐고 제대로 파일을 찾아서 매칭하지 못했다. 이 인코딩을 한글명칭을 뽑기 위해서 -c core.quotepath=false
를 통해서 인코딩하는 것을 껐다.
** 참고로 아예 checkout 때 한개만 fetch-depth를 설정 안하면 이번에 commit 하는 대상만 가져온다.
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 2
fetch 결과물
# Sample workflow for building and deploying a Next.js site to GitHub Pages
#
# To get started with Next.js see: https://nextjs.org/docs/getting-started
#
name: Deploy Next.js site to Pages
on:
# Runs on pushes targeting the default branch
push:
branches: ["master"]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
contents: write
pages: write
id-token: write
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
# Build job
build:
environment: github-pages
runs-on: ubuntu-latest
env:
NEXT_PUBLIC_BASE_URL: ${{ secrets.NEXT_PUBLIC_BASE_URL }}
API_DOC_ENV: ${{ secrets.API_DOC_ENV }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 2
# (여기) Front Matter 업데이트 스크립트 실행(임시로 업데이트 날짜 제거)
# - name: Install gray-matter (for front matter parsing)
# run: npm install gray-matter dayjs
# - name: Update front matter
# run: node scripts/update-frontmatter.js
# # 여기서 다시 Git에 푸시
# - name: Commit & push updated front matter
# run: |
# git config user.name "rn0614"
# git config user.email "rn0614@naver.com"
# git add posts/**/*.md
# git commit -m "chore: update front matter [skip ci]" || echo "No changes to commit."
# git push origin HEAD:master
- name: Detect package manager
id: detect-package-manager
run: |
if [ -f "${{ github.workspace }}/yarn.lock" ]; then
echo "manager=yarn" >> $GITHUB_OUTPUT
echo "command=install" >> $GITHUB_OUTPUT
echo "runner=yarn" >> $GITHUB_OUTPUT
exit 0
elif [ -f "${{ github.workspace }}/package.json" ]; then
echo "manager=npm" >> $GITHUB_OUTPUT
echo "command=ci" >> $GITHUB_OUTPUT
echo "runner=npx --no-install" >> $GITHUB_OUTPUT
exit 0
else
echo "Unable to determine package manager"
exit 1
fi
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "20"
cache: ${{ steps.detect-package-manager.outputs.manager }}
- name: Setup Pages
uses: actions/configure-pages@v5
with:
# Automatically inject basePath in your Next.js configuration file and disable
# server side image optimization (https://nextjs.org/docs/api-reference/next/image#unoptimized).
#
# You may remove this line if you want to manage the configuration yourself.
static_site_generator: next
- name: Restore cache
uses: actions/cache@v4
with:
path: |
.next/cache
# Generate a new cache whenever packages or source files change.
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }}
# If source files changed but packages didn't, rebuild from a prior cache.
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json', '**/yarn.lock') }}-
- name: Install dependencies
run: ${{ steps.detect-package-manager.outputs.manager }} ${{ steps.detect-package-manager.outputs.command }}
- name: Create .env
run: |
echo "NEXT_PUBLIC_BASE_URL=${{ secrets.NEXT_PUBLIC_BASE_URL }}" >> .env.production
echo "API_DOC_ENV=${{ secrets.API_DOC_ENV }}" >> .env.production
- name: Build with Next.js
run: ${{ steps.detect-package-manager.outputs.runner }} next build
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./out
# Deployment job
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4