Skip to content

Commit

Permalink
🚧Add auto update scripts (#255)
Browse files Browse the repository at this point in the history
* Start work for issue #254

* cleanup: remove unused script

* ci: create script to update repo variable

* ci: add step to release workflow to update cicd scripts repo variable

* ide: create launch config for update-cicd-script-version.ts script

* deps: update deno lock

* ci: convert vscode task script to cicd script

* ci: add step to update reusable workflow versions

* chore: remove unused exports

* deps: update deno lock

* ide: remove task

* ci: update launch config

* ide: add word to dictionary

* ci: remove branch check step

* ci: remove reusable workflow version check step

* ci: fix spelling mistake

* cleanup: remove unused imports

* ci: add deno permissions

* ci: fix spelling mistakes

* ide: adjust deno permissions for launch config

* ide: add words to dictionary

* ci: update release workflow

* ci: remove workflow version status check and associated scripts

* ci: update step names

* ci: update all action versions

* ci: improve step names

* ci: replace yarn with pnpm
  • Loading branch information
CalvinWilkinson authored Feb 9, 2025
1 parent 952904f commit 25c3110
Show file tree
Hide file tree
Showing 28 changed files with 282 additions and 284 deletions.
1 change: 0 additions & 1 deletion .github/internal-cicd/deno-tasks/clear-screen.ts

This file was deleted.

33 changes: 33 additions & 0 deletions .github/internal-cicd/scripts/update-cicd-script-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { RepoClient } from "jsr:@kinsondigital/[email protected]";
import getEnvVar from "../../../cicd/core/GetEnvVar.ts";
import { Utils } from "../../../cicd/core/Utils.ts";

const scriptFileName = new URL(import.meta.url).pathname.split("/").pop();
const ownerName = getEnvVar("OWNER_NAME", scriptFileName);
const repoName = getEnvVar("REPO_NAME", scriptFileName);
const token = getEnvVar("GITHUB_TOKEN", scriptFileName);
const cicdScriptVersionRepoVarName = getEnvVar("CICD_SCRIPTS_VERSION_REPO_VAR_NAME", scriptFileName);
const newVersion = getEnvVar("VERSION", scriptFileName);

try {
const client = new RepoClient(ownerName, repoName, token);
const exists = await client.variableExists(cicdScriptVersionRepoVarName);

if (!exists) {
console.error(`The repository variable '${cicdScriptVersionRepoVarName}' does not exist.`);
Deno.exit(1);
}

await client.updateVariable(cicdScriptVersionRepoVarName, newVersion);

Utils.printAsGitHubNotice(`Updated the repository variable '${cicdScriptVersionRepoVarName}' value to '${newVersion}'.`);
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
} else {
console.error("An error occurred.");
console.error(error);
}

Deno.exit(1);
}
73 changes: 26 additions & 47 deletions .github/internal-cicd/scripts/update-workflow-versions.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,43 @@
import { Directory, Input, RepoClient, TagClient } from "../../../deps.ts";
import chalk from "../../../deps.ts";
import { File } from "../../../cicd/core/File.ts";
import { Utils } from "../../../cicd/core/Utils.ts";
import { existsSync, walkSync } from "jsr:@std/[email protected]";
import { RepoClient } from "jsr:@kinsondigital/[email protected]";
import getEnvVar from "../../../cicd/core/GetEnvVar.ts";

const scriptFileName = new URL(import.meta.url).pathname.split("/").pop();

let baseDirPath = getEnvVar("BASE_DIR_PATH", scriptFileName);
const ownerName = getEnvVar("OWNER_NAME", scriptFileName);
const repoName = getEnvVar("REPO_NAME", scriptFileName);
const baseDirPath = getEnvVar("BASE_DIR_PATH", scriptFileName);
const token = getEnvVar("GITHUB_TOKEN", scriptFileName);
const newVersion = getEnvVar("NEW_VERSION", scriptFileName);

baseDirPath = Utils.normalizePath(baseDirPath.trim());

