1+ name : Sign PowerShell Script
2+
3+ on :
4+ push :
5+ branches : [main, dev]
6+ paths : ['setup_venv.ps1']
7+ release :
8+ types : [published]
9+
10+ jobs :
11+ sign-script :
12+ runs-on : windows-latest
13+ permissions :
14+ contents : write
15+ steps :
16+ - uses : actions/checkout@v4
17+ with :
18+ token : ${{ secrets.GITHUB_TOKEN }}
19+
20+ - name : Create self-signed certificate
21+ shell : powershell
22+ run : |
23+ Write-Host "Creating self-signed code signing certificate..."
24+ $cert = New-SelfSignedCertificate -Subject "CN=Infoblox Universal DDI Setup" `
25+ -Type CodeSigningCert -CertStoreLocation Cert:\CurrentUser\My `
26+ -NotAfter (Get-Date).AddYears(1)
27+ Write-Host "Certificate created with thumbprint: $($cert.Thumbprint)"
28+
29+ # Export certificate for potential manual import
30+ $cert | Export-Certificate -FilePath cert.cer -Type CERT
31+ Write-Host "Certificate exported to cert.cer"
32+
33+ - name : Sign PowerShell script
34+ shell : powershell
35+ run : |
36+ Write-Host "Signing setup_venv.ps1..."
37+ $cert = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert | Select-Object -First 1
38+ if (-not $cert) {
39+ throw "No code signing certificate found"
40+ }
41+
42+ Set-AuthenticodeSignature -FilePath "setup_venv.ps1" -Certificate $cert
43+ Write-Host "Script signed successfully"
44+
45+ - name : Verify signature
46+ shell : powershell
47+ run : |
48+ Write-Host "Verifying script signature..."
49+ $signature = Get-AuthenticodeSignature -FilePath "setup_venv.ps1"
50+ Write-Host "Signature status: $($signature.Status)"
51+
52+ if ($signature.Status -ne "Valid") {
53+ Write-Host "Signature details: $($signature | Format-List | Out-String)"
54+ throw "Script signature is not valid"
55+ }
56+
57+ Write-Host "Signature verification successful"
58+
59+ - name : Commit signed script
60+ if : github.event_name == 'push'
61+ run : |
62+ Write-Host "Checking for changes to commit..."
63+ git config --local user.email "action@github.com"
64+ git config --local user.name "GitHub Action"
65+ git add setup_venv.ps1 cert.cer
66+ git status
67+
68+ # Only commit if there are changes
69+ if (git diff --staged --quiet) {
70+ Write-Host "No changes to commit"
71+ } else {
72+ git commit -m "Sign PowerShell script with self-signed certificate [skip ci]"
73+ Write-Host "Changes committed, pushing..."
74+ git push
75+ Write-Host "Signed script committed and pushed"
76+ }
77+
78+ - name : Upload signed script and certificate
79+ if : github.event_name == 'release'
80+ uses : actions/upload-artifact@v4
81+ with :
82+ name : signed-setup-venv-${{ github.event.release.tag_name }}
83+ path : |
84+ setup_venv.ps1
85+ cert.cer
0 commit comments