-
Notifications
You must be signed in to change notification settings - Fork 0
104 lines (93 loc) · 3.32 KB
/
reusable-workflow-packer-image.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
name: Reusable workflow - build packer VM
on:
workflow_call:
inputs:
image-name:
required: true
type: string
secrets:
AZURE_CLIENT_ID:
required: true
AZURE_CLIENT_SECRET:
required: true
AZURE_TENANT_ID:
required: true
AZURE_SUBSCRIPTION_ID:
required: true
jobs:
packer-build:
runs-on: self-hosted
env:
AZ_CLI_VERSION: 2.64.0
PACKER_VERSION: 1.9.4
AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}
AZURE_RESOURCE_GROUP: ${{ vars.AZURE_RESOURCE_GROUP }}
AZURE_ACG: ${{ vars.AZURE_ACG }}
AZURE_CREDENTIALS: |
{
"clientId": "${{ secrets.AZURE_CLIENT_ID }}",
"clientSecret": "${{ secrets.AZURE_CLIENT_SECRET }}",
"subscriptionId": "${{ secrets.AZURE_SUBSCRIPTION_ID }}",
"tenantId": "${{ secrets.AZURE_TENANT_ID }}"
}
IMAGE_NAME: ${{ inputs.image-name }}
IMAGE_PUBLISHER: "wp10-image-factory-user"
IMAGE_OFFER: "wp10-user-packer"
IMAGE_SKU: "v1"
IMAGE_OS_TYPE: "linux"
IMAGE_VERSION: "1.0.${{ github.run_number }}"
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
repository: comsysto/wp10-image-factory
- name: Checkout User Repository
uses: actions/checkout@v4
with:
path: user-repo
- name: Azure Login
uses: azure/login@v2
with:
creds: ${{ env.AZURE_CREDENTIALS }}
- name: Create Azure Image Definition
run: |
set +e
az sig image-definition show \
--resource-group "$AZURE_RESOURCE_GROUP" \
--gallery-name "$AZURE_ACG" \
--gallery-image-definition "$IMAGE_NAME" \
--query "name" -o tsv
az_exit_code=$?
set -e
if [ $az_exit_code -eq 3 ]; then
echo "Image definition does not exist. Creating it..."
az sig image-definition create \
--resource-group "$AZURE_RESOURCE_GROUP" \
--gallery-name "$AZURE_ACG" \
--gallery-image-definition "$IMAGE_NAME" \
--publisher "$IMAGE_PUBLISHER" \
--offer "$IMAGE_OFFER" \
--sku "$IMAGE_SKU" \
--os-type "$IMAGE_OS_TYPE"
else
echo "Image definition '$IMAGE_NAME' already exists."
fi
- name: Packer Init
run: |
ls -lha user-repo/packer/example-linux
cp user-repo/packer/example-linux/user-setup-vm.sh images/packer/user-packer/scripts/setup-vm.sh
ls -lha images/packer/user-packer
packer init images/packer/user-packer
- name: Packer Build
run: |
packer build \
-var "client_id=${{ secrets.AZURE_CLIENT_ID }}" \
-var "client_secret=${{ secrets.AZURE_CLIENT_SECRET }}" \
-var "tenant_id=${{ secrets.AZURE_TENANT_ID }}" \
-var "subscription_id=${{ secrets.AZURE_SUBSCRIPTION_ID }}" \
-var "gallery_image_name=${{ env.IMAGE_NAME }}" \
-var "resource_group=${{ env.AZURE_RESOURCE_GROUP }}" \
-var "gallery_name=${{ env.AZURE_ACG }}" \
-var "gallery_image_version=${{ env.IMAGE_VERSION }}" \
-var "location=${{ env.AZURE_LOCATION }}" \
images/packer/user-packer