Skip to content

Commit

Permalink
Add current commit into logs for custom builds (actions#1419)
Browse files Browse the repository at this point in the history
* Show latest commit for custom repos

* Remove unused variables from templates

* Add Get-LatestCommit function

* Fix in synopsis

* Remove SSH_Password variables from Windows Images

* Rename download-customrepo

* Fix in synopsis
  • Loading branch information
vmapetr authored Aug 24, 2020
1 parent ba17c75 commit f97706e
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 40 deletions.
4 changes: 2 additions & 2 deletions helpers/CreateAzureVMFromPackerTemplate.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Function CreateAzureVMFromPackerTemplate {
<#
.SYNOPSIS
Creates an Azure VM from a template. Also generates network resources in Azure to make the VM accessible.
A helper function to deploy a VM from a generated image.
.DESCRIPTION
Creates Azure resources and kicks off a packer image generation for the selected image type.
Creates an Azure VM from a template. Also generates network resources in Azure to make the VM accessible.
.PARAMETER SubscriptionId
The Azure subscription Id where resources will be created.
Expand Down
12 changes: 12 additions & 0 deletions helpers/GenerateResourcesAndImage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ Function Get-PackerTemplatePath {
return $RepositoryRoot + $relativePath;
}

Function Get-LatestCommit {
[CmdletBinding()]
param()

process {
Write-Host "Latest commit:"
git log --pretty=format:"Date: %cd; Commit: %H - %s; Author: %an <%ae>" -1
}
}

Function GenerateResourcesAndImage {
<#
.SYNOPSIS
Expand Down Expand Up @@ -170,6 +180,8 @@ Function GenerateResourcesAndImage {
$tenantId = $sub.TenantId
# "", "Note this variable-setting script for running Packer with these Azure resources in the future:", "==============================================================================================", "`$spClientId = `"$spClientId`"", "`$ServicePrincipalClientSecret = `"$ServicePrincipalClientSecret`"", "`$SubscriptionId = `"$SubscriptionId`"", "`$tenantId = `"$tenantId`"", "`$spObjectId = `"$spObjectId`"", "`$AzureLocation = `"$AzureLocation`"", "`$ResourceGroupName = `"$ResourceGroupName`"", "`$storageAccountName = `"$storageAccountName`"", "`$install_password = `"$install_password`"", ""

Get-LatestCommit -ErrorAction SilentlyContinue

packer.exe build -on-error=ask `
-var "client_id=$($spClientId)" `
-var "client_secret=$($ServicePrincipalClientSecret)" `
Expand Down
11 changes: 4 additions & 7 deletions images.CI/azure-pipelines/image-generation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ jobs:
displayName: 'Download custom repository'
condition: and(ne(variables['CUSTOM_REPOSITORY_URL'], ''), ne(variables['CUSTOM_REPOSITORY_BRANCH'], ''))
inputs:
targetType: 'inline'
script: |
Write-Host "Clean up default repository"
Remove-Item -path './*' -Recurse -Force
Write-Host "Download $(CUSTOM_REPOSITORY_BRANCH) branch from $(CUSTOM_REPOSITORY_URL)"
$env:GIT_REDIRECT_STDERR = '2>&1'
git clone $(CUSTOM_REPOSITORY_URL) . -b $(CUSTOM_REPOSITORY_BRANCH) --single-branch --depth 1
targetType: 'filePath'
filePath: ./images.CI/download-repo.ps1
arguments: -RepoUrl $(CUSTOM_REPOSITORY_URL) `
-RepoBranch $(CUSTOM_REPOSITORY_BRANCH)

- task: PowerShell@2
displayName: 'Build VM'
Expand Down
14 changes: 14 additions & 0 deletions images.CI/download-repo.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
param(
[String] [Parameter (Mandatory=$true)] $RepoUrl,
[String] [Parameter (Mandatory=$true)] $RepoBranch
)

Write-Host "Clean up default repository"
Remove-Item -path './*' -Recurse -Force

Write-Host "Download ${RepoBranch} branch from ${RepoUrl}"
$env:GIT_REDIRECT_STDERR = '2>&1'
git clone $RepoUrl . -b $RepoBranch --single-branch --depth 1

Write-Host "Latest commit:"
git log --pretty=format:"Date: %cd; Commit: %H - %s; Author: %an <%ae>" -1
6 changes: 1 addition & 5 deletions images/linux/ubuntu1604.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"variables": {
"commit_url": "{{env `COMMIT_URL`}}",
"client_id": "{{env `ARM_CLIENT_ID`}}",
"client_secret": "{{env `ARM_CLIENT_SECRET`}}",
"subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}",
Expand All @@ -14,7 +13,6 @@
"virtual_network_subnet_name": "{{env `VNET_SUBNET`}}",
"private_virtual_network_with_public_ip": "{{env `PRIVATE_VIRTUAL_NETWORK_WITH_PUBLIC_IP`}}",
"image_folder": "/imagegeneration",
"commit_file": "/imagegeneration/commit.txt",
"imagedata_file": "/imagegeneration/imagedata.json",
"metadata_file": "/imagegeneration/metadatafile",
"installer_script_folder": "/imagegeneration/installers",
Expand Down Expand Up @@ -59,9 +57,7 @@
"type": "shell",
"inline": [
"mkdir {{user `image_folder`}}",
"chmod 777 {{user `image_folder`}}",
"echo {{user `commit_url`}} > {{user `commit_file`}}",
"chmod +r {{user `commit_file`}}"
"chmod 777 {{user `image_folder`}}"
],
"execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'"
},
Expand Down
6 changes: 1 addition & 5 deletions images/linux/ubuntu1804.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"variables": {
"commit_url": "{{env `COMMIT_URL`}}",
"client_id": "{{env `ARM_CLIENT_ID`}}",
"client_secret": "{{env `ARM_CLIENT_SECRET`}}",
"subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}",
Expand All @@ -14,7 +13,6 @@
"virtual_network_subnet_name": "{{env `VNET_SUBNET`}}",
"private_virtual_network_with_public_ip": "{{env `PRIVATE_VIRTUAL_NETWORK_WITH_PUBLIC_IP`}}",
"image_folder": "/imagegeneration",
"commit_file": "/imagegeneration/commit.txt",
"imagedata_file": "/imagegeneration/imagedata.json",
"metadata_file": "/imagegeneration/metadatafile",
"installer_script_folder": "/imagegeneration/installers",
Expand Down Expand Up @@ -59,9 +57,7 @@
"type": "shell",
"inline": [
"mkdir {{user `image_folder`}}",
"chmod 777 {{user `image_folder`}}",
"echo {{user `commit_url`}} > {{user `commit_file`}}",
"chmod +r {{user `commit_file`}}"
"chmod 777 {{user `image_folder`}}"
],
"execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'"
},
Expand Down
6 changes: 1 addition & 5 deletions images/linux/ubuntu2004.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"variables": {
"commit_url": "{{env `COMMIT_URL`}}",
"client_id": "{{env `ARM_CLIENT_ID`}}",
"client_secret": "{{env `ARM_CLIENT_SECRET`}}",
"subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}",
Expand All @@ -14,7 +13,6 @@
"virtual_network_subnet_name": "{{env `VNET_SUBNET`}}",
"private_virtual_network_with_public_ip": "{{env `PRIVATE_VIRTUAL_NETWORK_WITH_PUBLIC_IP`}}",
"image_folder": "/imagegeneration",
"commit_file": "/imagegeneration/commit.txt",
"imagedata_file": "/imagegeneration/imagedata.json",
"metadata_file": "/imagegeneration/metadatafile",
"installer_script_folder": "/imagegeneration/installers",
Expand Down Expand Up @@ -61,9 +59,7 @@
"type": "shell",
"inline": [
"mkdir {{user `image_folder`}}",
"chmod 777 {{user `image_folder`}}",
"echo {{user `commit_url`}} > {{user `commit_file`}}",
"chmod +r {{user `commit_file`}}"
"chmod 777 {{user `image_folder`}}"
],
"execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'"
},
Expand Down
10 changes: 2 additions & 8 deletions images/win/Windows2016-Azure.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"storage_account": "{{env `ARM_STORAGE_ACCOUNT`}}",
"temp_resource_group_name": "{{env `TEMP_RESOURCE_GROUP_NAME`}}",
"location": "{{env `ARM_RESOURCE_LOCATION`}}",
"ssh_password": "{{env `SSH_PASSWORD`}}",
"virtual_network_name": "{{env `VNET_NAME`}}",
"virtual_network_resource_group_name": "{{env `VNET_RESOURCE_GROUP`}}",
"virtual_network_subnet_name": "{{env `VNET_SUBNET`}}",
Expand All @@ -20,12 +19,9 @@
"root_folder": "C:",
"toolset_json_path": "{{env `TEMP`}}\\toolset.json",
"image_folder": "C:\\image",
"commit_file": "C:\\image\\commit.txt",
"imagedata_file": "C:\\imagedata.json",
"metadata_file": "C:\\image\\metadata.txt",
"helper_script_folder": "C:\\Program Files\\WindowsPowerShell\\Modules\\",
"psmodules_root_folder": "C:\\Modules",
"commit_url": "LATEST",
"install_user": "installer",
"install_password": null,
"capture_name_prefix": "packer",
Expand All @@ -34,7 +30,7 @@
"github_feed_token": "{{env `GITHUB_FEED_TOKEN`}}",
"announcements": "{{env `ANNOUNCEMENTS`}}"
},
"sensitive-variables": ["install_password", "ssh_password", "client_secret", "github_feed_token"],
"sensitive-variables": ["install_password", "client_secret", "github_feed_token"],
"builders": [
{
"name": "vhd",
Expand Down Expand Up @@ -70,9 +66,7 @@
{
"type": "powershell",
"inline":[
"New-Item -Path {{user `image_folder`}} -ItemType Directory -Force",
"Write-Output {{user `commit_url`}} > {{user `commit_file` }}",
"Write-Host (Get-Content -Path {{user `commit_file`}})"
"New-Item -Path {{user `image_folder`}} -ItemType Directory -Force"
]
},
{
Expand Down
10 changes: 2 additions & 8 deletions images/win/Windows2019-Azure.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"storage_account": "{{env `ARM_STORAGE_ACCOUNT`}}",
"temp_resource_group_name": "{{env `TEMP_RESOURCE_GROUP_NAME`}}",
"location": "{{env `ARM_RESOURCE_LOCATION`}}",
"ssh_password": "{{env `SSH_PASSWORD`}}",
"virtual_network_name": "{{env `VNET_NAME`}}",
"virtual_network_resource_group_name": "{{env `VNET_RESOURCE_GROUP`}}",
"virtual_network_subnet_name": "{{env `VNET_SUBNET`}}",
Expand All @@ -20,12 +19,9 @@
"root_folder": "C:",
"toolset_json_path": "{{env `TEMP`}}\\toolset.json",
"image_folder": "C:\\image",
"commit_file": "C:\\image\\commit.txt",
"imagedata_file": "C:\\imagedata.json",
"metadata_file": "C:\\image\\metadata.txt",
"helper_script_folder": "C:\\Program Files\\WindowsPowerShell\\Modules\\",
"psmodules_root_folder": "C:\\Modules",
"commit_id": "LATEST",
"install_user": "installer",
"install_password": null,
"capture_name_prefix": "packer",
Expand All @@ -34,7 +30,7 @@
"github_feed_token": "{{env `GITHUB_FEED_TOKEN`}}",
"announcements": "{{env `ANNOUNCEMENTS`}}"
},
"sensitive-variables": ["install_password", "ssh_password", "client_secret", "github_feed_token"],
"sensitive-variables": ["install_password", "client_secret", "github_feed_token"],
"builders": [
{
"name": "vhd",
Expand Down Expand Up @@ -70,9 +66,7 @@
{
"type": "powershell",
"inline":[
"New-Item -Path {{user `image_folder`}} -ItemType Directory -Force",
"Write-Output {{user `commit_id`}} > {{user `commit_file`}}",
"Write-Host (Get-Content -Path {{user `commit_file`}})"
"New-Item -Path {{user `image_folder`}} -ItemType Directory -Force"
]
},
{
Expand Down

0 comments on commit f97706e

Please sign in to comment.