카테고리 없음

GitHub 대용량 파일 업로드 release binary로 해결하기

SciomageLAB 2024. 10. 20. 16:49
반응형

개요

GitHub에 이미지나 비디오 같은 대용량 미디어 파일이나 딥러닝 가중치 파일같이 큰 용량의 파일을 업로드 해야 할 일이 있다.

대용량 파일을 git add 해서 그냥 업로드 하면 아래와 같은 메세지를 만나서 LFS 어쩌구를 하게 되는데, 이 방법이 쉬운것도 아니고 자주 바뀌는 파일이라면 매번 git이 추적하다 보니 .git 용량이 어마어마 해 진다.

remote: error: Trace: ***
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File *** is 200 MB; this exceeds GitHub’s file size limit of 100.00 MB

최근 딥러닝 모델 오픈소스를 보면 pre trained model을 google drive나 바이두 드라이브에 압로드하고 링크를 README에 넣는다. 일부 프로젝트는 GitHub release에 파일을 업로드 하는데 이 방법에 대해서 알아보고자 한다

샘플 소스만 보고 싶으면 https://github.com/dankernel/release-file-sample 링크에 있는 샘플만 보면 된다.

GitHub release

상술 한대로 GitHub release를 이용하면 git에서 매번 파일을 추척하지 않아서 .git 디렉토리가 어마어마하게 커지는걸 막을 수 있다. 말 그대로 릴리즈 이기 때문에 릴리즈 버전이 올라갈 때 마다 새로 업로드 하거나 파일을 수정해도 된다.

이렇게 릴리즈 할 때 마다 파일을 업로드 할 수 있다. 100MB가넘는 파일도 아주 잘 올라간다.

GitHub release binary다운받기

이렇게 올라간 파일을 다운받아서 사용하면 된다. 일반적으로 host 환경에서는 그냥 다운로드 받으면 되지만, ssh 원격 환경에서는 다운받은 파일을 원격에 업로드 하거나, 링크 주소를 복사해서 wget으로 다운받아야 하는데, private인 경우에는 이것도 안된다.

이런 상황에서 github TOKEN으로 인증해서 release 파일을 다운받는 shell script 스니펫을 사용하면 된다.

<pre class="wp-block-code">```bash
# Verify args
if [ $# -ne 1 ] 
then
  echo Usage: $0 GITHUB_TOKEN >&2
  exit 1
fi

# Set args
TOKEN=$1
REPO=dankernel/release-file-sample
VERSION=v0.0.1
GITHUB=https://api.github.com
DES=./files/
mkdir $DES
alias errcho='>&2 echo'

# Set files
FILES=(linux-5.15.11.tar.xz)

errcho() {
  echo $@ >&2
}

# DO NOT EDIT
function gh_curl() {
  curl -H Authorization: token $TOKEN \
       -H Accept: application/vnd.github.v3.raw \
       $@
}

# Get files
for FILE in ${FILES[@]}; do
    echo GET ${FILE}...

    # Get asset_id
    parser=.assets | map(select(.name == \$FILE\))[0].id
    asset_id=`gh_curl -s $GITHUB/repos/$REPO/releases/tags/$VERSION | jq $parser`

    if [ -z $asset_id ]; then
      errcho ERROR: version not found $VERSION
      exit 1
    fi;
    if [ $asset_id = null ]; then
      errcho ERROR: file $FILE not found in version $VERSION
      exit 2
    fi;

    wget --auth-no-challenge --header='Accept:application/octet-stream' \
        https://$TOKEN:@api.github.com/repos/$REPO/releases/assets/$asset_id \
        -O $DES/$FILE
done

자세히 보기 : https://github.com/dankernel/release-file-sample/blob/main/get_release_file.sh

이렇게 하면 원격에서도 쉽게 다운받을 수 있다.

Github Action

이렇게 스크립트를 만들어 놓으면 Github Action에서도 사용할 수 있다.

결론

전체 샘플 소스는 https://github.com/dankernel/release-file-sample 에 있다.

반응형