-
Notifications
You must be signed in to change notification settings - Fork 1
190 lines (161 loc) · 7.09 KB
/
image-builder.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# This is a basic workflow to help you get started with Actions
name: build-image-and-test
# Controls when the action will run.
on:
workflow_dispatch:
inputs:
environment:
description: 'Environment name'
required: true
default: 'jm-azure'
resourceGroup:
description: 'Resource group name'
required: true
default: 'marketplace-poc03'
dist-type:
description: 'Image creation output, possible values: ManagedImage | SharedImageGallery | VHD'
required: true
default: 'VHD'
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build-vm:
# The type of runner that the job will run on
runs-on: ubuntu-latest
environment:
name: ${{ github.event.inputs.environment }}
outputs:
imageUri: ${{ steps.build-vm-image.outputs.custom-image-uri }}
ip: ${{ steps.create-vm.outputs.ipaddr }}
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: Create Workflow Artifacts
run: |
cd ${{ GITHUB.WORKSPACE }}
mkdir workflow-artifacts/
cp -r ${{ GITHUB.WORKSPACE }}/vmcode/* ${{ GITHUB.WORKSPACE }}/workflow-artifacts/
- name: Azure Login
uses: Azure/#@v1.1
with:
# Paste output of `az ad sp create-for-rbac` as value of secret variable: AZURE_CREDENTIALS
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Check az providers and feature registration
uses: azure/CLI@1.0.4
with:
azcliversion: latest
inlineScript: |
#constants
BLUE='\033[1;34m'
RED='\033[1;31m'
NC='\033[0m'
errorState=0
# Check provider list
providers=("Microsoft.VirtualMachineImages" "Microsoft.KeyVault" "Microsoft.Compute" "Microsoft.Storage" "Microsoft.Network")
for provider in ${providers[@]}; do
state=$(az provider show --namespace ${provider} -o tsv --query registrationState)
if [ $state != 'Registered' ]; then
echo -e "::error::Account not registered for provider ${provider}\nPlease run this in your account:\n\t${BLUE}az provider register --namespace ${provider}${NC}"
errorState=1
else
echo "::debug::Provider ${provider} registered!"
fi
done
#check vm template feature (needed because it's still in preview)
state=$(az feature show --namespace Microsoft.VirtualMachineImages --name VirtualMachineTemplatePreview -o tsv --query properties.state)
if [ $state != 'Registered' ]; then
echo -e "::error::Account not registered for VirtualMachineTemplatePreview\nPlease run this in your account:\n\t${BLUE}az feature register --namespace Microsoft.VirtualMachineImages --name VirtualMachineTemplatePreview${NC}"
errorState=1
else
echo "::debug::Feature registered!"
fi
exit $errorState
- name: Build VM Image
id: build-vm-image
uses: azure/build-vm-image@v0
with:
resource-group-name: '${{ github.event.inputs.resourceGroup }}'
managed-identity: '${{ secrets.IDENTITYFORVM }}'
location: 'northeurope'
source-os-type: 'linux'
source-image: Canonical:UbuntuServer:18.04-LTS:latest
dist-type: '${{ github.event.inputs.dist-type }}'
customizer-script: |
env
mkdir /opt/app/
cp -r /tmp/workflow-artifacts/* /opt/app/
echo '######### run install.sh'
cd /opt/app
sudo sh install.sh
- name: Generate url artifact
if: github.event.inputs.dist-type == 'VHD'
run: |
echo "${{ steps.build-vm-image.outputs.custom-image-uri }}" > vhd-url.txt
- name: Upload the url as a Build Artifact
uses: actions/upload-artifact@v2.2.3
if: github.event.inputs.dist-type == 'VHD'
with:
name: vhd_uri
# A file, directory or wildcard pattern that describes what to upload
path: "vhd-url.txt"
build-template:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Bicep Build
id: bicep-build
uses: aliencube/bicep-build-actions@v0.3
with:
files: template/template.bicep
- name: Check the result
shell: bash
run: |
shopt -s globstar
ls -altR **/template*.*
- name: Upload template as artifact
uses: actions/upload-artifact@v2.2.3
with:
name: template
path: "**/template*.json"
test:
runs-on: ubuntu-latest
environment:
name: ${{ github.event.inputs.environment }}
needs: [build-vm, build-template]
steps:
- name: Azure Login
uses: Azure/#@v1.1
with:
# Paste output of `az ad sp create-for-rbac` as value of secret variable: AZURE_CREDENTIALS
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Create VM From Image
id: create-vm-from-image
uses: Azure/cli@1.0.4
with:
inlineScript: |
groupName="app-vm-grp-${{ GITHUB.RUN_NUMBER }}"
region="northeurope"
az group create -n $groupName -l $region
if [ '${{ github.event.inputs.dist-type }}' = 'VHD' ]; then
image=$(echo "${{ needs.build-vm.outputs.imageUri }}" | cut -d "?" -f1)
imageId=$(az image create --name "img-${{ GITHUB.RUN_NUMBER }}" --resource-group $groupName --source $image --os-type Linux --storage-sku Standard_LRS --hyper-v-generation V2 --query id -o tsv)
else
imageId="${{ needs.build-vm.outputs.imageUri }}"
fi
ipaddr=$(az vm create --resource-group $groupName --name "app-vm-${{ GITHUB.RUN_NUMBER }}" --admin-username azureadmin --generate-ssh-keys --location $region \
--image $imageId --public-ip-address-allocation dynamic --query publicIpAddress -o tsv)
az vm open-port --port 80 -n "app-vm-${{ GITHUB.RUN_NUMBER }}" -g $groupName
echo "::set-output name=ipaddress::$ipaddr"
- name: Test VM
run: |
echo "Deployment finished. Checking ${{ steps.create-vm-from-image.outputs.ipaddress }} output"
echo -n "Warm up wait..."
sleep 15
echo "calling http endpoint.."
curl http://${{ steps.create-vm-from-image.outputs.ipaddress }}
- name: cleanup
uses: azure/CLI@1.0.4
with:
inlineScript: |
az group delete -n "app-vm-grp-${{ GITHUB.RUN_NUMBER }}" -y