Skip to content

Commit 6fe440c

Browse files
Script refactoring
- Use `${env:}` format to access environment variables for consistency. - Avoid interpolation. - Remove redundant quoting. - Use single quotes in YAML. - Refactor permissions.
1 parent 39e640d commit 6fe440c

File tree

10 files changed

+81
-60
lines changed

10 files changed

+81
-60
lines changed

.github/workflows/approve-and-merge.yml

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ on:
55
branches: [ main, dotnet-vnext ]
66

77
env:
8+
POWERSHELL_YAML_VERSION: '0.4.12'
89
REVIEWER_LOGIN: ${{ vars.REVIEWER_USER_NAME }}
910

10-
permissions:
11-
contents: read
11+
permissions: {}
1212

1313
jobs:
1414
review-pull-request:
1515
runs-on: ubuntu-latest
16-
if: ${{ github.event.pull_request.user.login == vars.UPDATER_COMMIT_USER_NAME }}
16+
if: github.event.pull_request.user.login == vars.UPDATER_COMMIT_USER_NAME
17+
18+
permissions:
19+
contents: read
1720

1821
steps:
1922

@@ -23,31 +26,33 @@ jobs:
2326
with:
2427
application_id: ${{ secrets.REVIEWER_APPLICATION_ID }}
2528
application_private_key: ${{ secrets.REVIEWER_APPLICATION_PRIVATE_KEY }}
26-
permissions: "contents:write, pull_requests:write"
29+
permissions: 'contents:write, pull_requests:write'
2730

2831
- name: Install powershell-yaml
2932
shell: pwsh
30-
run: Install-Module -Name powershell-yaml -Force -MaximumVersion "0.4.7"
33+
run: Install-Module -Name powershell-yaml -Force -MaximumVersion ${env:POWERSHELL_YAML_VERSION}
3134

