Jenkins 사용 버전

플러그인 설치

  • PowerShell : 빌드 스크립트 powershell 사용
  • credentials-binding :
    • credential 에 윈도우 접속 아이디/패스워드를 넣고 읽어들이기 위함
    • powershell 에서 "$env:정의한_변수명" 으로 사용할 수 있음

 

 

########################################################
## 배포 대상 서버에서 실행
winrm quickconfig -force


# "Enable-PSRemoting -Force" 실행시 아래 에러 발생한다면 Private 네트워크로 변경해야 한다.
#	에러: 이 컴퓨터의 네트워크 연결 형식 중 하나가 공용으로 설정되어 WinRM 방화벽 예외가 작동하지 않습니다. 네트워크 연결 형식을 도메인 또는 개인으로 변경한 후 다시 시도하십시오.
#	조치 방법1: 체크를 무시하도록 변경
#        PS> Enable-PSRemoting -SkipNetworkProfileCheck -Force
#	조치 방법2: Pulbic Network 를 Private 로 변경
#        PS> Set-NetConnectionProfile -NetworkCategory Private
#	참고: https://gist.github.com/ajchemist/5ae3b87add56d39a5b051d860b8bc781
#		  https://4sysops.com/archives/enabling-powershell-remoting-fails-due-to-public-network-connection-type/
#   확인: PS> Get-NetConnectionProfile
Enable-PSRemoting -Force


########################################################
## Jenkins 서버 세팅 (관리자 권한 파워쉘)
winrm quickconfig -force
Set-ExecutionPolicy RemoteSigned -Force


########################################################
## Jenkins 서버 Trust Ip 등록 (관리자 권한 파워쉘)

$trustIp = "192.168.56.101"
$currentTrustedHosts = (Get-Item WSMan:\localhost\Client\TrustedHosts).value
if ($currentTrustedHosts)
{
	$hosts = "$currentTrustedHosts, $trustIp"
}
else 
{
	$hosts = "$trustIp"
}
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "$hosts" -Force
(Get-Item WSMan:\localhost\Client\TrustedHosts).value


########################################################
## 파일 복사

$ip = "192.168.56.101"
$user = "admin"
$pass = "admin_password"

$src = "$(Get-Location)\GameServer\GameServer"
$dst = "d:\dist\" + $(Get-Date).ToString("yyyyMMdd.HHmmss") + "\"

# START SESSION
$securePass = ConvertTo-SecureString -AsPlainText -Force $pass
$credential = New-Object System.Management.Automation.PSCredential($user, $securePass)
$session = New-PSSession -ComputerName $ip -Credential $credential

# STOP SERVICE
Invoke-Command -Session $session {Stop-Service -Name TestService}

# COPY BINARY
Copy-Item "$src" -Destination "$dst" -ToSession $session -Recurse

# START SERVICE
Invoke-Command -Session $session {Start-Service -Name TestService}

# END SESSION
Remove-PSSession $session


########################################################
## 부록

# credential 로 명령
Invoke-Command -Computername $ip -Credential $credential {Stop-Service -Name TestService -Force}
Invoke-Command -Computername $ip -Credential $credential {Start-Service -Name TestService -Force}

 

 

 

 

 

 

TeamCity 에서 사용한 버전

<#
########################################################
# Teamcity Server PC 실행 (실제론 배포될 대상 PC)
winrm quickconfig -force


# "Enable-PSRemoting -Force" 실행시 아래 에러 발생한다면 Private 네트워크로 변경해야 한다.
#	에러: 이 컴퓨터의 네트워크 연결 형식 중 하나가 공용으로 설정되어 WinRM 방화벽 예외가 작동하지 않습니다. 네트워크 연결 형식을 도메인 또는 개인으로 변경한 후 다시 시도하십시오.
#	조치 방법1: 체크를 무시하도록 변경
#        PS> Enable-PSRemoting -SkipNetworkProfileCheck -Force
#	조치 방법2: Pulbic Network 를 Private 로 변경
#        PS> Set-NetConnectionProfile -NetworkCategory Private
#	참고: https://gist.github.com/ajchemist/5ae3b87add56d39a5b051d860b8bc781
#		  https://4sysops.com/archives/enabling-powershell-remoting-fails-due-to-public-network-connection-type/
#   확인: PS> Get-NetConnectionProfile
Enable-PSRemoting -Force


