Skip to content

PowershellModuleTest on 3 platforms and create pull request to main #800

PowershellModuleTest on 3 platforms and create pull request to main

PowershellModuleTest on 3 platforms and create pull request to main #800

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)"; # Example: 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)"; # Example: {"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"}}
# 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
# Note: we do not run: echo "List github context as json: ...toJson(github)...";
# because sometimes fails after some json output with:
# 2023-07: /home/runner/work/_temp/858ee69a-4952-4151-832b-4c9945ad8f90.sh: line 115: timestamp:: command not found
# Error: Process completed with exit code 127.
# Note: The error also occurrs in comment, if the three dots would have replaced by github dollar and braces!
- 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}}"; # Example: "refs/heads/main"
# BranchName=$(echo "${{github.ref}}" | cut --delimiter=/ --fields=3- -); # Example: "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