3235
- name: Check which dependencies were updated
3336
id: check-dependencies
3437
env:
3538
# This list of trusted package prefixes needs to stay in sync with include-nuget-packages in the update-dotnet-sdk workflow.
36-
INCLUDE_NUGET_PACKAGES: "Microsoft.AspNetCore.,Microsoft.EntityFrameworkCore.,Microsoft.Extensions.,Microsoft.NET.Test.Sdk"
39+
INCLUDE_NUGET_PACKAGES: 'Microsoft.AspNetCore.,Microsoft.EntityFrameworkCore.,Microsoft.Extensions.,Microsoft.NET.Test.Sdk'
3740
GH_TOKEN: ${{ steps.generate-application-token.outputs.token }}
41+
PR_NUMBER: ${{ github.event.pull_request.number }}
42+
UPDATER_COMMIT_USER_NAME: ${{ vars.UPDATER_COMMIT_USER_NAME }}
3843
shell: pwsh
3944
run: |
4045
# Replicate the logic in the dependabot/fetch-metadata action.
4146
# See https://github.com/dependabot/fetch-metadata/blob/aea2135c95039f05c64436f1d14638c300e10b2b/src/dependabot/update_metadata.ts#L29-L68.
4247
# Query the GitHub API to get the commits in the pull request.
4348
$commits = gh api `
44-
/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/commits `
49+
"/repos/${env:GITHUB_REPOSITORY}/pulls/${env:PR_NUMBER}/commits" `
4550
--jq '.[] | { author: .author.login, message: .commit.message }' | ConvertFrom-Json
4651
4752
# We should only approve pull requests that only contain commits from
4853
# the GitHub user we expected and only commits that contain the metadata
4954
# we need to determine what dependencies were updated by the other workflow.
50-
$expectedUser = "${{ vars.UPDATER_COMMIT_USER_NAME }}"
55+
$expectedUser = "${env:UPDATER_COMMIT_USER_NAME}"
5156
$onlyDependencyUpdates = $True
5257
$onlyChangesFromUser = $True
5358
@@ -82,7 +87,7 @@ jobs:
8287
# Did we find at least one dependency?
8388
$isPatch = $dependencies.Length -gt 0
8489
$onlyTrusted = $dependencies.Length -gt 0
85-
$trustedPackages = $env:INCLUDE_NUGET_PACKAGES.Split(',')
90+
$trustedPackages = ${env:INCLUDE_NUGET_PACKAGES}.Split(',')
8691
8792
foreach ($dependency in $dependencies) {
8893
$isPatch = $isPatch -And $dependency.Type -eq "version-update:semver-patch"
@@ -98,31 +103,35 @@ jobs:
98103
# Microsoft-published NuGet packages that were made by the GitHub
99104
# login we expect to make those changes in the other workflow.
100105
$isTrusted = (($onlyTrusted -And $isPatch) -And $onlyChangesFromUser) -And $onlyDependencyUpdates
101-
"is-trusted-update=$isTrusted" >> $env:GITHUB_OUTPUT
106+
"is-trusted-update=$isTrusted" >> ${env:GITHUB_OUTPUT}
102107
103108
- name: Checkout code
104109
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
110+
with:
111+
filter: 'tree:0'
112+
show-progress: false
105113

106114
# As long as it's not already approved, approve the pull request and enable auto-merge.
107115
# Our CI tests coupled with required statuses should ensure that the changes compile
108116
# and that the application is still functional after the update; any bug that might be
109117
# introduced by the update should be caught by the tests. If that happens, the build
110118
# workflow will fail and the preconditions for the auto-merge to happen won't be met.
111119
- name: Approve pull request and enable auto-merge
112-
if: ${{ steps.check-dependencies.outputs.is-trusted-update == 'true' }}
120+
if: steps.check-dependencies.outputs.is-trusted-update == 'true'
113121
env:
114122
GH_TOKEN: ${{ steps.generate-application-token.outputs.token }}
123+
PR_NUMBER: ${{ github.event.pull_request.number }}
115124
PR_URL: ${{ github.event.pull_request.html_url }}
116125
shell: pwsh
117126
run: |
118-
$approvals = gh api /repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/reviews | ConvertFrom-Json
119-
$approvals = $approvals | Where-Object { $_.user.login -eq $env:REVIEWER_LOGIN }
127+
$approvals = gh api "/repos/${env:GITHUB_REPOSITORY}/pulls/${env:PR_NUMBER}/reviews" | ConvertFrom-Json
128+
$approvals = $approvals | Where-Object { $_.user.login -eq ${env:REVIEWER_LOGIN} }
120129
$approvals = $approvals | Where-Object { $_.state -eq "APPROVED" }
121130
122131
if ($approvals.Length -eq 0) {
123-
gh pr checkout "$env:PR_URL"
124-
gh pr review --approve "$env:PR_URL"
125-
gh pr merge --auto --squash "$env:PR_URL"
132+
gh pr checkout ${env:PR_URL}
133+
gh pr review --approve ${env:PR_URL}
134+
gh pr merge --auto --squash ${env:PR_URL}
126135
}
127136
else {
128137
Write-Output "PR already approved.";
@@ -134,23 +143,24 @@ jobs:
134143
# automatically if there's an unexpected change introduced. Any existing review
135144
# approvals that were made by the bot are also dismissed so human approval is required.
136145
- name: Disable auto-merge and dismiss approvals
137-
if: ${{ steps.check-dependencies.outputs.is-trusted-update != 'true' }}
146+
if: steps.check-dependencies.outputs.is-trusted-update != 'true'
138147
env:
139148
GH_TOKEN: ${{ steps.generate-application-token.outputs.token }}
149+
PR_NUMBER: ${{ github.event.pull_request.number }}
140150
PR_URL: ${{ github.event.pull_request.html_url }}
141151
shell: pwsh
142152
run: |
143-
$approvals = gh api /repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/reviews | ConvertFrom-Json
144-
$approvals = $approvals | Where-Object { $_.user.login -eq $env:REVIEWER_LOGIN }
153+
$approvals = gh api "/repos/${env:GITHUB_REPOSITORY}/pulls/${env:PR_NUMBER}/reviews" | ConvertFrom-Json
154+
$approvals = $approvals | Where-Object { $_.user.login -eq ${env:REVIEWER_LOGIN} }
145155
$approvals = $approvals | Where-Object { $_.state -eq "APPROVED" }
146156
147157
if ($approvals.Length -gt 0) {
148-
gh pr checkout "$env:PR_URL"
149-
gh pr merge --disable-auto "$env:PR_URL"
158+
gh pr checkout ${env:PR_URL}
159+
gh pr merge --disable-auto ${env:PR_URL}
150160
foreach ($approval in $approvals) {
151161
gh api `
152162
--method PUT `
153-
/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/reviews/$($approval.id)/dismissals `
163+
"/repos/${env:GITHUB_REPOSITORY}/pulls/${env:PR_NUMBER}/reviews/$($approval.id)/dismissals" `
154164
-f message='Cannot approve as other changes have been introduced.' `
155165
-f event='DISMISS'
156166
}

