git repo에서 지정된 파일을 삭제하는 쉘 스크립트 생성

git repo에서 지정된 파일을 삭제하는 쉘 스크립트 생성

Git 저장소의 파일 이름 목록을 입력으로 사용하는 스크립트를 만드는 방법(한 줄에 하나의 파일 이름이 있는 .txt 파일을 사용할 계획이지만 다른 제안도 가능합니다) 저장소에서 지정된 항목을 제거합니다. 문서?

나는 "cat" 명령을 사용하고 터미널에서 명령이 다음과 같이 보이도록 파이프할 생각입니다.

cd my-git-repo
cat fileNames.txt | myShellScript.sh
git push origin master

그러면 "myShellScript.sh"는 다음과 같습니다.

#!/bin/bash
git rm $0
git add .
git commit -m "removed unused file"

또한 fileNames.txt를 한 줄씩 읽으려면 일종의 "읽기" 명령이 있어야 한다는 것도 알고 있습니다.

답변1

.git을 사용하여 git 저장소에서 코드를 배포할 수 있습니다 rsync. 예를 들어 파일에는 exclude-rsync.txt대상에 배포하지 않을 모든 파일과 디렉터리(한 줄에 하나씩)가 포함될 수 있습니다(또는 새 디렉터리일 수도 있음).

제외-rsync.txt

.git
.gitignore
include/database.yml

rsync 명령

 rsync -avz --delete --exclude-from=exclude-rsync.txt . [email protected]:/var/www/foobar/

CI/CD 파이프라인

gitlab, travis ci(& jenkins)를 사용하면 일반적으로 yaml 파일에 ci/cd 파이프라인을 정의할 수 있습니다. 무언가가 git(lab)에 푸시되거나 브랜치가 마스터에 병합될 때마다 파이프라인이 자동으로 시작됩니다. gitlab의 경우 .gitlab-ci.yml다음과 같은 파일을 추가합니다 .

stages:
  - deploy
deploy_production:
  stage: deploy
  environment: production
  only:
    - master@my_repo
  before_script:
    - yum -y install rsync    
  script:
    - |
       rsync -avz --delete --exclude-from=exclude-rsync.txt . [email protected]:/var/www/foobar/

그러나 더 간단한 경우에는 예를 들어 이러한 파이프가 존재한다는 것을 기억하기 위해 셸 파일을 추가할 수 있으며 deploy.sh이는 좋은 방법이 될 수 있습니다.

#!/bin/bash
rsync -avz --delete --exclude-from=exclude-rsync.txt . [email protected]:/var/www/foobar/

관련 정보