PowerShell modules do not load load within GitHub Docker container Actions - powershell

Running PowerShell cmdlets from modules fails within a GitHub Action with the following error:
The term 'Get-AzResourceGroup' is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that
the path is correct and try again.
Dockerfile
FROM mcr.microsoft.com/powershell
RUN pwsh -c "Install-Module AZ";
GitHub Workflow
name: Run PowerShell in GH Action
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
container:
image: acrURL/imageName
credentials:
username: ${{secrets.acrUserName}}
password: ${{secrets.acrPassword}}
steps:
- uses: actions/checkout#v2
- name: Run
shell: pwsh
run: Get-AzResourceGroup -Name myRG

GitHub actions change the user path to /github/home/.local/share/powershell/Modules instead of /root/.
PowerShell is failing to load the modules because they are located under a different user account. Changing your Dockerfile to install the module to -Scope AllUsers will resolve the issue.
New Dockerfile
FROM mcr.microsoft.com/powershell
RUN pwsh -c "Install-Module AZ -Scope AllUsers";

Related

Create Zip file of Github Reporitory using a workflow which runs on [windows-latest]

I'm trying to create a zip file of my repository using git hub workflow. Below is the code:
name: .NET
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: [windows-latest]
steps:
- uses: actions/checkout#v3
- name: Set Up MS Build
uses: microsoft/setup-msbuild#v1
- name: Restore dependencies
run: nuget restore Solution.sln
- name: Build Solution
run: msbuild Solution.sln
- name: Creating Zip
run: zip -r release.zip . -x ".git/*" ".github/*"
This gives me an error :
> The term 'zip' is not recognized as a name of a cmdlet, function,
> script file, or executable program
.
Figured out that, zip command is not available on Windows.
Tried googling, but no luck. Any resources would be helpful.

How to execute a a remote script in a reusable github workflow

I have this workflow in a repo called terraform-do-database and I'm trying to use a reusable workflow coming from the public repo foo/git-workflows/.github/workflows/tag_validation.yaml#master
name: Tag Validation
on:
pull_request:
branches: [master]
push:
branches:
- '*' # matches every branch that doesn't contain a '/'
- '*/*' # matches every branch containing a single '/'
- '**' # matches every branch
- '!master' # excludes master
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
jobs:
tag_check:
uses: foo/git-workflows/.github/workflows/tag_validation.yaml#master
And this is the reusable workflow file from the public git-workflows repo that has the script that should run on it. What is happening is that the workflow is trying to use a script inside the repo terraform-do-database
name: Tag Validation
on:
pull_request:
branches: [master]
workflow_call:
jobs:
tag_check:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v3
# Runs a single command using the runners shell
- name: Verify the tag value
run: ./scripts/tag_verify.sh
So the question: How can I make the workflow use the script stored in the git-worflows repo instead of the terraform-do-database?
I want to have a single repo where I can call the workflow and the scripts, I don't want to have everything duplicated inside all my repos.
I have found that if I wrap the script into a composite action. I can use GitHub context github.action_path to locate the scripts.
Example:
run: ${{ github.action_path }}/scripts/foo.sh
One way to go about this is perform a checkout inside your reusable workflow that essentially clones the content of the repo where your scripts are and only then you can access it. It's not the cleanest solution but it works.
Perform a second checkout, to clone your repo that has the reusable workflow into a dir reusable-workflow-repo
- name: Checkout reusable workflow dir
uses: actions/checkout#v3
with:
repository: <your-org>/terraform-do-database
token: ${{ secrets.GIT_ACCESS_TOKEN }}
path: reusable-workflow-repo
Now you have all the code you need inside reusable-workflow-repo. Use ${GITHUB_WORKSPACE} to find the current path and simply append the path to the script.
- name: Verify the tag value
run: ${GITHUB_WORKSPACE}/reusable-workflow-repo/scripts/tag_verify.sh
I was able to solve it adding a few more commands to manually download the script and execute it.
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout#v3
# Runs a single command using the runners shell
- name: Check current directory
run: pwd
- name: Download the script
run: curl -o $PWD/tag_verify.sh https://raw.githubusercontent.com/foo/git-workflows/master/scripts/tag_verify.sh
- name: Give script permissions
run: chmod +x $PWD/tag_verify.sh
- name: Execute script
run: $PWD/tag_verify.sh
Following Kaleby Cadorin example but for the case where the script is in a private repository
- name: Download & run script
run: |
curl --header "Authorization: token ${{ secrets.MY_PAT }}" \
--header 'Accept: application/vnd.github.raw' \
--remote-name \
--location https://raw.githubusercontent.com/COMPANY/REPO/BRANCH/PATH/script.sh
chmod +x script.sh
./script.sh
Note: GITHUB_TOKEN doesn't seem to work here, a PAT is required.
According to this thread on github-community the script needs to be downloaded/checked out separatly.
The "reusable" workflow you posted is not reusable in this sense, because since it is not downloading the script the workflow can only run within its own repository (or a repository that already has the script).

