Scenario/agents/setup #12
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Run Setup | |
# This action can be run on-demand against a branch. | |
# It attempts to auto-fix some errors in the most recently updated sample in the branch: | |
# Builds main.bicep -> azuredeploy.json | |
# Attempts some fixes with the metadata | |
# To run/debug locally, try /~https://github.com/nektos/act | |
# Actions documentation: https://docs.github.com/en/actions/reference | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
workflow_dispatch: | |
permissions: | |
contents: read | |
jobs: | |
check-if-external: | |
runs-on: ubuntu-latest | |
outputs: | |
environment: ${{ steps.set-environment.outputs.result }} | |
steps: | |
- uses: actions/github-script@v7 | |
id: set-environment | |
with: | |
script: | | |
const actionInitiator = context.payload.sender.login; | |
const org = "Azure-Samples"; | |
let isPublicMember = true; | |
// Check if initiator is a public member of the org | |
try { | |
await github.rest.orgs.checkPublicMembershipForUser({ | |
org, | |
username: actionInitiator | |
}); | |
} catch (error) { | |
if (error.status != 404) { | |
throw new Error("Unknown error", {cause: error}); | |
} | |
console.debug([ | |
`User is not a public member of the organization "${org}"`, | |
"", | |
`If you are a Microsoft employee, you can join the "${org}" org and set your org membership visibility to public: https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-personal-account-on-github/managing-your-membership-in-organizations/publicizing-or-hiding-organization-membership#changing-the-visibility-of-your-organization-membership` | |
].join("\n")); | |
isPublicMember = false; | |
} | |
const isPullRequestEvent = ["pull_request", "pull_request_target"].includes(context.eventName); | |
if (!(isPublicMember && isPullRequestEvent)) { | |
return "external-contribution"; | |
} | |
return ""; | |
result-encoding: string | |
main: | |
permissions: | |
contents: write # for git push | |
name: Run Setup | |
runs-on: ubuntu-latest | |
env: | |
# don't print dotnet logo | |
DOTNET_NOLOGO: true | |
# disable telemetry (reduces dotnet tool output in logs) | |
DOTNET_CLI_TELEMETRY_OPTOUT: true | |
steps: | |
- name: Setup .NET Core | |
uses: actions/setup-dotnet@v1.8.0 | |
- name: Install Bicep | |
run: | | |
# See /~https://github.com/Azure/bicep/blob/main/docs/installing.md#windows-installer | |
# Create the install folder | |
INSTALL_PATH="$RUNNER_TEMP/bicep" | |
BICEP_PATH="$RUNNER_TEMP/bicep/bicep" | |
mkdir -p $INSTALL_PATH | |
# Fetch the latest Bicep CLI binary | |
curl -sLo bicep /~https://github.com/Azure/bicep/releases/latest/download/bicep-linux-x64 | |
chmod +x ./bicep | |
sudo mv ./bicep $INSTALL_PATH | |
echo Using bicep at $BICEP_PATH: | |
$BICEP_PATH --version | |
echo "BICEP_PATH=$BICEP_PATH" >> $GITHUB_ENV | |
- name: Install PowerShell | |
run: | | |
# Update the list of packages | |
sudo apt-get update | |
# Install pre-requisite packages. | |
sudo apt-get install -y wget apt-transport-https software-properties-common | |
# Download the Microsoft repository GPG keys | |
wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb | |
# Register the Microsoft repository GPG keys | |
sudo dpkg -i packages-microsoft-prod.deb | |
# Update the list of packages after we added packages.microsoft.com | |
sudo apt-get update | |
# Install PowerShell | |
sudo apt-get install -y powershell | |
- name: Sync main | |
uses: actions/checkout@v2.3.4 | |
with: | |
ref: main | |
fetch-depth: 0 # avoid shallow clone so nbgv can do its work. | |
- uses: actions/checkout@v2.3.4 | |
with: | |
fetch-depth: 0 # avoid shallow clone so nbgv can do its work. | |
- name: Find and scan Bicep files | |
run: | | |
git status | |
pwd | |
cd $GITHUB_WORKSPACE | |
pwd | |
# Get all modified files in the last 100 commits, filtering out .github | |
MODIFIED_FILES=$(git log --pretty="" --name-only -n 100 origin/main... | fgrep -v ".github") | |
if [ -z "$MODIFIED_FILES" ]; then | |
echo "No modified files found." | |
exit 1 | |
fi | |
# Convert MODIFIED_FILES into an array | |
IFS=$'\n' read -r -a MODIFIED_FILES_ARRAY <<< "$MODIFIED_FILES" | |
# Initialize arrays for environment variables | |
MAINBICEP_PATHS=() | |
AZDEPLOYJSON_PATHS=() | |
for FILE in "${MODIFIED_FILES_ARRAY[@]}"; do | |
echo "Processing: $FILE" | |
TESTFOLDER_PATH=$(dirname "$FILE") | |
FOUNDBICEP_PATH="" | |
FOUNDJSON_PATH="" | |
# Traverse up the directory structure to find main.bicep and azuredeploy.json | |
while [ "$TESTFOLDER_PATH" != "." ]; do | |
echo "Looking in: $TESTFOLDER_PATH" | |
if [ -z "$FOUNDBICEP_PATH" ] && [ -f "$TESTFOLDER_PATH/main.bicep" ]; then | |
FOUNDBICEP_PATH="$TESTFOLDER_PATH/main.bicep" | |
echo "Found main.bicep: $FOUNDBICEP_PATH" | |
fi | |
if [ -z "$FOUNDJSON_PATH" ] && [ -f "$TESTFOLDER_PATH/azuredeploy.json" ]; then | |
FOUNDJSON_PATH="$TESTFOLDER_PATH/azuredeploy.json" | |
echo "Found azuredeploy.json: $FOUNDJSON_PATH" | |
fi | |
# If both files are found, stop searching | |
if [ -n "$FOUNDBICEP_PATH" ] && [ -n "$FOUNDJSON_PATH" ]; then | |
break | |
fi | |
TESTFOLDER_PATH=$(dirname "$TESTFOLDER_PATH") | |
done | |
if [ -n "$FOUNDBICEP_PATH" ]; then | |
MAINBICEP_PATHS+=("$FOUNDBICEP_PATH") | |
fi | |
if [ -n "$FOUNDJSON_PATH" ]; then | |
AZDEPLOYJSON_PATHS+=("$FOUNDJSON_PATH") | |
fi | |
done | |
# Store paths as space-separated strings in GitHub environment variables | |
echo "MAINBICEP_PATHS=${MAINBICEP_PATHS[*]}" >> $GITHUB_ENV | |
echo "AZDEPLOYJSON_PATHS=${AZDEPLOYJSON_PATHS[*]}" >> $GITHUB_ENV | |
- name: Build Bicep files | |
run: | | |
# Convert space-separated environment variables back to arrays | |
IFS=' ' read -ra MAINBICEP_PATHS_ARRAY <<< "$MAINBICEP_PATHS" | |
IFS=' ' read -ra AZDEPLOYJSON_PATHS_ARRAY <<< "$AZDEPLOYJSON_PATHS" | |
# Ensure the number of Bicep files and JSON paths match | |
if [ ${#MAINBICEP_PATHS_ARRAY[@]} -ne ${#AZDEPLOYJSON_PATHS_ARRAY[@]} ]; then | |
echo "Error: Mismatch in number of Bicep files and output JSON paths." | |
exit 1 | |
fi | |
for i in "${!MAINBICEP_PATHS_ARRAY[@]}"; do | |
MAINBICEP_FILE="${MAINBICEP_PATHS_ARRAY[$i]}" | |
AZDEPLOYJSON_FILE="${AZDEPLOYJSON_PATHS_ARRAY[$i]}" | |
if [ -f "$MAINBICEP_FILE" ]; then | |
echo "Building: $MAINBICEP_FILE -> $AZDEPLOYJSON_FILE" | |
$BICEP_PATH build "$MAINBICEP_FILE" --outfile "$AZDEPLOYJSON_FILE" | |
else | |
echo "Warning: Bicep file not found: $MAINBICEP_FILE" | |
fi | |
done | |
- name: Commit changes | |
if: always() | |
run: | | |
git config --global user.email "azureai-samples@noreply.github.com" | |
git config --global user.name "azureai-sample Automation" | |
git add . | |
if ! git diff-index --quiet HEAD --; then | |
git commit -m "Automatic fixes" | |
git push | |
fi |