Skip to content

Commit

Permalink
First Try, I swear. No rebases here.
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinGrote committed Jun 26, 2024
1 parent 4533c05 commit 65abdd6
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 33 deletions.
25 changes: 5 additions & 20 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
"hostRequirements": {
"cpus": 4
},
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile

// NOTE: This is a workaround for the powershell devcontainer not being published yet. We use the dotnet image which
// has the latest (as of the build) version of powershell already bundled
"image": "mcr.microsoft.com/devcontainers/dotnet",
"features": {
// Uncomment this if you need a newer version of PowerShell then what ships with the dotnet devcontainer
"ghcr.io/devcontainers/features/powershell:1": {
"version": "latest"
}
},
// Allows our container to work in rootless mode which is more secure
"containerUser": "vscode",
"remoteUser": "vscode",

// VSCode specific configuration
"customizations": {
"vscode": {
Expand All @@ -33,16 +28,6 @@
"editor.inlayHints.enabled": "offUnlessPressed",
"extensions.ignoreRecommendations": true, // Suppresses a message about PowerShell stable because we use preview extension
"git.autofetch": true,
"powershell.codeFormatting.alignPropertyValuePairs": true,
"powershell.codeFormatting.autoCorrectAliases": true,
"powershell.codeFormatting.newLineAfterOpenBrace": true,
"powershell.codeFormatting.openBraceOnSameLine": true,
"powershell.codeFormatting.pipelineIndentationStyle": "IncreaseIndentationForFirstPipeline",
"powershell.codeFormatting.preset": "OTBS",
"powershell.codeFormatting.trimWhitespaceAroundPipe": true,
"powershell.codeFormatting.useConstantStrings": true,
"powershell.codeFormatting.useCorrectCasing": true,
"powershell.codeFormatting.whitespaceBetweenParameters": true,
"powershell.powerShellDefaultVersion": "PowerShell",
"terminal.integrated.defaultProfile.linux": "pwsh",
"workbench.iconTheme": "vscode-icons"
Expand Down
15 changes: 15 additions & 0 deletions .github/workflows/ex01_theBasics.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#This is the bare minimum a GitHub Actions workflow can be. It runs a script that outputs a message to the console.
on: workflow_dispatch

#This isn't technically required, and can also be set at the job level, but it defaults to bash so why not PowerShell?
defaults:
run:
shell: pwsh

jobs:
theBasics:
runs-on: ubuntu-latest
steps:
- run: | #In YAML this lets you write the script across multiple lines, perfect for PowerShell
$date = Get-Date
Write-Output "Congrats it worked at ${date}! 🎉🎉🎉"
19 changes: 19 additions & 0 deletions .github/workflows/ex02_moreMeta.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Example 02 - More Meta #Display name that will show in UI
on: workflow_dispatch
run-name: Run ${{ github.run_id }} by @${{ github.actor }}

defaults:
run:
shell: pwsh

jobs:
theBasics:
#A display name other than the job
name: Demonstrate GHA Basics
runs-on: ubuntu-latest
steps:
- name: 🕰️ Get the Date
id: getDate
run: |
$date = Get-Date
Write-Output "Congrats it worked at ${date}! 🎉🎉🎉"
19 changes: 19 additions & 0 deletions .github/workflows/ex03_scriptFile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Example 03 - Script File
on: workflow_dispatch

defaults:
run:
shell: pwsh

jobs:
getExternalScript:
runs-on: ubuntu-latest
steps:
#Checkout is needed to get the script from the repo
- name: 📤 Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: 📝 Run Script
run: . ./scripts/01_basic.ps1
35 changes: 35 additions & 0 deletions .github/workflows/ex04_runnerInfo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Example 04 - Runner Info
on: workflow_dispatch

defaults:
run:
shell: pwsh

jobs:
getRunnerInfo:
runs-on: ubuntu-latest
steps:
- run: |
"=== RUNNER INFO ==="
$PSVersionTable
""
"=== ENV VARS ==="
dir env:
""
"=== CURRENT DIRECTORY ==="
$PWD
""
"=== VARIABLES ==="
Get-Variable | ft -auto name,value
""
"=== LOADED MODULES ==="
get-module | ft -auto name,version
""
"=== AVAILABLE MODULES ==="
get-module -listavailable | ft -auto name,version
""
25 changes: 25 additions & 0 deletions .github/workflows/ex05_multiStepInputOutput.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Example 05 - Multi-Step Inputs and Outputs
on:
workflow_dispatch:
inputs:
color:
description: Color to set
required: false
default: green

defaults:
run:
shell: pwsh

jobs:
multiStepInputOutput:
runs-on: ubuntu-latest
steps:
- name: Set color to ${{ github.event.inputs.color }}
id: color-selector
run: |
"SELECTED_COLOR=${{ github.event.inputs.color }}" | Out-File -FilePath $env:GITHUB_OUTPUT -Append
- name: Get color
env:
SELECTED_COLOR: ${{ steps.color-selector.outputs.SELECTED_COLOR }}
run: Write-Output "The selected color is $env:SELECTED_COLOR"
25 changes: 25 additions & 0 deletions .github/workflows/ex06_inputScript.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Example 06 - Inputs as Script Parameters
on:
workflow_dispatch:
inputs:
color:
description: Color to set
required: false
default: green

defaults:
run:
shell: pwsh

jobs:
inputOutputScript:
runs-on: ubuntu-latest
steps:
- name: 📤 Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set color to ${{ inputs.color }}
id: color-selector
run: . scripts/06_color.ps1 -Color '${{ inputs.color }}'
26 changes: 26 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,29 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

---

## [Adam Driscoll: Github Actions Examples](https://github.com/adamdriscoll/pwsh-github-actions)

MIT License

Copyright (c) 2024 Adam Driscoll

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 12 additions & 13 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Creating GitHub Actions with PowerShell

[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/JustinGrote/PowerShellGithubActions?quickstart=1)

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**

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

[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.

### Steps
### Common Setup

1. [Sign up for a GitHub account](https://github.com/join) if you do not already have one. It is free to join.
1. Open our codespaces quickstart link in the browser of your choice (Edge/Chrome recommended)
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/JustinGrote/PowerShellGithubActions?quickstart=1)
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.
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.
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.
![alt text](images/README/image-1.png)

### Option 1: Online/Codespaces (recommended)

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.
1. Go to your forked repo and click `Create Codespace on Main` ![alt text](images/README/image.png)
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.
1. The codespace may take several minutes to load.
1. **Congratulations!** You are now ready to start your GitHub Action development journey.
Expand All @@ -25,14 +28,10 @@ If you run into any problems with this setup feel free to reach out to @JustinWG
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.

1. Follow the [Dev Containers Tutorial](https://code.visualstudio.com/docs/devcontainers/tutorial) to setup devcontainers on your local computer.
1. Clone the Github Repository (https://codespaces.new/JustinGrote/PowerShellGithubActions) to a local folder and then open it in Visual Studio Code
1. Clone your fork to a local folder and then open it in Visual Studio Code.
1. You should be prompted to reopen the project in a devcontainer.
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.
![Alt text](images/README/image-2.png)
1. **Congratulations!** You are now ready to start your Github Action- development journey.
1. **Congratulations!** You are now ready to start your Github Action development journey.

### Option 3: Local Development

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**

## Customizati
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).
Binary file added images/README/image-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/README/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions scripts/01_basic.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$date = Get-Date
Write-Output "Congrats it worked at ${date}! 🎉🎉🎉"
14 changes: 14 additions & 0 deletions scripts/06_color.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Color
)

$PSStyle.Foreground.$Color +
"The Color is $Color" +
$PSStyle.Reset

Write-Host -ForegroundColor $Color 'Write-Host gets colors stripped for some reason'

"But object headers don't"

Get-ChildItem $PSScriptRoot

0 comments on commit 65abdd6

Please sign in to comment.