-
Notifications
You must be signed in to change notification settings - Fork 0
230 lines (215 loc) · 13.1 KB
/
PowershellModuleTest.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
name: PowershellModuleTest
run-name: "PowershellModuleTest on 3 platforms and create pull request to main"
# Triggers the workflow on push or pull request events but only for the given branches and ignore changes of some file types.
on:
push:
branches:
[ main, trunk ]
paths-ignore:
- '*.txt'
- '*.md'
- '.github/workflows/**'
pull_request:
branches:
[ main, trunk ]
paths-ignore:
- '*.txt'
- '*.md'
- '.github/workflows/**'
workflow_dispatch: # Allows you to run this workflow manually from the Actions tab
schedule:
# schedules are taking always the default branch of the repo (master/main).
# cron: minute(0..59) hour(0..23) dayOfMonth(1..31) month(1..12 or JAN..DEC) dayOfWeek(0..6 or SUN-SAT);
# '* * * * *' seams to be 15min; multivalues comma separated (2,4); ranges with a dash (2-5);
# increment with slash after min10 each 5 min (10/5); shortes are 5 minutes; in UTC;
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
# (00:01GMT+0 means at: 01:01GMT+1 wintertime, 02:01GMT+2 daylight-saving-time)
- cron: '1 0 * * 0' # 00:01UTC each sunday with main branch
- cron: '34 15 * * 4' # 00:11UTC each sunday with trunk branch
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
Test:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{matrix.os}} # run on multiple OS, if: ${{ runner.os == 'Linux' || runner.os == 'macOS' || runner.os == 'Windows' }}
name: "Test on ${{matrix.os}} ${{github.ref_type}}=${{github.ref_name}} ${{github.event.repository.updated_at}}"
# example: "Test: windows: branch=main 2021-12-31T23:59:59Z"
steps:
- name: Checkout repository current branch
if: github.event_name != 'schedule'
uses: actions/checkout@v3
- name: Checkout repository main branch when started by a specific schedule
if: github.event_name == 'schedule' && github.event.schedule == '1 0 * * 0'
uses: actions/checkout@v3
with: { ref: main }
- name: Checkout repository trunk branch when started by a specific schedule
if: github.event_name == 'schedule' && github.event.schedule != '1 0 * * 0'
uses: actions/checkout@v3
with: { ref: trunk }
- name: Install tools
shell: pwsh
run: |
# Assert installed tools: curl.
if( "${{runner.os}}" -eq "Linux" ){
which curl; # /usr/bin/curl
# alternative: sudo apt install -y curl; # curl is already the newest version (7.81.0-1ubuntu1.8).
}elseif( "${{runner.os}}" -eq "macOS" ){
which curl; # /usr/local/opt/curl/bin/curl
# alternative: brew install curl; # Warning: curl 7.88.1 is already installed and up-to-date. To reinstall 7.88.1, run: brew reinstall curl
}elseif( "${{runner.os}}" -eq "Windows" ){
Get-Command "curl" -ErrorAction SilentlyContinue; # 7.83.1.0 C:\Windows\system32\curl.exe
}
echo "github.event.schedule=${{github.event.schedule}} ";
- name: Test all
shell: pwsh
# Default runners have installed: PS7.2.5 and Pester.
# More about PS: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-powershell#powershell-module-locations
run: |
echo "Running on OS=${{runner.os}}; $(which pwsh); $(pwsh --version)";
# example: "OS=Linux; /opt/microsoft/powershell/7/pwsh; Powershell V7.2.9"
# example: "OS=macOS; /usr/local/microsoft/powershell/7/pwsh; PowerShell 7.2.9"
# example: "OS=Windows; /c/Program Files/PowerShell/7/pwsh; PowerShell 7.2.10"
echo "List all environment variables in pwsh"; Get-Variable;
echo "List all aliases in pwsh"; Get-Alias;
& "./TestAll.ps1";
echo "Test ok.";
Merge:
runs-on: ubuntu-latest # must run on linux because tool for creating pr
# Available 2022-08:
# - ubuntu-latest = Ubuntu-V20.04.3-LTS contains PowerShell-V7.2.9, already installed /usr/bin/pwsh
# - windows-latest = WinServer2022
# - macos-latest = macOS 11
# - self-hosted = own machine
# - More see: https://github.com/actions/virtual-environments/
name: "Merge to main if all unittests are ok"
needs: [Test]
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, see https://github.com/actions/checkout
- name: Checkout repository
uses: actions/checkout@v3
- name: Show environment variables of bash
shell: bash
run: |
echo "Running on OS=${{runner.os}}; $(which pwsh); $(pwsh --version)"; # example: "Linux; /usr/bin/pwsh; Powershell V7.2.9"
echo "RepoRef = ${{github.ref}};" # example: "refs/heads/trunk"
echo "Repo = ${{github.repository}};" # example: "mniederw/MnCommonPsToolLib"
echo "Actor = ${{github.actor}};" # example: "mniederw"
echo "EventName = ${{github.event_name}};" # example: "schedule", "push", "workflow_dispatch"
echo "ActionUrl = https://github.com/${{github.repository}}/actions/runs/${{github.run_id}};" # example: "https://github.com/mniederw/MnCommonPsToolLib/actions/runs/4272755554"
echo "Action = ${{github.workflow}} #${{github.run_number}};" # example: "PowershellModuleTest #570"
echo "WorkflowRef = ${{github.workflow_ref}};" # example: "mniederw/MnCommonPsToolLib/.github/workflows/PowershellModuleTest.yml@refs/heads/main"
echo "List all environment variables in bash"; printenv;
echo "List pnpm version: $((pnpm --version) || true)";
echo "List npm version: $((npm --version) || true)";
echo "List npm installed modules globally: $((npm list --global) || true)";
echo "List npm installed modules locally: $((npm list) || true)";
echo "ListDir /usr/lib/node_modules/: $((ls /usr/lib/node_modules/) || true)"; # ex: cordova corepack npm sfdx-cli
echo "ListDir /home/runner/.local/bin/: $((ls /home/runner/.local/bin/) || true)";
echo "ListDir ./node_modules/: $((ls ./node_modules/) || true)";
echo "ListFile ./package.json: $((cat ./package.json) || true)"; # ex: {"dependencies":{"fs":"0.0.1-security", "fs-extra":"^11.1.1", "xml2js":"^0.6.0", "jsonfile":"^6.1.0", "xmlmerge-js":"^0.2.5", "minimist":"^1.2.8", "path":"^0.12.7", "shelljs":"^0.8.5"}}
echo "List github context as json: ${{toJson(github)}}"; # 2023-07: fails after some json output with: Error: Process completed with exit code 127.
# More usuful variables:
# RUNNER_WORKSPACE = /home/runner/work/MnCommonPsToolLib
# GITHUB_WORKSPACE = /home/runner/work/MnCommonPsToolLib/MnCommonPsToolLib
# RUNNER_TEMP = /home/runner/work/_temp
# GITHUB_ENV = /home/runner/work/_temp/_runner_file_commands/set_env_dddaeb53-3cd8-4be8-b76c-5fa50a64656c
# GITHUB_OUTPUT = /home/runner/work/_temp/_runner_file_commands/set_output_dddaeb53-3cd8-4be8-b76c-5fa50a64656c
# GITHUB_STEP_SUMMARY = /home/runner/work/_temp/_runner_file_commands/step_summary_dddaeb53-3cd8-4be8-b76c-5fa50a64656c
# INVOCATION_ID = 765a5a77e7694d118c0feadb5fdc152d
# PATH = /opt/hostedtoolcache/node/18.16.1/x64/bin:/home/runner/setup-pnpm/node_modules/.bin:/home/runner/.local/bin:/opt/pipx_bin:
# /home/runner/.cargo/bin:/home/runner/.config/composer/vendor/bin:/usr/local/.ghcup/bin:/home/runner/.dotnet/tools:/snap/bin:
# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
# PNPM_HOME = /home/runner/setup-pnpm/node_modules/.bin
- name: If tests on trunk of a push or workflow_dispatch was successful then create pull request to main
if: ${{ success() && (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/trunk' && runner.os == 'Linux' }}
uses: repo-sync/[email protected]
# for parameters see: https://github.com/repo-sync/pull-request
# works only on linux, see releases for updating version from time to time
with:
pr_title: "Merge ${{github.ref_name}} to main - Unittests are successful - ${{ github.event.commits[0].message }}"
source_branch: "" # If blank, default: triggered branch
destination_branch: "main" #
github_token: ${{ secrets.GITHUB_TOKEN }} # If blank, default: secrets.GITHUB_TOKEN
pr_allow_empty: false # Creates pull request even if there are no changes
pr_label: "" # Comma-separated list (no spaces)
pr_milestone: "" # Milestone name
#pr_reviewer: "user1,user2" # Comma-separated list (no spaces)
#pr_assignee: "user1,user2" # Comma-separated list (no spaces)
- name: Output job state
if: ${{ always() }} # if alternatives: success(), failure(), cancelled(),
run: echo "Process ${{ job.status }}."; # job.status = [success,failure,cancelled].
# More docu: https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions
# For future:
# if: github.ref == 'refs/heads/mybranch'
# steps:
# - name: Install package manager pnpm
# uses: pnpm/action-setup@v2
# with: { version: 8.6.5 }
# - name: Install node.js (adds node and npm to path)
# if: steps.cache.outputs.cache-hit != 'true'
# uses: actions/setup-node@v3
# with: { node-version: 18.16.1 }
# - name: Install nodejs modules locally and not globally
# run: |
# pnpm install fs fs-extra xml2js jsonfile xmlmerge-js minimist path shelljs [email protected];
# alias sfdx="$PWD/node_modules/sfdx-cli/bin/run"; # required (only global install would put it to path)
# - shell: bash
# run: |
# echo "Inputtags: ${{ github.event.inputs.tag }}"
# echo "BranchRef: ${{github.ref}}"; # ex: "refs/heads/main"
# BranchName=$(echo "${{github.ref}}" | cut --delimiter=/ --fields=3- -); # ex: "main"
# BranchEnv="";
# if [[ "$BranchName" == "main" ]]; then BranchEnv="MAIN";
# elif [[ "$BranchName" == "trunk" ]]; then BranchEnv="ENV1";
# else BranchEnv="UNKN-$BranchName"; fi;
# echo "Run BranchEnv=$BranchEnv BranchName=$BranchName BranchRef=${{github.ref}}";
# #
# m="^main$" ; if [[ "$BranchName" =~ $m ]]; then BranchEnv="ENV2" ; fi;
# m="^main.*$" ;
# if [[ "$BranchName" =~ $m ]]; then BranchEnv="ENV2" ; fi;
# echo "Run SfEnv=$SfEnv BranchName=$BranchName BranchRef=${{github.ref}}";
# #
# if [[ $BranchEnv == "MAIN" ]]; then
# echo "MAIN";
# elif [[ $BranchEnv == "ENV1" ]]; then
# echo "ENV1";
# else echo "Expected BranchEnv=$BranchEnv is one of [MAIN,ENV1]"; exit 1; fi;
# For future: Run all *.Tests.ps1 recursively by using https://pester-docs.netlify.app/docs/commands/Invoke-Pester
# For future: Publish to Gallery: https://scriptingchris.tech/2021/05/16/how-to-setup-a-github-actions-pipeline-for-publishing-your-powershell-module/
# For future: pnpm install --global lodash;
# For future: autocommit
# uses: peter-evans/[email protected] # see: https://github.com/peter-evans/create-pull-request
# with:
# title: 'Merge trunk to main and commit it - ${{ github.event.commits[0].message }}'
# body: 'Autogenerated PR and committed from trunk to main'
# For future: alternative - same as repo-sync/[email protected] but with another tool:
# uses: vsoch/[email protected] # see https://github.com/vsoch/pull-request-action works only on linux
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# PULL_REQUEST_BRANCH: "main"
# PULL_REQUEST_TITLE: "Merge trunk to main - ${{ github.event.commits[0].message }}"
# PASS_IF_EXISTS:
# CI: true
# For future: cache: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-powershell
# For future: query gh https://docs.github.com/en/actions/using-workflows/using-github-cli-in-workflows
# For future: checking matrix context
# if: matrix.os == 'ubuntu-latest'
# if: matrix.os == 'windows-latest'
# if: matrix.os == 'macOS-latest'
# For future: checking runner context
# if: runner.os == 'Linux'
# if: runner.os == 'Windows'
# if: runner.os == 'macOS'
# For future: checking runner variable:
# if [ "$RUNNER_OS" == "Linux" ]; then
# For future:
# run: |
# git config --local user.name github-actions
# git config --local user.email "[email protected]"
# git add dir1/*
# git commit -am "Autocommit $(date)"
# git push origin main --force
# env:
# REPO_KEY: ${{secrets.GITHUB_TOKEN}}
# username: github-actions