.github/workflows/build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,6 @@ jobs:
155155
dotnet-version: ${{ needs.build.outputs.dotnet-sdk-version }}
156156

157157
- name: Push NuGet packages to NuGet.org
158-
run: dotnet nuget push "*.nupkg" --api-key ${{ secrets.NUGET_TOKEN }} --skip-duplicate --source https://api.nuget.org/v3/index.json
158+
run: dotnet nuget push "*.nupkg" --api-key ${env:NUGET_API_KEY} --skip-duplicate --source https://api.nuget.org/v3/index.json
159+
env:
160+
NUGET_API_KEY: ${{ secrets.NUGET_TOKEN }}

.github/workflows/bump-version.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ jobs:
7777
7878
"" >> $properties
7979
80-
"version=${updatedVersion}" >> $env:GITHUB_OUTPUT
80+
"version=${updatedVersion}" >> ${env:GITHUB_OUTPUT}
8181
8282
- name: Push changes to GitHub
8383
id: push-changes
@@ -113,9 +113,9 @@ jobs:
113113
git commit -m "Bump version`n`nBump version to ${env:NEXT_VERSION} for the next release."
114114
git push -u origin $branchName
115115
116-
"branch-name=${branchName}" >> $env:GITHUB_OUTPUT
117-
"updated-version=true" >> $env:GITHUB_OUTPUT
118-
"version=${env:NEXT_VERSION}" >> $env:GITHUB_OUTPUT
116+
"branch-name=${branchName}" >> ${env:GITHUB_OUTPUT}
117+
"updated-version=true" >> ${env:GITHUB_OUTPUT}
118+
"version=${env:NEXT_VERSION}" >> ${env:GITHUB_OUTPUT}
119119
120120
- name: Create pull request
121121
if: steps.push-changes.outputs.updated-version == 'true'

.github/workflows/dependabot-approve.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ name: dependabot-approve
22

33
on: pull_request_target
44

5-
permissions:
6-
contents: read
5+
permissions: {}
76

87
jobs:
98
dependabot:
109
runs-on: ubuntu-latest
11-
if: ${{ github.event.pull_request.user.login == 'dependabot[bot]' }}
10+
if: github.event.pull_request.user.login == 'dependabot[bot]'
11+
12+
permissions:
13+
contents: read
1214

1315
steps:
1416

@@ -22,7 +24,7 @@ jobs:
2224
with:
2325
application_id: ${{ secrets.REVIEWER_APPLICATION_ID }}
2426
application_private_key: ${{ secrets.REVIEWER_APPLICATION_PRIVATE_KEY }}
25-
permissions: "contents:write, pull_requests:write"
27+
permissions: 'contents:write, pull_requests:write'
2628

2729
- name: Checkout code
2830
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

.github/workflows/dependency-review.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ on:
44
pull_request:
55
branches: [ main, dotnet-vnext ]
66

7-
permissions:
8-
contents: read
7+
permissions: {}
98

109
jobs:
1110
dependency-review:
1211
runs-on: ubuntu-latest
1312

13+
permissions:
14+
contents: read
15+
1416
steps:
1517

1618
- name: Checkout code

.github/workflows/lint.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ on:
1414
- dotnet-nightly
1515
workflow_dispatch:
1616

17-
permissions:
18-
contents: read
17+
permissions: {}
1918

2019
env:
2120
FORCE_COLOR: 3
@@ -27,6 +26,9 @@ jobs:
2726
lint:
2827
runs-on: ubuntu-latest
2928

