Skip to content

Commit 4fe161c

Browse files
committed
First Try, I swear. No rebases here.
1 parent 4533c05 commit 4fe161c

19 files changed

+324
-33
lines changed

.devcontainer/devcontainer.json

+5-20
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
"hostRequirements": {
55
"cpus": 4
66
},
7-
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
7+
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
8+
9+
// NOTE: This is a workaround for the powershell devcontainer not being published yet. We use the dotnet image which
10+
// has the latest (as of the build) version of powershell already bundled
811
"image": "mcr.microsoft.com/devcontainers/dotnet",
9-
"features": {
10-
// Uncomment this if you need a newer version of PowerShell then what ships with the dotnet devcontainer
11-
"ghcr.io/devcontainers/features/powershell:1": {
12-
"version": "latest"
13-
}
14-
},
15-
// Allows our container to work in rootless mode which is more secure
16-
"containerUser": "vscode",
17-
"remoteUser": "vscode",
12+
1813
// VSCode specific configuration
1914
"customizations": {
2015
"vscode": {
@@ -33,16 +28,6 @@
3328
"editor.inlayHints.enabled": "offUnlessPressed",
3429
"extensions.ignoreRecommendations": true, // Suppresses a message about PowerShell stable because we use preview extension
3530
"git.autofetch": true,
36-
"powershell.codeFormatting.alignPropertyValuePairs": true,
37-
"powershell.codeFormatting.autoCorrectAliases": true,
38-
"powershell.codeFormatting.newLineAfterOpenBrace": true,
39-
"powershell.codeFormatting.openBraceOnSameLine": true,
40-
"powershell.codeFormatting.pipelineIndentationStyle": "IncreaseIndentationForFirstPipeline",
41-
"powershell.codeFormatting.preset": "OTBS",
42-
"powershell.codeFormatting.trimWhitespaceAroundPipe": true,
43-
"powershell.codeFormatting.useConstantStrings": true,
44-
"powershell.codeFormatting.useCorrectCasing": true,
45-
"powershell.codeFormatting.whitespaceBetweenParameters": true,
4631
"powershell.powerShellDefaultVersion": "PowerShell",
4732
"terminal.integrated.defaultProfile.linux": "pwsh",
4833
"workbench.iconTheme": "vscode-icons"

.github/workflows/ex01_theBasics.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#This is the bare minimum a GitHub Actions workflow can be. It runs a script that outputs a message to the console.
2+
on: workflow_dispatch
3+
4+
#This isn't technically required, and can also be set at the job level, but it defaults to bash so why not PowerShell?
5+
defaults:
6+
run:
7+
shell: pwsh
8+
9+
jobs:
10+
theBasics:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- run: | #In YAML this lets you write the script across multiple lines, perfect for PowerShell
14+
$date = Get-Date
15+
Write-Output "Congrats it worked at ${date}! 🎉🎉🎉"

.github/workflows/ex02_moreMeta.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Example 02 - More Meta #Display name that will show in UI
2+
on: workflow_dispatch
3+
run-name: Run ${{ github.run_id }} by @${{ github.actor }}
4+
5+
defaults:
6+
run:
7+
shell: pwsh
8+
9+
jobs:
10+
theBasics:
11+
#A display name other than the job
12+
name: Demonstrate GHA Basics
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: 🕰️ Get the Date
16+
id: getDate
17+
run: |
18+
$date = Get-Date
19+
Write-Output "Congrats it worked at ${date}! 🎉🎉🎉"

.github/workflows/ex03_scriptFile.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Example 03 - Script File
2+
on: workflow_dispatch
3+
4+
defaults:
5+
run:
6+
shell: pwsh
7+
8+
jobs:
9+
getExternalScript:
10+
runs-on: ubuntu-latest
11+
steps:
12+
#Checkout is needed to get the script from the repo
13+
- name: 📤 Checkout
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
18+
- name: 📝 Run Script
19+
run: . ./scripts/01_basic.ps1

.github/workflows/ex04_runnerInfo.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Example 04 - Runner Info
2+
on: workflow_dispatch
3+
4+
defaults:
5+
run:
6+
shell: pwsh
7+
8+
jobs:
9+
getRunnerInfo:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- run: |
13+
"=== RUNNER INFO ==="
14+
$PSVersionTable
15+
""
16+
17+
"=== ENV VARS ==="
18+
dir env:
19+
""
20+
21+
"=== CURRENT DIRECTORY ==="
22+
$PWD
23+
""
24+
25+
"=== VARIABLES ==="
26+
Get-Variable | ft -auto name,value
27+
""
28+
29+
"=== LOADED MODULES ==="
30+
get-module | ft -auto name,version
31+
""
32+
33+
"=== AVAILABLE MODULES ==="
34+
get-module -listavailable | ft -auto name,version
35+
""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Example 05 - Multi-Step Inputs and Outputs
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
color:
6+
description: Color to set
7+
required: false
8+
default: green
9+
10+
defaults:
11+
run:
12+
shell: pwsh
13+
14+
jobs:
15+
multiStepInputOutput:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Set color to ${{ github.event.inputs.color }}
19+
id: color-selector
20+
run: |
21+
"SELECTED_COLOR=${{ github.event.inputs.color }}" | Out-File -FilePath $env:GITHUB_OUTPUT -Append
22+
- name: Get color
23+
env:
24+
SELECTED_COLOR: ${{ steps.color-selector.outputs.SELECTED_COLOR }}
25+
run: Write-Output "The selected color is $env:SELECTED_COLOR"
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Example 06 - Inputs as Script Parameters
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
color:
6+
description: Color to set
7+
required: false
8+
default: green
9+
10+
defaults:
11+
run:
12+
shell: pwsh
13+
14+
jobs:
15+
inputOutputScript:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: 📤 Checkout
19+
uses: actions/checkout@v4
20+
with:
21+
fetch-depth: 0
22+
23+
- name: Set color to ${{ inputs.color }}
24+
id: color-selector
25+
run: . scripts/06_color.ps1 -Color '${{ inputs.color }}'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#Docs: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions
2+
name: Example 07 - Workflow Commands
3+
on: workflow_dispatch
4+
5+
defaults:
6+
run:
7+
shell: pwsh
8+
9+
jobs:
10+
workflowCommandsDemo:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: 📤 Checkout
14+
uses: actions/checkout@v4
15+
with:
16+
fetch-depth: 0
17+
18+
- name: Workflow Commands Demo
19+
id: color-selector
20+
run: . scripts/07_workflowCommands.ps1

.github/workflows/ex08_modules.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Example 08 - Modules
2+
on: workflow_dispatch
3+
4+
defaults:
5+
run:
6+
shell: pwsh
7+
8+
jobs:
9+
modules:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: 🐢 Install Pester
13+
run: Install-PSResource Pester -Version 5.6.0 -Verbose -TrustRepository
14+
15+
- name: 📥 Checkout
16+
uses: actions/checkout@v4
17+
18+
- name: 📦 Install from Required Resources
19+
run: Install-PSResource -RequiredResourceFile scripts/08_requiredresources.psd1 -Verbose -TrustRepository
20+
21+
- name: 📄 Report Installed Modules
22+
run: |
23+
Get-Module -ListAvailable
24+
| Where-Object path -Like "$HOME/.local/share/powershell/Modules*"

.github/workflows/ex09_GitHubApi.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Example 09 - GithubApi
2+
on: workflow_dispatch
3+
4+
#The token is read only by default, we need to give it rights for this workflow
5+
permissions:
6+
issues: write
7+
8+
defaults:
9+
run:
10+
shell: pwsh
11+
12+
jobs:
13+
modules:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: 📥 Checkout
17+
uses: actions/checkout@v4
18+
19+
#The token is a secret and must be passed in, cannot be accessed as env variable
20+
- name: 🐙 Use GitHub Rest Api to Create Issue
21+
run: . scripts/09_createissue.ps1 -Token ${{secrets.GITHUB_TOKEN}}

LICENSE

+26
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,29 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4545
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
4646
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
4747
SOFTWARE.
48+
49+
---
50+
51+
## [Adam Driscoll: Github Actions Examples](https://github.com/adamdriscoll/pwsh-github-actions)
52+
53+
MIT License
54+
55+
Copyright (c) 2024 Adam Driscoll
56+
57+
Permission is hereby granted, free of charge, to any person obtaining a copy
58+
of this software and associated documentation files (the "Software"), to deal
59+
in the Software without restriction, including without limitation the rights
60+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
61+
copies of the Software, and to permit persons to whom the Software is
62+
furnished to do so, subject to the following conditions:
63+
64+
The above copyright notice and this permission notice shall be included in all
65+
copies or substantial portions of the Software.
66+
67+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
68+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
69+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
70+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
71+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
72+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
73+
SOFTWARE.

README.MD

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Creating GitHub Actions with PowerShell
22

3-
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/JustinGrote/PowerShellGithubActions?quickstart=1)
4-
53
This is a demo of how to develop custom GitHub Actinos in PowerShell. This repository is the companion to the PSConfEU 2024 Presentation **Building Custom GitHub Actions in PowerShell**
64

75
## Follow Along Setup
@@ -10,12 +8,17 @@ If you run into any problems with this setup feel free to reach out to @JustinWG
108

119
[GitHub Codespaces](https://github.com/features/codespaces) provides a complete preconfigured development environment that runs in GitHub. GitHub provides all users 60 hours a month of free usage. You do not even need Visual Studio Code **installed** locally, it can run within your browser.
1210

13-
### Steps
11+
### Common Setup
1412

1513
1. [Sign up for a GitHub account](https://github.com/join) if you do not already have one. It is free to join.
16-
1. Open our codespaces quickstart link in the browser of your choice (Edge/Chrome recommended)
17-
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/JustinGrote/PowerShellGithubActions?quickstart=1)
18-
1. Click `Create new codespace` when prompted. You can optionally click `Change Options` and customize some aspects of the codespace such as the number of CPUs. This repo is preconfigured with 4 cores. While 2 core is totally sufficient for Github Action editing, we are using 4 for this demo to reduce startup and build time and generally avoid lag due to time constraints.
14+
1. [Fork the Repository](https://github.com/JustinGrote/PowerShellGithubActions/fork), this will create a copy of the repository in your account, and give you a way to pull future changes.
15+
1. Enable Github Actions in your new repository by going to the `Actions` tab and accept to enable them. You don't have to trust me, feel free to review the GitHub Actions before you do, but the initial examples are all manually triggered.
16+
![alt text](images/README/image-1.png)
17+
18+
### Option 1: Online/Codespaces (recommended)
19+
20+
1. The first part of the process will just use the embedded Actions editor, which is pretty good. The second part we will go into some vscode tooling.
21+
1. Go to your forked repo and click `Create Codespace on Main` ![alt text](images/README/image.png)
1922
1. If you have Visual Studio Code installed, it will prompt you to open a link in Visual Studio code to connect to your codespace, otherwise the web version of Visual Studio Code will open in your browser and connect to the codespace.
2023
1. The codespace may take several minutes to load.
2124
1. **Congratulations!** You are now ready to start your GitHub Action development journey.
@@ -25,14 +28,10 @@ If you run into any problems with this setup feel free to reach out to @JustinWG
2528
This is an option if you wish to work "offline". It does not require a GitHub account, though we still recommend a GitHub Account as you will not be able to make pull requests to test the lab exercises or run GitHub Actions without one.
2629

2730
1. Follow the [Dev Containers Tutorial](https://code.visualstudio.com/docs/devcontainers/tutorial) to setup devcontainers on your local computer.
28-
1. Clone the Github Repository (https://codespaces.new/JustinGrote/PowerShellGithubActions) to a local folder and then open it in Visual Studio Code
31+
1. Clone your fork to a local folder and then open it in Visual Studio Code.
2932
1. You should be prompted to reopen the project in a devcontainer.
30-
1. Once the codespace loads, you will likely see a warning about the C# prerelease being required. Go ahead and click Upgrade to reload the codespace. This is due to the new C# Dev Kit extension and this message will stop occuring once it becomes generally available.
31-
![Alt text](images/README/image-2.png)
32-
1. **Congratulations!** You are now ready to start your Github Action- development journey.
33+
1. **Congratulations!** You are now ready to start your Github Action development journey.
3334

3435
### Option 3: Local Development
3536

36-
If you prefer local development, or are in a controlled environment without access to Docker or Codespaces, you can simply clone this GitHub repository and work locally. You will need to manually install PowerShell, .NET Core, and all the relevant extensions. **This is not recommended**
37-
38-
## Customizati
37+
If you prefer local development, you can simply clone this GitHub repository and work locally. It is recommended you install the [GitHub Actions VSCode Extension](https://marketplace.visualstudio.com/items?itemName=GitHub.vscode-github-actions).

images/README/image-1.png

133 KB
Loading

images/README/image.png

227 KB
Loading

scripts/01_basic.ps1

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$date = Get-Date
2+
Write-Output "Congrats it worked at ${date}! 🎉🎉🎉"

scripts/06_color.ps1

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[CmdletBinding()]
2+
param(
3+
[Parameter(Mandatory)][string]$Color
4+
)
5+
6+
$PSStyle.Foreground.$Color +
7+
"The Color is $Color" +
8+
$PSStyle.Reset
9+
10+
Write-Host -ForegroundColor $Color 'Write-Host gets colors stripped for some reason, possible bug https://github.com/orgs/community/discussions/40864'
11+
12+
'But object headers colorize just fine'
13+
14+
Get-ChildItem $PSScriptRoot

scripts/07_workflowCommands.ps1

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'::debug::Wheres Waldo? Find me in the debug logs!'
2+
3+
'::notice file=01_basic.ps1,line=1,col=5,endColumn=7::This script is crap'
4+
5+
'::warning file=06_color.ps1,line=1,col=1,endColumn=2::This might be dangerous!'
6+
7+
'::group::Environment Variables'
8+
Get-ChildItem env:
9+
'::endgroup::'
10+
11+
'::add-mask::SuperSecretValue'
12+
'The Password is SuperSecretValue!'
13+
14+
#Job Summaries. More info: https://github.blog/2022-05-09-supercharging-github-actions-with-job-summaries/
15+
@'
16+
# Facts
17+
1. Github Actions are *awesome*!
18+
1. PowerShell is *awesome*!
19+
1. You are *awesome*!
20+
21+
```mermaid
22+
graph LR
23+
A[You] -- are --> B((AWESOME))
24+
```
25+
26+
```powershell
27+
Get-AwesomePerson -Name You
28+
```
29+
30+
![image](https://github.com/JustinGrote/PowerShellGithubActions/assets/15258962/c3744b21-0eaa-4a76-a0e0-71ae9c0a0516)
31+
32+
'@ >> $env:GITHUB_STEP_SUMMARY

0 commit comments

Comments
 (0)