Azure Devops yaml Pipeline - PowerShell file move, then commit

I am able to move files (using PowerShell) in my Azure DevOps yaml pipeline.
I am able to commit to my git repo via Azure DevOps yaml pipeline. (As found here)
The problem is that I cannot do both in the same pipeline. Moving files worked fine until I added the following commands in a nested template (gitCalled.yml):
steps:
- checkout: self
persistCredentials: true
- script: |
git config --global user.email Continuous.Integrator#yourcompany.com & git config --global user.name "Continuous.Integrator"
workingDirectory: $(System.DefaultWorkingDirectory)
- script: |
git checkout -b packaging-test
echo 'This is a test' > data.txt
git add -A
git commit -m "deployment $(Build.BuildNumber)"
git push --set-upstream origin packaging-test
displayName: Add data.txt file
workingDirectory: $(System.DefaultWorkingDirectory)
Specifically, the checkout/persistCredentials command is what appears to be the issue. The result, when using it, is that my powershell file is no longer able to be found. If I comment out that command, my git commands fail, but the powerShell file is found and ran.
I'm in an either-or situation, but I want to do both ;)
Here is the calling template:
parameters:
- name: aliasPackageName
type: string
stages:
- stage: PipelineMoveFiles
displayName: Pipeline
jobs:
- job: MoveFiles
displayName: MoveFiles
pool:
vmImage: ubuntu-latest
steps:
- pwsh: |
$numOfForceAppFiles = $(Build.SourcesDirectory)/PackageCreation/MoveFiles.ps1
Write-Host "##vso[task.setvariable variable=numOfForceAppFiles;isOutput=true]$numOfForceAppFiles"
name: movesFilesStep
displayName: Move files to their package folders
- pwsh: |
Write-Host "Number of files still to move: "$(movesFilesStep.numOfForceAppFiles)
if ( 0 -eq $numOfForceAppFiles )
{
Write-Host "All Files were moved"
}
else
{
Write-Host "All Files were NOT moved"
# exit 1;
}
displayName: Validate that all files were moved
- template: gitCalled.yml
ERROR:
Starting: Move files to their package folders
==============================================================================
Task : PowerShell
Description : Run a PowerShell script on Linux, macOS, or Windows
Version : 2.200.0
Author : Microsoft Corporation
Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
==============================================================================
Generating script.
========================== Starting Command Output ===========================
/usr/bin/pwsh -NoLogo -NoProfile -NonInteractive -Command . '/home/vsts/work/_temp/33de9584-b260-4422-b6db-b8cef3ea129d.ps1'
/home/vsts/work/1/s/PackageCreation/MoveFiles.ps1: /home/vsts/work/_temp/33de9584-b260-4422-b6db-b8cef3ea129d.ps1:2
Line |
2 | … umOfForceAppFiles = /home/vsts/work/1/s/PackageCreation/MoveFiles.ps1
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The term '/home/vsts/work/1/s/PackageCreation/MoveFiles.ps1'
| is not recognized as a name of a cmdlet, function, script
| file, or executable program. Check the spelling of the name,
| or if a path was included, verify that the path is correct and
| try again.
##[error]PowerShell exited with code '1'.
Finishing: Move files to their package folders
Any help would be much appreciated!!
Based on your YAML sample, you need to execute the Powershell file in PowerShell Inline script and get the output.
You can use the script to execute the ps file:
$numOfForceAppFiles = & "$(Build.SourcesDirectory)/PackageCreation/MoveFiles.ps1"
Refer to the following sample:
parameters:
- name: aliasPackageName
type: string
stages:
- stage: PipelineMoveFiles
displayName: Pipeline
jobs:
- job: MoveFiles
displayName: MoveFiles
pool:
vmImage: ubuntu-latest
steps:
- pwsh: |
       $numOfForceAppFiles = & "$(Build.SourcesDirectory)/PackageCreation/MoveFiles.ps1"
       
       Write-Host "##vso[task.setvariable variable=numOfForceAppFiles;isOutput=true]$numOfForceAppFiles"