29+
permissions:
30+
contents: read
31+
3032
steps:
3133

3234
- name: Checkout code

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
$properties = Join-Path "." "Directory.Build.props"
3939
$xml = [xml](Get-Content $properties)
4040
$version = $xml.SelectSingleNode('Project/PropertyGroup/VersionPrefix').InnerText
41-
"version=${version}" >> $env:GITHUB_OUTPUT
41+
"version=${version}" >> ${env:GITHUB_OUTPUT}
4242
4343
- name: Create release
4444
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1

.github/workflows/update-docs.yml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
with:
2525
application_id: ${{ secrets.UPDATER_APPLICATION_ID }}
2626
application_private_key: ${{ secrets.UPDATER_APPLICATION_PRIVATE_KEY }}
27-
permissions: "contents:write, pull_requests:write"
27+
permissions: 'contents:write, pull_requests:write'
2828

2929
- name: Checkout code
3030
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
@@ -42,12 +42,14 @@ jobs:
4242
env:
4343
DOTNET_CLI_TELEMETRY_OPTOUT: true
4444
DOTNET_NOLOGO: true
45+
UPDATER_COMMIT_USER_EMAIL: ${{ vars.UPDATER_COMMIT_USER_EMAIL }}
46+
UPDATER_COMMIT_USER_NAME: ${{ vars.UPDATER_COMMIT_USER_NAME }}
4547
run: |
4648
$ErrorActionPreference = "Stop"
4749
$ProgressPreference = "SilentlyContinue"
4850
4951
dotnet tool restore
50-
dotnet mdsnippets "$env:GITHUB_WORKSPACE" --exclude-directories ./artifacts
52+
dotnet mdsnippets ${env:GITHUB_WORKSPACE} --exclude-directories ./artifacts
5153
5254
if ($LASTEXITCODE -ne 0) {
5355
Write-Output "Failed to update documentation."
@@ -60,11 +62,11 @@ jobs:
6062
exit 0
6163
}
6264
63-
$branchName = "update-docs/$($env:GITHUB_SHA)"
65+
$branchName = "update-docs/${env:GITHUB_SHA}"
6466
65-
git config user.email "${{ vars.UPDATER_COMMIT_USER_EMAIL }}" | Out-Null
66-
git config user.name "${{ vars.UPDATER_COMMIT_USER_NAME }}" | Out-Null
67-
git remote set-url "${{ github.server_url }}/${{ github.repository }}.git" | Out-Null
67+
git config user.email "${env:UPDATER_COMMIT_USER_EMAIL}" | Out-Null
68+
git config user.name "${env:UPDATER_COMMIT_USER_NAME}" | Out-Null
69+
git remote set-url "${env:GITHUB_SERVER_URL}/${env:GITHUB_REPOSITORY}.git" | Out-Null
6870
git fetch origin | Out-Null
6971
git rev-parse --verify --quiet "remotes/origin/$branchName" | Out-Null
7072
@@ -78,8 +80,8 @@ jobs:
7880
git commit -m "Update documentation`n`nUpdate examples in documentation." -s
7981
git push -u origin $branchName
8082
81-
"branchName=$branchName" >> $env:GITHUB_OUTPUT
82-
"updated-docs=true" >> $env:GITHUB_OUTPUT
83+
"branchName=$branchName" >> ${env:GITHUB_OUTPUT}
84+
"updated-docs=true" >> ${env:GITHUB_OUTPUT}
8385
8486
- name: Create pull request
8587
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1

.github/workflows/update-dotnet-sdk.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ on:
55
- cron: '00 19 * * TUE'
66
workflow_dispatch:
77

8-
permissions:
9-
contents: read
8+
permissions: {}
109

1110
jobs:
1211
update-sdk:
1312
uses: martincostello/update-dotnet-sdk/.github/workflows/update-dotnet-sdk.yml@758e92b362c4164925583874878423a794cce239 # v3.4.1
13+
permissions:
14+
contents: read
1415
with:
15-
include-nuget-packages: "Microsoft.AspNetCore.,Microsoft.EntityFrameworkCore.,Microsoft.Extensions.,Microsoft.NET.Test.Sdk"
16-
labels: "dependencies,.NET"
16+
include-nuget-packages: 'Microsoft.AspNetCore.,Microsoft.EntityFrameworkCore.,Microsoft.Extensions.,Microsoft.NET.Test.Sdk'
17+
labels: 'dependencies,.NET'
1718
user-email: ${{ vars.UPDATER_COMMIT_USER_EMAIL }}
1819
user-name: ${{ vars.UPDATER_COMMIT_USER_NAME }}
1920
secrets:

