15

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
Ishan
  • 3,931
  • 11
  • 37
  • 59
  • I have used this and it worked without any issue. [Powershell GitLab-part3](http://www.powershell.amsterdam/2015/12/04/powershell-and-gitlab-ci-part-3/). You can go through all the parts to have a complete understanding on this. – Ranadip Dutta Jun 02 '17 at 03:39
  • I have been through the 5 tutorials on their website, but it requires having to install the Windows runner on your own machine and considerable setup before it would probably work. Why do you have to install the runner on your own machine for it to work. Previously when i used gitlab ci with .gitlab-ci.yml I did not have to install any application on my own machine. The static website created using different static generator on https://gitlab.com/pages/ is an example of that as you could just fork the repo to have your own website hosted using gitlab pages – Ishan Jun 02 '17 at 04:11

3 Answers3

16

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
Ishan
  • 3,931
  • 11
  • 37
  • 59
6

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
live2
  • 3,771
  • 2
  • 37
  • 46
0

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"
Colin
  • 1,758
  • 1
  • 19
  • 24