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

Generate module metadata #1

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
56 changes: 56 additions & 0 deletions .github/workflows/get-modules-metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Get modules metadata
on:
pull_request:
branches:
- main
workflow_call:
secrets:
# Secrets must be passed from the caller workflow explicitly.
CLIENT_ID:
required: true
TENANT_ID:
required: true
SUBSCRIPTION_ID:
required: true
workflow_dispatch:

permissions:
id-token: write
contents: read

jobs:
main:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Install packages
run: npm ci

- name: Get modules metadata
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const script = require("./scripts/github-actions/get-modules-metadata.js")
await script({ require, core })

- name: Upload modules metadata
uses: actions/upload-artifact@v3
with:
name: modulesMetadata.json
path: modulesMetadata.json

- name: Log in to Azure
uses: azure/login@v1
with:
client-id: ${{ secrets.CLIENT_ID }}
tenant-id: ${{ secrets.TENANT_ID }}
subscription-id: ${{ secrets.SUBSCRIPTION_ID }}

- name: Upload to blob storage
uses: azure/CLI@v1
with:
inlineScript: |
az storage blob upload --account-name biceplivedatasadf --container-name bicep-cdn-live-data-container --name modulesMetadata --file modulesMetadata.json --auth-mode login --overwrite
40 changes: 40 additions & 0 deletions scripts/github-actions/get-modules-metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
async function getModulesMetadata({ require, core }) {
const fs = require("fs");
const getSubdirNames = require("./scripts/github-actions/get-sub-directory-names.js");
const moduleGroups = getSubdirNames(fs, "modules");

var result = [];

const path = require("path");
const axios = require("axios").default;

for (const moduleGroup of moduleGroups) {
var moduleGroupPath = path.join("modules", moduleGroup);
var moduleNames = getSubdirNames(fs, moduleGroupPath);

for (const moduleName of moduleNames) {
const modulePath = `${moduleGroup}/${moduleName}`;
const versionListUrl = `https://mcr.microsoft.com/v2/bicep/${modulePath}/tags/list`;

try {
const versionListResponse = await axios.default.get(versionListUrl);
const tags = versionListResponse.data.tags.sort();

result.push({
moduleName: modulePath,
tags: tags,
});
} catch (error) {
core.setFailed(error);
}
}
}

const newModulesMetadata = JSON.stringify(result, null, 2);

fs.writeFileSync("modulesMetadata.json", newModulesMetadata, (err) => {
if (err) throw err;
});
}

module.exports = getModulesMetadata;
12 changes: 12 additions & 0 deletions scripts/github-actions/get-sub-directory-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @param {typeof import("fs")} fs
* @param {string} dir
*/
function getSubdirNames(fs, dir) {
return fs
.readdirSync(dir, { withFileTypes: true })
.filter((x) => x.isDirectory())
.map((x) => x.name);
}

module.exports = getSubdirNames;
12 changes: 1 addition & 11 deletions scripts/github-actions/refresh-module-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,6 @@ function getTimestamp() {
return `${year}${month}${date}${hours}${minutes}${seconds}`;
}

/**
* @param {typeof import("fs")} fs
* @param {string} dir
*/
function getSubdirNames(fs, dir) {
return fs
.readdirSync(dir, { withFileTypes: true })
.filter((x) => x.isDirectory())
.map((x) => x.name);
}

/**
* @param {typeof import("axios").default} axios
* @param {typeof import("fs")} fs
Expand All @@ -29,6 +18,7 @@ function getSubdirNames(fs, dir) {
*/
async function generateModulesTable(axios, fs, path, core) {
const tableData = [["Module", "Version", "Docs"]];
const getSubdirNames = require("./scripts/github-actions/get-sub-directory-names.js");
const moduleGroups = getSubdirNames(fs, "modules");

for (const moduleGroup of moduleGroups) {
Expand Down