build.ps1

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,36 @@ else {
4444

4545
if ($installDotNetSdk -eq $true) {
4646

47-
$env:DOTNET_INSTALL_DIR = Join-Path "$(Convert-Path "$PSScriptRoot")" ".dotnetcli"
48-
$sdkPath = Join-Path $env:DOTNET_INSTALL_DIR "sdk" $dotnetVersion
47+
${env:DOTNET_INSTALL_DIR} = Join-Path $PSScriptRoot ".dotnetcli"
48+
$sdkPath = Join-Path ${env:DOTNET_INSTALL_DIR} "sdk" $dotnetVersion
4949

5050
if (!(Test-Path $sdkPath)) {
51-
if (!(Test-Path $env:DOTNET_INSTALL_DIR)) {
52-
mkdir $env:DOTNET_INSTALL_DIR | Out-Null
51+
if (!(Test-Path ${env:DOTNET_INSTALL_DIR})) {
52+
mkdir ${env:DOTNET_INSTALL_DIR} | Out-Null
5353
}
5454
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor "Tls12"
5555

5656
if (($PSVersionTable.PSVersion.Major -ge 6) -And !$IsWindows) {
57-
$installScript = Join-Path $env:DOTNET_INSTALL_DIR "install.sh"
57+
$installScript = Join-Path ${env:DOTNET_INSTALL_DIR} "install.sh"
5858
Invoke-WebRequest "https://dot.net/v1/dotnet-install.sh" -OutFile $installScript -UseBasicParsing
5959
chmod +x $installScript
60-
& $installScript --version "$dotnetVersion" --install-dir "$env:DOTNET_INSTALL_DIR" --no-path
60+
& $installScript --version "$dotnetVersion" --install-dir ${env:DOTNET_INSTALL_DIR} --no-path
6161
}
6262
else {
63-
$installScript = Join-Path $env:DOTNET_INSTALL_DIR "install.ps1"
63+
$installScript = Join-Path ${env:DOTNET_INSTALL_DIR} "install.ps1"
6464
Invoke-WebRequest "https://dot.net/v1/dotnet-install.ps1" -OutFile $installScript -UseBasicParsing
65-
& $installScript -Version "$dotnetVersion" -InstallDir "$env:DOTNET_INSTALL_DIR" -NoPath
65+
& $installScript -Version "$dotnetVersion" -InstallDir ${env:DOTNET_INSTALL_DIR} -NoPath
6666
}
6767
}
6868
}
6969
else {
70-
$env:DOTNET_INSTALL_DIR = Split-Path -Path (Get-Command dotnet).Path
70+
${env:DOTNET_INSTALL_DIR} = Split-Path -Path (Get-Command dotnet).Path
7171
}
7272

73-
$dotnet = Join-Path "$env:DOTNET_INSTALL_DIR" "dotnet"
73+
$dotnet = Join-Path ${env:DOTNET_INSTALL_DIR} "dotnet"
7474

7575
if ($installDotNetSdk -eq $true) {
76-
$env:PATH = "$env:DOTNET_INSTALL_DIR;$env:PATH"
76+
${env:PATH} = "${env:DOTNET_INSTALL_DIR};${env:PATH}"
7777
}
7878

7979
function DotNetPack {
@@ -98,7 +98,7 @@ function DotNetTest {
9898

9999
$additionalArgs = @()
100100

101-
if (![string]::IsNullOrEmpty($env:GITHUB_SHA)) {
101+
if (![string]::IsNullOrEmpty(${env:GITHUB_SHA})) {
102102
$additionalArgs += "--logger"
103103
$additionalArgs += "GitHubActions;report-warnings=false"
104104
}

0 commit comments

Comments
 (0)