-
Notifications
You must be signed in to change notification settings - Fork 9
137 lines (119 loc) · 4.09 KB
/
packer.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
name: Packer
on:
push:
branches:
- main
schedule:
- cron: 0 0 * * *
env:
AZ_CLI_VERSION: 2.40.0
IMAGE_PUBLISHER: MicrosoftWindowsDesktop
# With Office 365
IMAGE_OFFER: office-365
IMAGE_SKU: win11-23h2-avd-m365
# Without Office 365
#IMAGE_OFFER: windows-11
#IMAGE_SKU: win11-23h2-avd
jobs:
latest_windows_version:
name: Get latest Windows version from Azure
runs-on: ubuntu-latest
outputs:
version: ${{ steps.get_latest_version.outputs.version }}
steps:
- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Get Latest Version
id: get_latest_version
uses: azure/cli@v2
with:
azcliversion: ${{ env.AZ_CLI_VERSION }}
inlineScript: |
latest_version=$(
az vm image show \
--urn "${IMAGE_PUBLISHER}:${IMAGE_OFFER}:${IMAGE_SKU}:latest" \
--query name \
--out tsv
)
echo "Publisher: ${IMAGE_PUBLISHER}"
echo "Offer: ${IMAGE_OFFER}"
echo "SKU: ${IMAGE_SKU}"
echo "Version: ${latest_version}"
echo "version=${latest_version}" >> $GITHUB_OUTPUT
check_image_exists:
name: Check if latest version has already been built
runs-on: ubuntu-latest
needs: latest_windows_version
outputs:
exists: ${{ steps.get_image.outputs.exists }}
steps:
- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Check If Image Exists
id: get_image
uses: azure/cli@v2
with:
azcliversion: ${{ env.AZ_CLI_VERSION }}
inlineScript: |
if az image show \
--resource-group "${{ secrets.PACKER_ARTIFACTS_RESOURCE_GROUP }}" \
--name "${IMAGE_SKU}-${{ needs.latest_windows_version.outputs.version }}"; then
image_exists=true
else
image_exists=false
fi
echo "Image Exists: ${image_exists}"
echo "exists=${image_exists}" >> $GITHUB_OUTPUT
packer:
name: Run Packer
runs-on: ubuntu-latest
needs: [latest_windows_version, check_image_exists]
if: needs.check_image_exists.outputs.exists == 'false'
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Validate Packer Template
uses: hashicorp/packer-github-actions@master
with:
command: validate
arguments: -syntax-only
- name: Build Packer Image
uses: hashicorp/packer-github-actions@master
with:
command: build
arguments: -color=false -on-error=abort
env:
PKR_VAR_client_id: ${{ secrets.PACKER_CLIENT_ID }}
PKR_VAR_client_secret: ${{ secrets.PACKER_CLIENT_SECRET }}
PKR_VAR_subscription_id: ${{ secrets.PACKER_SUBSCRIPTION_ID }}
PKR_VAR_tenant_id: ${{ secrets.PACKER_TENANT_ID }}
PKR_VAR_artifacts_resource_group: ${{ secrets.PACKER_ARTIFACTS_RESOURCE_GROUP }}
PKR_VAR_build_resource_group: ${{ secrets.PACKER_BUILD_RESOURCE_GROUP }}
PKR_VAR_source_image_publisher: ${{ env.IMAGE_PUBLISHER }}
PKR_VAR_source_image_offer: ${{ env.IMAGE_OFFER }}
PKR_VAR_source_image_sku: ${{ env.IMAGE_SKU }}
PKR_VAR_source_image_version: ${{ needs.latest_windows_version.outputs.version }}
cleanup:
name: Cleanup Packer Resources
runs-on: ubuntu-latest
needs: packer
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Cleanup Resource Group
uses: azure/cli@v2
with:
azcliversion: ${{ env.AZ_CLI_VERSION }}
inlineScript: |
az deployment group create \
--mode Complete \
--resource-group "${{ secrets.PACKER_BUILD_RESOURCE_GROUP }}" \
--template-file cleanup-resource-group.bicep