Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
14e9343
Create building-and-testing-powershell.md
potatoqualitee Oct 8, 2020
db0cdbd
add versions
potatoqualitee Oct 8, 2020
d7900f3
remove . github example, add 'bin'
potatoqualitee Oct 8, 2020
712bc2e
remove errant brace
potatoqualitee Oct 8, 2020
4b381ec
no multi-line, pipe not needed
potatoqualitee Oct 8, 2020
8a661b5
add consistency and shells
potatoqualitee Oct 8, 2020
1038367
fix formatting and code
potatoqualitee Oct 8, 2020
92012f1
added recommendation
potatoqualitee Oct 8, 2020
6caa306
add to index
potatoqualitee Oct 8, 2020
60d38a3
Update content/actions/guides/building-and-testing-powershell.md
potatoqualitee Oct 10, 2020
b33dfa6
Update content/actions/guides/building-and-testing-powershell.md
potatoqualitee Oct 10, 2020
ca5e7c2
add sample builder
potatoqualitee Oct 10, 2020
8215b0c
Update building-and-testing-powershell.md
potatoqualitee Oct 16, 2020
e872b14
Added requested changes
martin389 Oct 19, 2020
edc0bc7
Converted "dependencies" example into a job, added small edits
martin389 Oct 21, 2020
630360c
Added edits (described below)
martin389 Oct 22, 2020
e454742
Added small fixes
martin389 Oct 22, 2020
e2996bc
Changed PSScriptAnalyzer example into a standalone job
martin389 Oct 23, 2020
2f06b5f
throw exceptions for errors
potatoqualitee Oct 23, 2020
63b317d
Added note about package management
martin389 Oct 29, 2020
10ced0f
Added update from tech review
martin389 Oct 29, 2020
7ba29c4
Merge branch 'main' into powershell
martin389 Oct 29, 2020
6a92143
Small edit
martin389 Oct 29, 2020
ed396dd
Merge branch 'main' into powershell
martin389 Nov 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 213 additions & 0 deletions content/actions/guides/building-and-testing-powershell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
---
title: Building and testing PowerShell
intro: You can create a continuous integration (CI) workflow to build and test your PowerShell project.
versions:
free-pro-team: '*'
enterprise-server: '>=2.22'
---

{% data reusables.actions.enterprise-beta %}
{% data reusables.actions.enterprise-github-hosted-runners %}

### Introduction

This guide shows you how to install, test and publish a PowerShell module.

{% data variables.product.prodname_dotcom %}-hosted runners have a tools cache with pre-installed software, which includes PowerShell and Pester. You don't have to install anything! For a full list of up-to-date software and the pre-installed versions of PowerShell and Pester, see "[Specifications for {% data variables.product.prodname_dotcom %}-hosted runners](/actions/reference/specifications-for-github-hosted-runners/#supported-software)".

### Prerequisites

You should be familiar with YAML and the syntax for {% data variables.product.prodname_actions %}. For more information, see "[Learn {% data variables.product.prodname_actions %}](/actions/learn-github-actions)."

