Packer #393
This file contains 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
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 |