########################################################
# Agent PC 실행 (관리자 권한 Windows PowerShell ISE) (빌드를 수행하는 PC)

# 빌드 결과물 복사할 IP
$trustIp = "192.168.56.101"
winrm quickconfig -force
Set-ExecutionPolicy RemoteSigned -Force

# current trusted hosts
$currentTrustedHosts = (Get-Item WSMan:\localhost\Client\TrustedHosts).value

# add trusted hosts
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "$currentTrustedHosts, $trustIp" -Force

# trusted hosts 확인
(Get-Item WSMan:\localhost\Client\TrustedHosts).value

#winrm s winrm/config/client '@{TrustedHosts='$trustIp'}'

########################################################
# 참고 자료
https://svrstudy.tistory.com/75

########################################################
# cmd 에서 ps1 실행 방법
Powershell.exe -noprofile -executionpolicy bypass -file "d:\test.ps1"

#>



$ip = "192.168.56.101"
$user = "COMPUTER_NAME\계정명"
$pass = "암호"
$securePass = ConvertTo-SecureString -AsPlainText -Force $pass
$credential = New-Object System.Management.Automation.PSCredential($user, $securePass)
$session = New-PSSession -ComputerName $ip -Credential $credential

# copy file (remote pc) : full path
# Copy-Item "C:\source" -Destination "d:\dist" -ToSession $session -Recurse

# copy file (remote pc) : teamcity copy example
$now = Get-Date
$dist = "d:\dist\App\Debug\" + $now.ToString("yyyyMMdd.HHmmss") + "\"
echo $dist

$currentLocation = Get-Location
Copy-Item "$currentLocation\App\App\bin\Debug\netcoreapp3.1" -Destination $dist -ToSession $session -Recurse

# remove session
Remove-PSSession $session


# remove session
Remove-PSSession $session

 

 

 

 

 

 

 

visual studio 2019 커맨드 라인 빌드 명령

nuget 은 .net framework 프로젝트 있는 경우 때문에 사용함.

 

# .net framework 설치 버전 확인
https://www.windowscentral.com/how-quickly-check-net-framework-version-windows-10

# .net framework 설치
https://dotnet.microsoft.com/en-us/download/dotnet-framework/net48

# .net core 설치 버전 확인
dotnet --list-sdks

# .net core 설치
https://dotnet.microsoft.com/en-us/download/dotnet

# 컴파일
https://www.nuget.org/downloads 에서 nuget.exe 다운로드

 

nuget restore [경로.sln]
dotnet restore [경로.sln]
dotnet build 경로/솔루션파일.sln -c 빌드구성이름

 

 

write-host $pwd

############################################################
## Build
$env:MSBUILDDISABLENODEREUSE=1
nuget restore ./TestServer/TestServer/TestServer.sln
dotnet restore ./TestServer/TestServer1/TestServer1.sln


dotnet build ./TestServer/TestServer1/TestServer1.sln -c Debug




############################################################
## Deploy

$ip = "192.168.56.101"
$user = $env:REMOTE_WIN_ID
$pass = $env:REMOTE_WIN_PW


# START SESSION
$securePass = ConvertTo-SecureString -AsPlainText -Force $pass
$credential = New-Object System.Management.Automation.PSCredential($user, $securePass)
$session = New-PSSession -ComputerName $ip -Credential $credential


function Deplay($src, $dst, $serviceName) {
  
	# STOP SERVICE
	Invoke-Command -Session $session {Stop-Service -Name $using:serviceName}
	Start-Sleep -seconds 5
	
	# Remove
	Invoke-Command -Session $session {Remove-Item -Path $using:dst -Recurse -Force -ErrorAction Ignore}
	Start-Sleep -seconds 5
	
	# COPY
	Copy-Item "$src" -Destination "$dst" -ToSession $session -Recurse -Force

	# START SERVICE
	Invoke-Command -Session $session {Start-Service -Name $using:serviceName}
}