We recommend that you have a basic understanding of PowerShell and Pester. For more information, see:
- [Getting started with PowerShell](https://docs.microsoft.com/en-us/powershell/scripting/learn/ps101/01-getting-started)
- [Pester](https://pester.dev)

{% data reusables.actions.enterprise-setup-prereq %}

### Starting with the PowerShell workflow template

{% data variables.product.prodname_dotcom %} provides a PowerShell workflow template that should work for most PowerShell projects. This guide includes examples that you can use to customize the template. For more information, see the [PowerShell workflow template](https://github.com/actions/starter-workflows/blob/main/ci/powershell.yml).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You noted that this guide is dependent on getting the new Powershell workflow template you created merged. I wonder if you could decouple that dependency a bit. For example, you could remove the references to the starter workflow and just use the workflow you provide as an example. Then when the starter workflow you created is merged, this guide could be updated to reference it!

Copy link
Contributor Author

@potatoqualitee potatoqualitee Oct 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it me that updates the guide? I have a bad memory and worry this 💎 will forgotten.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's visit this right before we get this guide merged. If the starter template you created is still waiting for a review, you or a docs team member can update the guide to remove the reference to the starter workflow as a last step.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it makes sense to decouple the dependency for now. @potatoqualitee - would you mind if I revised this example into a standalone workflow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martin389 please do 🙏

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a commit for this now 😄


To get started quickly, add the template to the `.github/workflows` directory of your repository.

{% raw %}
```yaml
name: Test PowerShell on Ubuntu, macOS and Windows

on:
push:
branches: [ $default-branch ]
pull_request:
branches: [ $default-branch ]

jobs:
build:
name: Pester tests work on all platforms
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
- uses: actions/checkout@v1
- name: Perform a Pester test from the command-line to ensure expected results
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you describe a bit more about what this step does? Is it part of a typical CI workflow?

Copy link
Contributor Author

@potatoqualitee potatoqualitee Oct 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pester testing is indeed part of a typical PowerShell CI workflow. The test is useless but provides a valid and simplified example that will work on any machine since Get-ChildItem is universally available.

Generally, we'll test from a file, and not right from the command line. I've done it at the command line before, then moved it to file.

What I like about this example is that it shows PowerShell being used at the command line w/o being executed from a file. I do execute a lot of PS right from run: - just not as much for Pester testing. I hope that's clear 🤞 I'm open to removing it. If you'd like to keep it, perhaps I can do some sort of ping test instead using Test-NetConnection.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all your great work here, @potatoqualitee ⚡ I also like having a command line example that demonstrates Pester. What do you think of an example that demonstrates how to use test-path from both the command line and the Unit.Tests.ps1 file? With that approach, the reader can understand that there are two different ways of doing the same thing, but will appreciate that the Tests file is more scalable. I could also add in these updates if you like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@martin389 sounds good. I had something similar in the original but the test was so silly, I ended up removing it. I'd love a better example. please add the updates 🚀

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, great 🎉 I've added a commit with the changes 👍

shell: pwsh
run: Get-ChildItem | Select-Object -ExpandProperty Name -First 1 | Should -Be 'bin'
- name: Perform advanced tests
shell: pwsh
run: |
Invoke-Pester Unit.Tests.ps1 -Passthru
Invoke-Pester Integration.Tests.ps1 -Passthru
```
{% endraw %}

### PowerShell module locations

The table below describes the locations for various PowerShell modules in each {% data variables.product.prodname_dotcom %}-hosted runner.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason to hardcode all theses locations in documentation?

  1. All these paths are listed in $env:PSModulePath in powershell. Probably, it is better to rely on this variable.
  2. I am not sure about cases where customers need these paths. The recommended use-case to install modules is Install-Module command that taking care about location by itself. I don't think that we should endorse any other ways to install modules

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added them because they were in the Python docs, but also, I needed the information when creating my PowerShell module cacher. As a PowerShell Dev, I'd like having this documentation, as opposed to having to figure out my paths manually.


|| Ubuntu | Mac | Windows |
|------|-------|------|----------|
|**PowerShell system modules** |`/opt/microsoft/powershell/7/Modules/*`|`/usr/local/microsoft/powershell/7/Modules/*`|`C:\program files\powershell\7\Modules\*`|
|**PowerShell add-on modules**|`/usr/local/share/powershell/Modules/*`|`/usr/local/share/powershell/Modules/*`|`C:\Modules\*`|
|**User-installed modules**|`/home/runner/.local/share/powershell/Modules/*`|`/Users/runner/.local/share/powershell/Modules/*`|`C:\Users\runneradmin\Documents\PowerShell\Modules\*`|

### Installing dependencies

{% data variables.product.prodname_dotcom %}-hosted runners have PowerShell 7 and Pester installed. You can use `Install-Module` to install additional dependencies from the PowerShell Gallery before building and testing your code. For example, the YAML below installs the `SqlServer` and `PSScriptAnalyzer` modules.

You can also cache dependencies to speed up your workflow. For more information, see "[Caching dependencies to speed up your workflow](/actions/automating-your-workflow-with-github-actions/caching-dependencies-to-speed-up-workflows)."

{% raw %}
```yaml
steps:
- uses: actions/checkout@v2
- name: Install dependencies
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module SqlServer, PSScriptAnalyzer
```
{% endraw %}

{% note %}

**Note:** By default, no repositories are trusted by PowerShell. When installing modules from the PowerShell Gallery, you must explicitly set the installation policy for `PSGallery` to `Trusted`.

{% endnote %}

#### Caching Dependencies

You can cache PowerShell module dependencies using a unique key, and restore the dependencies when you run future workflows using the [`cache`](https://github.com/marketplace/actions/cache) action. For more information, see "[Caching dependencies to speed up workflows](/actions/automating-your-workflow-with-github-actions/caching-dependencies-to-speed-up-workflows)."

PowerShell caches dependencies in different locations, depending on the operating system of the runner. The path you'll need to cache may differ from the Ubuntu example below depending on the operating system you use. For more information, see [PowerShell caching examples](https://github.com/actions/cache/blob/main/examples.md#PowerShell).

{% raw %}
```yaml
steps:
- uses: actions/checkout@v2
- name: Setup PowerShell module cache
id: cacher
uses: actions/cache@v2
with:
path: "~/.local/share/powershell/Modules"
key: ${{ runner.os }}-SqlServer-PSScriptAnalyzer
- name: Install required PowerShell modules
if: steps.cacher.outputs.cache-hit != 'true'
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module SqlServer, PSScriptAnalyzer -ErrorAction Stop
```
{% endraw %}

### Testing your code

You can use the same commands that you use locally to build and test your code.

#### Using PSScriptAnalyzer to lint code

The following example installs `PSScriptAnalyzer` and uses it to lint all ps1 files. For more information, see [PSScriptAnalyzer on GitHub](https://github.com/PowerShell/PSScriptAnalyzer).

{% raw %}
```yaml
steps:
- uses: actions/checkout@v2
- name: Install PSScriptAnalyzer
shell: pwsh
run: |
Set-PSRepository PSGallery -InstallationPolicy Trusted
Install-Module PSScriptAnalyzer -ErrorAction Stop
- name: Lint with PSScriptAnalyzer
shell: pwsh
run: |
# Consider using github-action-psscriptanalyzer in the Marketplace instead.
Invoke-ScriptAnalyzer -Path *.ps1 -Recurse -Outvariable issues
$errors = $issues.Where({$_.Severity -eq 'Error'})
$warnings = $issues.Where({$_.Severity -eq 'Warning'})
Write-Output "There were $($errors.Count) errors and $($warnings.Count) warnings total."
```
{% endraw %}

### Packaging workflow data as artifacts

You can upload artifacts to view after a workflow completes. For example, you may need to save log files, core dumps, test results, or screenshots. For more information, see "[Persisting workflow data using artifacts](/github/automating-your-workflow-with-github-actions/persisting-workflow-data-using-artifacts)."

The following example demonstrates how you can use the `upload-artifact` action to archive test results from running `Invoke-Pester`. For more information, see the [`upload-artifact` action](https://github.com/actions/upload-artifact).

{% raw %}
```yaml
name: Upload artifact from on Ubuntu, macOS and Windows

on: [push]

jobs:
build:
name: Upload Pester tests work from all platforms
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- uses: actions/checkout@v1
- name: Test with Pester
shell: pwsh
run: Invoke-Pester All.Tests.ps1 -Passthru | Export-CliXml -Path All.Tests.xml
- name: Upload test results
uses: actions/upload-artifact@v2
with:
# upload distinct zip files per OS
name: ${{ runner.os }}-All-Tests
path: All.Tests.xml
# Use always() to always run this step to publish test results when there are test failures
if: ${{ always() }}
```
{% endraw %}

### Publishing to package registries

You can configure your workflow to publish your PowerShell package to any package registry you'd like when your CI tests pass.

You can store any access tokens or credentials needed to publish your module using repository secrets. The following example creates and publishes a module to the PowerShell Gallery using `Publish-Module`. For more information, see "[Creating and using encrypted secrets](/github/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets)."

{% raw %}
```yaml
name: Publish PowerShell Module

on:
release:
types: [created]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build and publish
env:
NUGET_KEY: ${{ secrets.NUGET_KEY }}
shell: pwsh
run: Publish-Module -Path . -NuGetApiKey $env:NUGET_KEY -Verbose
```
{% endraw %}
1 change: 1 addition & 0 deletions content/actions/guides/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ You can use {% data variables.product.prodname_actions %} to create custom conti
{% link_in_list /about-continuous-integration %}
{% link_in_list /setting-up-continuous-integration-using-workflow-templates %}
{% link_in_list /building-and-testing-nodejs %}
{% link_in_list /building-and-testing-powershell %}
{% link_in_list /building-and-testing-python %}
{% link_in_list /building-and-testing-java-with-maven %}
{% link_in_list /building-and-testing-java-with-gradle %}
Expand Down