Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add-telem #3

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions .github/actions/version-check/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
author: AVM
name: Module version check
description: Checks that module version has been updated on PR
inputs:
version-file-name:
description: Terraform JSON file containing module version
required: false
default: locals.version.tf.json
jq-query:
description: jq query to extract module version
required: false
default: .locals.module_version
github_token:
description: GitHub token
required: true
runs:
using: composite
steps:
- name: semver regex
shell: bash
run: |
echo SEMVER_REGEX="^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$" >> "$GITHUB_ENV"

- name: Get latest release version
shell: bash
run: |
VER=$(curl --silent -L -H "Authorization: Bearer ${{ inputs.github_token }}" -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/${{ github.repository }}/releases/latest | jq -r .name | sed s/^v//)
if [ "$VER" -eq "null" ]; then
echo "No releases found"
echo LATEST_RELEASE="0.0.0" >> "$GITHUB_ENV"
exit 0
fi
if echo "$VER" | grep -P -qv "$SEMVER_REGEX"; then
echo "Release version $VER is not a valid semantic version"
exit 1
fi
echo LATEST_RELEASE="$VER" >> "$GITHUB_ENV"

- name: Get current module version
shell: bash
run: |
VER=$(jq -r '${{ inputs.jq-query }}' < ${{ inputs.version-file-name }})
if echo "$VER" | grep -P -qv "$SEMVER_REGEX"; then
echo "Module version $VER is not a valid semantic version"
exit 1
fi
echo MODULE_VERSION="$VER" >> "$GITHUB_ENV"

- name: Check module version is greater than latest release
shell: bash
run: |
MODVERMAJOR=$(echo ${{ env.MODULE_VERSION }} | cut -d. -f1)
MODVERMINOR=$(echo ${{ env.MODULE_VERSION }} | cut -d. -f2)
MODVERPATCH=$(echo ${{ env.MODULE_VERSION }} | cut -d. -f3)

RELVERMAJOR=$(echo ${{ env.LATEST_RELEASE }} | cut -d. -f1)
RELVERMINOR=$(echo ${{ env.LATEST_RELEASE }} | cut -d. -f2)
RELVERPATCH=$(echo ${{ env.LATEST_RELEASE }} | cut -d. -f3)

if [ "$MODVERMAJOR" -lt "$RELVERMAJOR" ]; then
echo "Module version ${{ env.MODULE_VERSION }} is less than latest release ${{ env.LATEST_RELEASE }}"
exit 1
fi

if [ "$MODVERMAJOR" -eq "$RELVERMAJOR" ] && [ "$MODVERMINOR" -lt "$RELVERMINOR" ]; then
echo "Module version ${{ env.MODULE_VERSION }} is less than latest release ${{ env.LATEST_RELEASE }}"
exit 1
fi

if [ "$MODVERMAJOR" -eq "$RELVERMAJOR" ] && [ "$MODVERMINOR" -eq "$RELVERMINOR" ] && [ "$MODVERPATCH" -lt "$RELVERPATCH" ]; then
echo "Module version ${{ env.MODULE_VERSION }} is less than latest release ${{ env.LATEST_RELEASE }}"
exit 1
fi
Empty file added .github/workflows/.gitkeep
Empty file.
16 changes: 16 additions & 0 deletions .github/workflows/version-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
name: version-check

on:
workflow_dispatch:
pull_request:
branches:
- main

jobs:
version-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check version
uses: https://github.com/Azure/terraform-azurerm-avm-template/.github/actions/version-check@main
46 changes: 46 additions & 0 deletions locals.telemetry.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
locals {
# This is the unique id for the module that is supplied by the AVM team.
# TODO: change this to the PUID for the module. See https://azure.github.io/Azure-Verified-Modules/specs/shared/#id-sfr3---category-telemetry---deploymentusage-telemetry
telem_puid = "00000000"

# TODO: change this to the name of the module. See https://azure.github.io/Azure-Verified-Modules/specs/shared/#id-sfr3---category-telemetry---deploymentusage-telemetry
module_name = "CHANGEME"

# TODO: Change this. Should be either `res` or `ptn`
module_type = "res"

# This ensures we don't get errors if telemetry is disabled.
telem_random_hex = can(random_id.telem[0].hex) ? random_id.telem[0].hex : ""

# This constructs the ARM deployment name that is used for the telemetry.
# We shouldn't ever hit the 64 character limit but use substr just in case.
telem_arm_deployment_name = substr(
format(
"%s.%s.%s.v%s.%s",
local.telem_puid,
local.module_type,
substr(local.module_name, 0, 30),
replace(local.module_version, ".", "-"),
local.telem_random_hex
),
0,
64
)

# This is an empty ARM deployment template.
telem_arm_template_content = <<TEMPLATE
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {},
"resources": [],
"outputs": {
"telemetry": {
"type": "String",
"value": "For more information, see https://aka.ms/avm/telemetry"
}
}
}
TEMPLATE
}
5 changes: 5 additions & 0 deletions locals.version.tf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"locals": {
"module_version": "0.1.0"
}
}
15 changes: 15 additions & 0 deletions main.telemetry.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
resource "random_id" "telemetry" {
count = local.enable_telemetry ? 1 : 0
byte_length = 4
}

# This is the module telemetry deployment that is only created if telemetry is enabled.
# It is deployed to the resource's resource group.
resource "azurerm_resource_group_template_deployment" "telemetry" {
count = var.enable_telemetry ? 1 : 0
name = local.telem_arm_deployment_name
resource_group_name = var.resource_group_name
deployment_mode = "Incremental"
location = var.location
template_content = local.telem_arm_template_content
}
Loading