Deplay "$(Get-Location)\TestServer\TestServer1\bin\Debug\netcoreapp3.1" "D:\Bin\TestServer1" "RM2TestServer1"


# END SESSION
Remove-PSSession $session

 

 

 

배포 향상 버전

-AsJob 으로 Invoke-Command 실행 결과 대기

Set-PSDebug -Trace 1

write-host $pwd

############################################################
## Build
$env:MSBUILDDISABLENODEREUSE=1
nuget restore ./TestServer/TestServer/TestServer.sln

dotnet build ./TestServer/TestServer/TestServer.sln -c Release


############################################################
## Deploy

$ip = "192.168.56.101"
$user = $env:REMOTE_WIN_ID
$pass = $env:REMOTE_WIN_PW



# START SESSION
$securePass = ConvertTo-SecureString -AsPlainText -Force $pass
$credential = New-Object System.Management.Automation.PSCredential($user, $securePass)
$session = New-PSSession -ComputerName $ip -Credential $credential


function FuncStopService($serviceName) {
	# STOP SERVICE - 원격 컴퓨터 서비스 중지
	$job = Invoke-Command -Session $session {Stop-Service -Name $using:serviceName} -AsJob
	Wait-Job -Job $job
}

function FuncStartService($serviceName) {
	# START SERVICE - 원격 컴퓨터 서비스 시작
	$job = Invoke-Command -Session $session {Start-Service -Name $using:serviceName} -AsJob
	Wait-Job -Job $job
}

function FuncRemoveOnRemote($dst) {
	# Remove - 원격 컴퓨터 파일 삭제
	$job = Invoke-Command -Session $session {Remove-Item -Path $using:dst -Recurse -Force -ErrorAction Ignore} -AsJob
	Wait-Job -Job $job
}

function FuncCopyToRemote($src, $dst) {
	# COPY - 로컬 컴퓨터 파일을 원격 컴퓨터 파일로 복사
	Copy-Item "$src" -Destination "$dst" -ToSession $session -Recurse -Force
}

function FuncCopyOnRemote($src, $dst) {
	# COPY- 원격 컴퓨터의 파일을 다른 곳으로 복사 
	$job = Invoke-Command -Session $session {Copy-Item $using:src -Destination $using:dst -Recurse -Force} -AsJob
	Wait-Job -Job $job
}


$remoteTempTestServer = "D:\Bin\Temp\TestServer"

FuncRemoveOnRemote $remoteTempTestServer


############################################################
# Stop All Service
FuncStopService "TestServerService"

# wait stop services : 서비스 중지 시간이 길다면 늘려야 한다.
Start-Sleep -seconds 10



############################################################
# GAME SERVER

FuncCopyToRemote "$(Get-Location)\TestServer\TestServer\bin\Release\netcoreapp3.1" $remoteTempTestServer


# RM2NewAevraury
FuncRemoveOnRemote "D:\Bin\TestServer"
FuncCopyOnRemote $remoteTempTestServer "D:\Bin\TestServer"
FuncCopyToRemote "$(Get-Location)\TestServer\TestServer\Config.json" "D:\Bin\TestServer\Config.json"




############################################################
# Start All Service
FuncStartService "TestServerService"


# SWEEP
FuncRemoveOnRemote $remoteTempTestServer




# END SESSION
Remove-PSSession $session

'가지가지' 카테고리의 다른 글

redis sentinel. master 정보 확인  (0) 2022.12.02
[kubernetes] deployment, service 테스트  (0) 2022.05.18
[kubernetes] CLI 명령어  (0) 2022.05.18
[kubernetes] k3s 설치 - on windows, ubuntu multipass  (0) 2022.04.27
docker, docker-compose 설치 on ubuntu  (0) 2021.03.12
logstash grok sample  (0) 2020.03.10
tcpdump  (0) 2020.03.03
로그 시간별로 쪼개기  (0) 2020.02.06