Skip to content

Commit

Permalink
[Chore] azure pipline 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
luna156 committed Nov 13, 2024
1 parent 6c07a5f commit f6734ae
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 35 deletions.
55 changes: 30 additions & 25 deletions .azure/innerjoin-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,48 @@

trigger: none

variables:
- group: innerjoin-cicd # 변수 그룹 사용

jobs:
- job: DeployToVM
pool:
vmImage: 'ubuntu-latest'
name: 'Default'
demands:
- agent.name -equals inner-join-WAS # 에이전트 이름
steps:
# Step 1: GitHub 아티팩트 다운로드 (GitHub Actions에서 업로드된 경우)
- task: DownloadPipelineArtifact@2
- task: AzureCLI@2
inputs:
buildType: 'current'
artifactName: 'repository-archive'
targetPath: '$(System.DefaultWorkingDirectory)'
azureSubscription: $(AZURE_SUBSCRIPTION_ID)
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
az storage blob download \
--account-name $(AZURE_STORAGE_NAME) \
--container-name $(AZURE_CONTAINER_NAME) \
--name $(filename) \
--file $(System.DefaultWorkingDirectory)/$(filename)
displayName: 'Download Blob from Azure Storage'

# Step 2: 파일 압축 해제
- task: ExtractFiles@1
inputs:
archiveFilePatterns: '$(System.DefaultWorkingDirectory)/repository.zip'
destinationFolder: '$(System.DefaultWorkingDirectory)/repository'
destinationFolder: '/home/innerjoin/deployment'
cleanDestinationFolder: true

# Step 3: SSH로 파일 업로드
- task: CopyFilesOverSSH@0
- task: Bash@3
inputs:
sshEndpoint: 'innerjoin-VM'
sourceFolder: '$(System.DefaultWorkingDirectory)/repository'
contents: '**'
targetFolder: '/home/innerjoin/deployment'
targetType: 'inline'
script: 'rm -f $(System.DefaultWorkingDirectory)/repository.zip'

# Step 4: 기존 실행중인 프로세스 종료
- task: SSH@0
inputs:
sshEndpoint: 'innerjoin-VM'
runOptions: 'commands'
commands: 'bash /home/innerjoin/deployment/script/stop.sh'

# Step 5: WAS 서버 실행
- task: SSH@0
inputs:
sshEndpoint: 'innerjoin-VM'
runOptions: 'commands'
commands: 'bash /home/innerjoin/deployment/script/start.sh'
- script: |
# 쉘 스크립트 실행 (VM 내에서 파일이 이미 저장된 경로로 지정)
bash /home/innerjoin/deployment/script/stop.sh
displayName: 'Execute stop.sh on VM'
- script: |
# 또 다른 쉘 스크립트 실행
bash /home/innerjoin/deployment/script/start.sh
displayName: 'Execute start.sh on VM'
95 changes: 85 additions & 10 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ env:
MYSQL_HOST: ${{ secrets.MYSQL_HOST }}
REDIS_HOST: ${{ secrets.REDIS_HOST }}


jobs:
build-and-upload:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -112,19 +113,93 @@ jobs:
mkdir -p artifacts
zip -r artifacts/repository.zip .
# (9) GitHub Actions 아티팩트로 저장 (선택 사항)
- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: repository-archive
path: artifacts/repository.zip
# (9) azure blob에 업로드
# 1. Azure CLI 설정
- name: Install azure-cli
uses: pietrobolcato/[email protected]

# 2. Azure 로그인
- name: Log in to Azure
run: |
az login --service-principal --username ${{ secrets.AZURE_CLIENT_ID }} --password ${{ secrets.AZURE_CLIENT_SECRET }} --tenant ${{ secrets.AZURE_TENANT_ID }}
# 기존 파일 삭제
- name: Clear folder in Azure Blob Storage
run: |
az storage blob delete-batch \
--account-name ${{ secrets.AZURE_STORAGE_ACCOUNT_NAME }} \
--account-key ${{ secrets.AZURE_STORAGE_ACCOUNT_KEY }} \
--source ${{ secrets.AZURE_STORAGE_CONTAINER_NAME }} \
--pattern "*"
# 5. Azure Blob Storage에 파일 업로드
- name: Upload file to Azure Blob Storage
run: |
az storage blob upload \
--account-name ${{ secrets.AZURE_STORAGE_ACCOUNT_NAME }} \
--account-key ${{ secrets.AZURE_STORAGE_ACCOUNT_KEY }} \
--container-name ${{ secrets.AZURE_STORAGE_CONTAINER_NAME }} \
--file artifacts/repository.zip \
--name repository.zip
# (10) Azure DevOps 파이프라인 트리거
- name: Trigger Azure DevOps Pipeline
run: |
# Azure DevOps 파이프라인을 트리거
curl -u ":$AZURE_DEVOPS_PAT" \
response=$(curl -u ":$AZURE_DEVOPS_PAT" \
-X POST \
-H "Content-Type: application/json" \
-d '{"resources": {"repositories": {"self": {"refName": "refs/heads/master"}}}}' \
"https://dev.azure.com/$AZURE_DEVOPS_ORG/$AZURE_DEVOPS_PROJECT/_apis/pipelines/$AZURE_DEVOPS_PIPELINE_ID/runs?api-version=6.0-preview.1"
-d '{
"resources": {
"repositories": {
"self": {
"refName": "refs/heads/master"
}
}
},
"variables": {
"filename": {
"value": "repository.zip"
}
}
}' \
"https://dev.azure.com/${{ env.AZURE_DEVOPS_ORG }}/${{ env.AZURE_DEVOPS_PROJECT }}/_apis/pipelines/${{ env.AZURE_DEVOPS_PIPELINE_ID }}/runs?api-version=6.0-preview.1")
pipeline_id=$(echo "$response" | jq -r '.id')
echo "pipeline = $response"
echo "Triggered pipeline with ID: $pipeline_id"
# (11) Azure DevOps 파이프라인 상태 확인
- name: Check Azure DevOps Pipeline Status
run: |
max_wait_time=300 # 최대 대기 시간 (초)
elapsed_time=0
sleep_interval=10
status="inProgress"
while [[ "$status" == "inProgress" || "$status" == "queued" ]] && [[ "$elapsed_time" -lt "$max_wait_time" ]]; do
echo "Checking pipeline status..."
response=$(curl -s -u ":$AZURE_DEVOPS_PAT" \
"https://dev.azure.com/$AZURE_DEVOPS_ORG/$AZURE_DEVOPS_PROJECT/_apis/pipelines/$AZURE_DEVOPS_PIPELINE_ID/runs/$pipeline_id?api-version=6.0-preview.1")
status=$(echo "$response" | jq -r '.status')
sleep $sleep_interval
elapsed_time=$((elapsed_time + sleep_interval))
done
if [[ "$elapsed_time" -ge "$max_wait_time" ]]; then
echo "Pipeline status check timed out."
exit 1
fi
# (12) Azure DevOps 파이프라인 성공 여부 확인
- name: Verify Pipeline Success
run: |
result=$(echo "$response" | jq -r '.result')
if [[ "$result" != "succeeded" ]]; then
failure_reason=$(echo "$response" | jq -r '.logs' || echo "No specific failure reason provided.")
echo "Azure DevOps pipeline failed. Reason:"
echo "${failure_reason:0:500}" # 메시지가 길 경우 자르기
exit 1
else
echo "Azure DevOps pipeline succeeded."
fi

0 comments on commit f6734ae

Please sign in to comment.