Skip to content

Commit 02f47cb

Browse files
authored
Add infrastructure to run as an action (#497)
1 parent 89e31b7 commit 02f47cb

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

.github/workflows/TestCase.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: "REST API repo scenario"
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
reason:
6+
description: "The reason for running the workflow"
7+
required: true
8+
default: "Manual run"
9+
10+
jobs:
11+
import:
12+
runs-on: ubuntu-latest
13+
permissions:
14+
id-token: write
15+
16+
steps:
17+
- name: "Print manual run reason"
18+
if: ${{ github.event_name == 'workflow_dispatch' }}
19+
run: |
20+
echo "Reason: ${{ github.event.inputs.reason }}"
21+
echo "Issue number: ${{ github.event.inputs.issue }}"
22+
23+
- name: Azure OpenID Connect
24+
id: azure-oidc-auth
25+
uses: dotnet/docs-tools/.github/actions/oidc-auth-flow@main
26+
with:
27+
client-id: ${{ secrets.CLIENT_ID }}
28+
tenant-id: ${{ secrets.TENANT_ID }}
29+
audience: ${{ secrets.OSMP_API_AUDIENCE }}
30+
31+
# This step occurs when ran manually, passing the manual issue number input
32+
- name: Test case
33+
id: test_case
34+
uses: dotnet/docs-tools/SmallRepo@main
35+
env:
36+
ImportOptions__ApiKeys__GitHubToken: ${{ secrets.GITHUB_TOKEN }}
37+
ImportOptions__ApiKeys__AzureAccessToken: ${{ steps.azure-oidc-auth.outputs.access-token }}
38+

SmallRepo/Program.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+

2+
using Microsoft.DotnetOrg.Ospo;
3+
4+
try
5+
{
6+
var azureAccessToken = CoalesceEnvVar(("ImportOptions__ApiKeys__AzureAccessToken", "AZURE_ACCESS_TOKEN"), false);
7+
8+
var ospoClient = new OspoClient(azureAccessToken, false);
9+
10+
var result = ospoClient.GetAsync("BillWagner");
11+
12+
13+
var id = await result;
14+
15+
if (id is not null)
16+
Console.WriteLine("Success");
17+
else
18+
Console.WriteLine("Failure");
19+
} catch (Exception e)
20+
{
21+
Console.WriteLine("Exception failure");
22+
Console.WriteLine(e.ToString());
23+
}
24+
25+
static string CoalesceEnvVar((string preferredKey, string fallbackKey) keys, bool required = true)
26+
{
27+
var (preferredKey, fallbackKey) = keys;
28+
29+
// Attempt the preferred key first.
30+
var value = Environment.GetEnvironmentVariable(preferredKey);
31+
if (string.IsNullOrWhiteSpace(value))
32+
{
33+
// If the preferred key is not set, try the fallback key.
34+
value = Environment.GetEnvironmentVariable(fallbackKey);
35+
Console.WriteLine($"{(string.IsNullOrWhiteSpace(value) ? $"Neither {preferredKey} or {fallbackKey} found" : $"Found {fallbackKey}")}");
36+
}
37+
else
38+
{
39+
Console.WriteLine($"Found value for {preferredKey}");
40+
}
41+
42+
// If neither key is set, throw an exception if required.
43+
if (string.IsNullOrWhiteSpace(value) && required)
44+
{
45+
throw new Exception(
46+
$"Missing env var, checked for both: {preferredKey} and {fallbackKey}.");
47+
}
48+
49+
return value!;
50+
}

SmallRepo/SmallRepo.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\DotNet.DocsTools\DotNet.DocsTools.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

SmallRepo/action.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: Small test case
2+
description: Small Test case against the OSPO REST API
3+
author: 'Bill Wagner'
4+
branding:
5+
icon: refresh-cw
6+
color: purple
7+
8+
runs:
9+
using: docker
10+
image: ../../smalltest.Dockerfile

smalltest.Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM mcr.microsoft.com/dotnet/sdk:9.0 as build-env
2+
# Copy everything and publish the release (publish implicitly restores and builds)
3+
WORKDIR /app
4+
COPY . ./
5+
RUN dotnet publish SmallRepo/SmallRepo.csproj" -c Release -o out --no-self-contained
6+
7+
# Relayer the .NET SDK, anew with the build output
8+
FROM mcr.microsoft.com/dotnet/sdk:9.0
9+
COPY --from=build-env /app/out .
10+
ENTRYPOINT [ "dotnet", "/SmallRepo.dll" ]

0 commit comments

Comments
 (0)