백엔드 서비스가 여러 개 있어 매번 해당 디렉터리로 이동해 git pull을 실행하는 것이 번거로웠던 경험이 있으실 것입니다. 이번 포스팅에서는 VS Code의 Tasks와 PowerShell 스크립트를 활용하여, 한 번의 커맨드 실행으로 여러 서비스의 Git 업데이트를 자동화하는 방법을 소개합니다.
사용 환경 및 목적
- 환경: VS Code, Windows PowerShell
- 목적: 여러 Git 리포지토리를 하나의 명령어로 업데이트
- 구성: VS Code의 tasks.json과 PowerShell 스크립트
1. VS Code Tasks 설정 (tasks.json)
아래 예시는 프로젝트 루트 또는 .vscode 폴더에 위치시킵니다.
여기서는 환경 변수 BASE_DIR를 이용해 현재 워크스페이스 경로를 전달합니다.
{
"version": "2.0.0",
"tasks": [
{
"label": "Update All Repositories",
"type": "shell",
"command": "powershell",
"args": [
"-NoExit",
"-File",
"${workspaceFolder}/.vscode/update-repos.ps1"
],
"options": {
"env": {
"BASE_DIR": "${workspaceFolder}"
}
},
"problemMatcher": []
}
]
}
2. PowerShell 스크립트 작성 ( update-repos.ps1 )
프로젝트의 .vscode 폴더에 아래 스크립트를 생성합니다.
이 스크립트는 지정한 여러 Git 리포지토리 디렉터리로 이동해 git pull을 실행하고, 결과를 로그 파일에 기록합니다.
# update-repos.ps1
# 이 스크립트는 여러 Git 리포지토리를 업데이트합니다.
$baseDir = $env:BASE_DIR
$repositories = @("backend-api", "auth-service", "data-service", "notification-service")
# 업데이트 로그 파일 경로 설정
$logPath = Join-Path $baseDir "update-log.txt"
"--- Update started at $(Get-Date) ---" | Out-File -FilePath $logPath
foreach ($repo in $repositories) {
$repoPath = Join-Path $baseDir $repo
if (Test-Path $repoPath) {
Write-Host "Updating repository '$repo' at $repoPath..."
"[$(Get-Date)] Updating repository '$repo'" | Out-File -Append -FilePath $logPath
Push-Location $repoPath
$output = git pull 2>&1
$output | Out-File -Append -FilePath $logPath
Pop-Location
}
else {
Write-Host "Repository '$repo' not found."
"[$(Get-Date)] Repository '$repo' not found." | Out-File -Append -FilePath $logPath
}
}
"--- Update completed at $(Get-Date) ---" | Out-File -Append -FilePath $logPath
3. 작업 실행 방법
- VS Code에서 Ctrl + Shift + P를 눌러 "Tasks: Run Task" 명령을 선택합니다.
- 목록에서 "Update All Repositories" 태스크를 선택하면,
지정된 모든 Git 리포지토리에서 자동으로 git pull 명령이 실행되고 로그가 남습니다. - 각 리포지토리의 업데이트 결과는 워크스페이스 루트에 생성된 update-log.txt 파일에서 확인할 수 있습니다.
마무리
위와 같이 설정하면,
여러 Git 리포지토리를 일일이 수동으로 업데이트할 필요 없이 VS Code에서 하나의 태스크 실행으로 모두 관리할 수 있습니다.
특히 백엔드 서비스가 많을 경우 작업 효율을 크게 높일 수 있으니 활용해 보시기 바랍니다
'GIT' 카테고리의 다른 글
[Git] git 명령어 (0) | 2023.08.30 |
---|---|
[Git] Git Data Structures (0) | 2023.08.30 |
[Git] git branch 전략 (0) | 2023.08.30 |
[Git] git branch 전략 (0) | 2023.06.01 |
[Git] ssh 참고 링크 (0) | 2023.06.01 |
댓글