if (!Directory.Exists(baseDirPath)) {
console.log(chalk.red(`Directory '${baseDirPath}' does not exist.`));
if (!existsSync(baseDirPath)) {
console.log(`%cDirectory '${baseDirPath}' does not exist.`, "color: red");
Deno.exit(0);
}

// Clear the console so the token is not visible from the tasks.json file
console.clear();

const tagRegex = /v([1-9]\d*|0)\.([1-9]\d*|0)\.([1-9]\d*|0)(-preview\.([1-9]\d*))?/gm;

const newVersion = await Input.prompt({
message: chalk.blue("Enter version to upgrade workflows to:"),
hint: "Use a tag with the syntax 'v#.#.#'.",
minLength: 5,
validate: (value) => {
return tagRegex.test(value.trim().toLowerCase());
},
transform: (value) => {
value = value.trim().toLowerCase();

return value.startsWith("v") ? value : `v${value}`;
},
});

const ownerName = "KinsonDigital";
const repoName = "Infrastructure";
const allFiles = Directory.getFiles(baseDirPath, true);

const yamlFiles = allFiles.filter((file) => file.toLowerCase().endsWith(".yaml") || file.toLowerCase().endsWith(".yml"));
const tagClient: TagClient = new TagClient(ownerName, repoName, token);

const allTags = (await tagClient.getAllTags()).map((t) => t.name);

// If the new tag already exists, throw an error
if (allTags.includes(newVersion)) {
console.log(chalk.red(`Tag '${newVersion}' already exists.`));
if (!tagRegex.test(newVersion)) {
console.log(`%cThe version '${newVersion}' is not a valid version number.`, "color: red");
Deno.exit(0);
}


const walkResult = walkSync(baseDirPath, {
includeDirs: false,
includeFiles: true,
exts: [".yml", ".yaml"],
});

const yamlFiles = Array.from(walkResult).map((e) => e.path);
const reusableWorkflowRegex = /uses: .+.(yml|yaml)@v([1-9]\d*|0)\.([1-9]\d*|0)\.([1-9]\d*|0)(-preview\.([1-9]\d*))?/gm;

const updateMsgs: string[] = [];
let noFilesUpdated = true;

// Search for workflow references with a version that has not been updated
yamlFiles.forEach((yamlFile) => {
let fileContent = File.LoadFile(yamlFile);
let fileContent = Deno.readTextFileSync(yamlFile);

const possibleUpdates = fileContent.match(reusableWorkflowRegex)?.map((w) => w) ?? [];

Expand Down Expand Up @@ -86,32 +66,31 @@ yamlFiles.forEach((yamlFile) => {
// If anything has been updated, save the file
if (fileUpdated) {
// Save the changes to the file
File.SaveFile(yamlFile, fileContent);
Deno.writeTextFileSync(yamlFile, fileContent);
}
});

// If no files were updated
if (noFilesUpdated) {
console.log(chalk.cyan("No files needed updating."));
console.log("%cNo files needed updating.", "color: cyan");
} else {
updateMsgs.sort();
updateMsgs.forEach((updateMsg) => {
console.log(chalk.cyan(updateMsg));
console.log(`%c${updateMsg}`, "color: cyan");
});
}

const repoVarName = "CICD_SCRIPTS_VERSION";
const repoClient = new RepoClient(ownerName, repoName, token);

if (!(await repoClient.repoVariableExists(repoVarName))) {
console.log(chalk.red(`The repository variable '${repoVarName}' does not exist.`));
if (!(await repoClient.variableExists(repoVarName))) {
console.log(`%cThe repository variable '${repoVarName}' does not exist.`, "color: red");
Deno.exit(0);
}

const scriptVersionVar = (await repoClient.getVariables()).find((v) => v.name == repoVarName);

await repoClient.updateVariable(repoVarName, newVersion);

console.log(
chalk.cyan(`Updated repository variable '${repoVarName}' from version '${scriptVersionVar?.value}' to '${newVersion}'.`),
);
const msg = `%cUpdated repository variable '${repoVarName}' from version '${scriptVersionVar?.value}' to '${newVersion}'.`;
console.log(msg, "color: cyan");

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/add-item-to-project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
exit 1;
}
- name: Set Up Deno
- name: Set Up Deno (${{ vars.DENO_VERSION }})
uses: denoland/setup-deno@v2
with:
deno-version: ${{ vars.DENO_VERSION }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build-csharp-project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ jobs:
repository: ${{ inputs.checkout-repository }}
ref: ${{ inputs.checkout-ref }}

- name: Setup .NET SDK ${{ inputs.net-sdk-version }}
uses: actions/setup-dotnet@v3
- name: Set Up .NET SDK (${{ inputs.net-sdk-version }})
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ inputs.net-sdk-version }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-status-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}

- name: Setup Deno (${{ vars.DENO_VERSION }})
- name: Set Up Deno (${{ vars.DENO_VERSION }})
uses: denoland/setup-deno@v2
with:
deno-version: ${{ vars.DENO_VERSION }}
Expand Down
24 changes: 13 additions & 11 deletions .github/workflows/docusaurus-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,31 +116,33 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Setup Node (v${{ inputs.node-version }})
uses: actions/setup-node@v3
- name: Set Up Node (v${{ inputs.node-version }})
uses: actions/setup-node@v4
with:
node-version: ${{ inputs.node-version }}

- name: Setup YARN
run: npm install --global yarn
- name: Setup PNPM (${{ vars.PNPM_VERSION }})
uses: pnpm/action-setup@v4
with:
version: ${{ vars.PNPM_VERSION }}

- name: Setup Docusaurus
run: yarn install
- name: Set Up Docusaurus
run: pnpm install

- name: Build Site
run: yarn build
run: pnpm exec docusaurus build

- name: Setup Pages
uses: actions/configure-pages@v3
- name: Set Up Pages
uses: actions/configure-pages@v5

- name: Upload Artifact
uses: actions/upload-pages-artifact@v2.0.0
uses: actions/upload-pages-artifact@v3
with:
path: '${{ inputs.artifact-dir-path }}'

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
uses: actions/deploy-pages@v4

- name: Create Release Tag (${{ inputs.release-version }})
run: |
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/dotnet-action-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ jobs:
# Verify that the build config is only 'Debug' or 'Release'
if ($buildConfig -ne "Debug" -and $buildConfig -ne "Release") {
Write-Host "::error::The build configuration '$buildConfig' is invalid. Expsting a value of 'Debug' or 'Release'.";
Write-Host "::error::The build configuration '$buildConfig' is invalid. Expecting a value of 'Debug' or 'Release'.";
exit 1;
}
Expand Down Expand Up @@ -183,8 +183,7 @@ jobs:
uses: KinsonDigital/Infrastructure/.github/workflows/[email protected]
with:
repo-name: "${{ inputs.project-name }}"
secrets:
cicd-pat: "${{ secrets.cicd-pat }}"


validate_release_notes_exist:
name: Validate Release Notes Exist
Expand All @@ -198,7 +197,7 @@ jobs:
run: |
$relativeDirPath = "${{ inputs.relative-release-notes-dir-path }}";
# Remove all leading forwaard slashes until none are left
# Remove all leading forward slashes until none are left
$relativeDirPath = $relativeDirPath.Replace("\", "/");
$relativeDirPath = $relativeDirPath.TrimStart("/").TrimEnd("/");
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/dotnet-lib-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ jobs:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Set Up Deno
- name: Set Up Deno (${{ vars.DENO_VERSION }})
uses: denoland/setup-deno@v2
with:
deno-version: ${{ vars.DENO_VERSION }}
Expand Down Expand Up @@ -309,15 +309,15 @@ jobs:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Setup .NET SDK
uses: actions/setup-dotnet@v3
- name: Set Up .NET SDK
uses: actions/setup-dotnet@v4
with:
dotnet-version: "${{ inputs.net-sdk-version }}"

- name: Setup Nuget
uses: NuGet/setup-nuget@v1
- name: Set Up Nuget
uses: NuGet/setup-nuget@v2

- name: Set Up Deno
- name: Set Up Deno (${{ vars.DENO_VERSION }})
uses: denoland/setup-deno@v2
with:
deno-version: ${{ vars.DENO_VERSION }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/initial-manual-sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
"$stepOutput" >> $env:GITHUB_OUTPUT;
- name: Set Up Deno
- name: Set Up Deno (${{ vars.DENO_VERSION }})
if: ${{ steps.skip-checking.outputs.skip == 'false' }}
uses: denoland/setup-deno@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint-status-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Set Up Deno
- name: Set Up Deno (${{ vars.DENO_VERSION }})
uses: denoland/setup-deno@v2
with:
deno-version: ${{ vars.DENO_VERSION }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nuget-package-does-not-exist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
needs: print_validate_workflow
runs-on: ubuntu-latest
steps:
- name: Set Up Deno
- name: Set Up Deno (${{ vars.DENO_VERSION }})
uses: denoland/setup-deno@v2
with:
deno-version: ${{ vars.DENO_VERSION }}
Expand Down
Loading

0 comments on commit 25c3110

Please sign in to comment.