displayName: Move files to their package folders
Instead of running the powershell and git commands in different steps (pwsh and script) I decided to roll them together. The following is a partial, but working code.
steps:
- checkout: self
persistCredentials: true
clean: true
- powershell: |
git --version
git config user.email Continuous.Integrator#yourcompany.com
git config user.name "Continuous.Integrator"
git checkout -b packaging-test
$numOfForceAppFiles = $(Build.SourcesDirectory)/PackageCreation/MoveFiles.ps1
git add -A
git commit -m "deployment $(Build.BuildNumber)"
git push --set-upstream origin packaging-test

Setting environment variable value from .ps1 script not working in Github Actions

I have two ps1 scripts in Github Actions.
My scenario:
The first script executes before build
Project builds
The second script executes after build.
I need to set the value inside the first script and use it inside the second script.
So I decided to use BUILD_NUMBER environment variable and set it to 10 as a default value.
jobs:
Droid:
runs-on: windows-latest
env:
BUILD_NUMBER: "10"
Inside the first script I tried to set this variable in several ways but in the second script the value of BUILD_NUMBER was 10.
My attempts to set it:
[Environment]::SetEnvironmentVariable($env:BUILD_NUMBER, $buildNumber, 'Machine')
$env:BUILD_NUMBER: '123'
But inside the second script I was getting 10 value by this $newName = "${env:BUILD_NUMBER}"
The whole code of Github Actions side:
name: CI
# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
push:
branches:
- 'master'
- 'develop'
- 'feature/*'
- 'rc/*'
pull_request:
branches:
- 'master'
- 'develop'
- 'feature/*'
- 'rc/*'
jobs:
Droid:
runs-on: windows-latest
env:
DOTNET_CLI_TELEMETRY_OPTOUT: 'true'
BUILD_NUMBER: "10"
steps:
- uses: actions/checkout#v1
- name: Run a calculate version and set sign in password script
run: .\Scripts\CalculateVersionAndSetSignPassword.ps1
shell: powershell
# Build goes here. It is skipped by me for testing purposes
- uses: actions/checkout#v1
- name: Run a change apk name script
run: |
.\Scripts\ChangeApkName.ps1
shell: powershell
set-env was depricated - please check GitHub Actions: Deprecating set-env and add-path commands
As a replacement you may use
echo "BUILD_NUMBER=yellow" >> $GITHUB_ENV
and then:
jobs:
show:
runs-on: ubuntu-latest
steps:
- name: Is variable exported?
run: |
echo "BUILD_NUMBER=yellow" >> $GITHUB_ENV
- name: PowerShell script
# You may pin to the exact commit or the version.
# uses: Amadevus/pwsh-script#25a636480c7bc678a60bbf4e3e5ac03aca6cf2cd
uses: Amadevus/pwsh-script#v2.0.0
continue-on-error: true
with:
# PowerShell script to execute in Actions-hydrated context
script: |
Write-Host $env:BUILD_NUMBER
- name: Read exported variable
run: |
echo "${{ env.BUILD_NUMBER}}"
To set environment variables in a step that can be referenced in another, you will need to use the ::set-env syntax.
In your case, your first script will have to run this command:
Write-Output "::set-env name=BUILD_NUMBER::$buildNumber"
And the second script should be able to reference it with $env:BUILD_NUMBER.
[6/20/20] Update with full example.
Action yaml file (Inline powershell will have similar behavior than with a ps1):
name: StackOverFlow
on:
push:
branches: [ master ]
jobs:
build:
runs-on: windows-latest
steps:
- run: |
$buildNumber = "12345"
Write-Output "::set-env name=BUILD_NUMBER::$buildNumber"
- run: Write-Output "Doing something else..."
- run: Write-Output "The build number is $env:BUILD_NUMBER"
Output logs:
2020-06-20T23:13:23.3209811Z ##[section]Starting: Request a runner to run this job
2020-06-20T23:13:23.5144969Z Can't find any online and idle self-hosted runner in current repository that matches the required labels: 'windows-latest'
2020-06-20T23:13:23.5145013Z Can't find any online and idle self-hosted runner in current repository's account/organization that matches the required labels: 'windows-latest'
2020-06-20T23:13:23.5145038Z Found online and idle hosted runner in current repository's account/organization that matches the required labels: 'windows-latest'
2020-06-20T23:13:23.6348644Z ##[section]Finishing: Request a runner to run this job
2020-06-20T23:13:29.9867339Z Current runner version: '2.263.0'
2020-06-20T23:13:29.9982614Z ##[group]Operating System
2020-06-20T23:13:29.9983190Z Microsoft Windows Server 2019
2020-06-20T23:13:29.9983380Z 10.0.17763
2020-06-20T23:13:29.9983515Z Datacenter
2020-06-20T23:13:29.9983691Z ##[endgroup]
2020-06-20T23:13:29.9983875Z ##[group]Virtual Environment
2020-06-20T23:13:29.9984067Z Environment: windows-2019
2020-06-20T23:13:29.9984247Z Version: 20200608.1
2020-06-20T23:13:29.9984524Z Included Software: https://github.com/actions/virtual-environments/blob/win19/20200608.1/images/win/Windows2019-Readme.md
2020-06-20T23:13:29.9984752Z ##[endgroup]
2020-06-20T23:13:29.9985890Z Prepare workflow directory
2020-06-20T23:13:30.0151643Z Prepare all required actions
2020-06-20T23:13:30.9154166Z ##[group]Run $buildNumber = "12345"
2020-06-20T23:13:30.9154566Z [36;1m$buildNumber = "12345"[0m
2020-06-20T23:13:30.9154784Z [36;1mWrite-Output "::set-env name=BUILD_NUMBER::$buildNumber"[0m
2020-06-20T23:13:30.9820753Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
2020-06-20T23:13:30.9821156Z ##[endgroup]
2020-06-20T23:13:43.2981407Z ##[group]Run Write-Output "Doing something else..."
2020-06-20T23:13:43.2981812Z [36;1mWrite-Output "Doing something else..."[0m
2020-06-20T23:13:43.3022226Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
2020-06-20T23:13:43.3022501Z env:
2020-06-20T23:13:43.3022706Z BUILD_NUMBER: 12345
2020-06-20T23:13:43.3022906Z ##[endgroup]
2020-06-20T23:13:43.8091340Z Doing something else...
2020-06-20T23:13:43.8671648Z ##[group]Run Write-Output "The build number is $env:BUILD_NUMBER"
2020-06-20T23:13:43.8671986Z [36;1mWrite-Output "The build number is $($env:BUILD_NUMBER)"[0m
2020-06-20T23:13:43.8717102Z shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '{0}'"
2020-06-20T23:13:43.8717288Z env:
2020-06-20T23:13:43.8718175Z BUILD_NUMBER: 12345
2020-06-20T23:13:43.8718286Z ##[endgroup]
2020-06-20T23:13:44.4148124Z The build number is 12345
2020-06-20T23:13:44.4368449Z Cleaning up orphan processes
Found the resolution in Michael Stum`s repo that he provided in this question:
The key was Get-ChildItem Env: | Where-Object {$_.Name -Match "^MH_"} | %{ echo "::set-output name=$($_.Name)::$($_.Value)" } in .yml and $Env:MH_BUILD_VERSION = $version in .ps1 script file in his repository.
So I successfully retrieved an output from .ps1 script and used it in Github Actions.

How do I use PowerShell with Gitlab CI in Gitlab Pages?

How do I use PowerShell commands/scripts with Gitlab CI in a .gitlab-ci.yml file which is used to deploy to gitlab pages?
I am trying to execute the build.ps1 file from .gitlab-ci.yml, but when it reaches the build.ps1 line, it gives an error saying
/bin/bash: line 5: .build.ps1: command not found
I am trying to use the PowerShell script to convert a file in my repo and have the converted file deployed to gitlab pages using .gitlab-ci.yml
Here is my code:
.gitlab.yml
pages:
stage: deploy
script:
- mkdir .public
- .\build.ps1
- cp -r * .public
- mv .public public
artifacts:
paths:
- public
only:
- master
I have been able to figure out a solution to my own question.
Solution
To Run PowerShell Command/Script from a .gitlab-ci.yml file on a gitlab.com using the Gitlab CI, you need to make sure that the contents of your .gitlab-ci.yml file is as shown below.
Note: The .gitlab-ci.yml below works without having to install a Gitlab Runner on your own machine and has been tested on the http://gitlab.com website.
image: philippheuer/docker-gitlab-powershell
pages:
stage: deploy
script:
- mkdir .public
# run PowerShell Script
- powershell -File build.ps1
# run PowerShell Command
- powershell -Command "Get-Date"
- cp -r * .public
- mv .public public
artifacts:
paths:
- public
only:
- master
The docker image philippheuer/docker-gitlab-powershell is outdated. The source on Github was also deleted.
I use in my gitlab-ci.yml the following image mcr.microsoft.com/powershell:latest more Informations available here
scriptjob:
stage: script
image:
name: "mcr.microsoft.com/powershell:latest"
script:
- pwsh ./myscript.ps1
For anyone who is having trouble launching grunt within their gitlab CI/CD via a powershell file, add this line to the top of your file:
$env:path += ";" + (Get-Item "Env:AppData").Value + "\npm"