📌 GitHub Actions Workflow & PowerShell
Script Documentation
📍 1. Introduction
🔹 What is GitHub Actions?
GitHub Actions is a CI/CD (Continuous Integration/Continuous Deployment) tool that
allows developers to automate workflows directly within a GitHub repository. It can:
Run scripts automatically when certain events occur (like a code push or pull request).
Build and test applications.
Deploy code to production environments.
🔹 What Does This Project Do?
This project uses GitHub Actions & PowerShell to:
1. Track commits made to the repository.
2. Store commit details (author, date, changed files) in commit-details.json.
3. Build and test a .NET Framework 4.7.2 project.
4. Push commit details back to the repository automatically.
📍 2. How the Workflow Works
🔹 Workflow Triggers
The workflow runs when:
A new commit is pushed to the main branch.
A pull request is created for the main branch.
🔹 Overview of the Workflow Execution
1️⃣ GitHub Actions starts running when a commit is made.
2️⃣ The repository is checked out into a virtual machine (Windows).
3️⃣ A PowerShell script (fetch_commit.ps1) is executed to fetch commit details.
4️⃣ Commit details are stored in commit-details.json.
5️⃣ The JSON file is committed back to the repository.
6️⃣ The .NET project is built and tested using dotnet.yml.
📍 3. GitHub Actions Workflow (commit-info.yml)
🔹 Purpose
This workflow fetches commit details and updates commit-details.json.
🔹 Workflow Code Explanation
name: Fetch Commit Details
on:
push:
branches:
- main
pull_request:
branches:
- main
✅ This section defines when the workflow should run – on push or pull_request events.
jobs:
fetch_commit_details:
runs-on: windows-latest
✅ The job runs on a Windows virtual machine.
- name: Checkout Repository
uses: actions/checkout@v3
with:
fetch-depth: 0
✅ Checks out the repository so files are accessible in GitHub Actions.
- name: Fetch Commit Details
run: pwsh -ExecutionPolicy Bypass -File ./scripts/fetch_commit.ps1
env:
MY_GITHUB_TOKEN: ${{ secrets.MY_GITHUB_TOKEN }}
✅ Runs the PowerShell script to fetch commit details.
- name: Upload Commit Details
uses: actions/upload-artifact@v4
with:
name: commit-details
path: artifacts/commit-details.json
✅ Saves commit-details.json as an artifact in GitHub Actions.
- name: Commit & Push Updated JSON to GitHub
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-
actions[bot]@users.noreply.github.com"
git add artifacts/commit-details.json
git commit -m "🤖 Auto-update commit-details.json after workflow run" ||
echo "No changes to commit"
git push https://x-access-token:$
{{ secrets.GITHUB_TOKEN }}@github.com/KUMAR-PRABHAKAR/Appointment-
Scheduling.git main
✅ Commits and pushes updated commit details to the repository.
📍 4. GitHub Actions Workflow (dotnet.yml)
This workflow builds and tests the .NET Framework project.
- name: Restore dependencies
run: dotnet restore
✅ Restores NuGet dependencies.
- name: Build the project
run: dotnet build --no-restore --configuration Release
✅ Builds the project in Release mode.
- name: Run unit tests
run: dotnet test --no-build --verbosity normal
✅ Runs tests to verify the project's correctness.
📍 5. PowerShell Script (fetch_commit.ps1)
🔹 Purpose
This script:
Fetches commit details from GitHub.
Extracts commit SHA, message, author, and changed files.
Appends the data to commit-details.json.
🔹 Script Breakdown
$orgURL = "https://api.github.com"
$repoOwner = "KUMAR-PRABHAKAR"
$repoName = "Appointment-Scheduling"
$branchName = "main"
✅ Defines repository details.
$myToken = $env:MY_GITHUB_TOKEN
if ([string]::IsNullOrEmpty($myToken)) {
Write-Host "❌ ERROR: GitHub Token is missing!"
exit 1
}
✅ Ensures authentication using GitHub token.
$shelveSetURL = "$orgURL/repos/$repoOwner/$repoName/commits?
sha=$branchName"
$shelveSetinfo = Invoke-RestMethod -Uri $shelveSetURL -Method Get -Headers
$header
✅ Fetches latest commits from GitHub.
$latestCommit = $shelveSetinfo[0]
$commitSha = $latestCommit.sha
$commitMessage = $latestCommit.commit.message
$commitAuthor = $latestCommit.commit.author.name
$commitDate = $latestCommit.commit.author.date
✅ Extracts commit details.
$existingData = Get-Content -Raw -Path $jsonFilePath | ConvertFrom-Json
$updatedData = $existingData + $newCommit
$updatedData | ConvertTo-Json -Depth 3 | Set-Content -Encoding utf8
$jsonFilePath
✅ Reads existing commit data and appends new commits.
📍 6. Expected Output (commit-details.json)
✅ Every new commit is logged automatically.
📍 7. Conclusion & Next Steps
✅ Run manual tests to verify everything is working.
✅ Ensure GitHub Actions has "Read and Write" permissions.
✅ Monitor commit-details.json updates in GitHub.
🚀 This workflow ensures that every commit is logged, built, tested, and stored automatically!
🎯