Created
November 11, 2024 02:38
-
-
Save Torokun/54b88febf913a318b75ab3c0f9b81a4a to your computer and use it in GitHub Desktop.
VM起動
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger: | |
- main | |
pool: | |
vmImage: 'ubuntu-latest' | |
variables: | |
VM_NAME: 'my-linux-vm' | |
RESOURCE_GROUP: 'my-resource-group' | |
LOCATION: 'eastus' | |
ADMIN_USERNAME: 'azureuser' | |
PRIVATE_KEY_PATH: '/path/to/private-key.pem' | |
steps: | |
- task: AzureCLI@2 | |
inputs: | |
azureSubscription: '<Your Azure Service Connection>' | |
scriptType: 'bash' | |
scriptLocation: 'inlineScript' | |
inlineScript: | | |
# VMを起動 | |
az vm start --resource-group $(RESOURCE_GROUP) --name $(VM_NAME) | |
# VMの状態を確認し、"running"になるまで待機 | |
for i in {1..30}; do # 最大30回(調整可)リトライ | |
STATUS=$(az vm get-instance-view --resource-group $(RESOURCE_GROUP) --name $(VM_NAME) --query "instanceView.statuses[?code=='PowerState/running'] | [0]" --output tsv) | |
if [ "$STATUS" == "PowerState/running" ]; then | |
echo "VMが起動しました。" | |
break | |
fi | |
echo "VMが起動中です。待機中... ($i)" | |
sleep 10 # 10秒待機(調整可) | |
done | |
if [ "$STATUS" != "PowerState/running" ]; then | |
echo "VMの起動に失敗しました。" | |
exit 1 | |
fi | |
- script: | | |
# SSH接続してコマンドを実行 | |
ssh -o StrictHostKeyChecking=no -i $(PRIVATE_KEY_PATH) $(ADMIN_USERNAME)@$(VM_NAME).$(LOCATION).cloudapp.azure.com 'echo "SSH接続成功"' | |
displayName: 'SSH接続してコマンド実行' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment