Tinus EngOps Wiki

Logo

OpsaC - Operating as PowerShell code

Links

Home

PowerShell Blog

PowerShell Index

PowerShell Search

Additional Websites

View my GitHub Profile

View my GitHub Gists

View Tinus IT Wiki

View my Photo Website

GitLab Configuration

tags: PowerShell categories: GitLab

Table of Contents

GitLab is your friend - UNDER CONSTRUCTION!

GitLab Configuration

$GitLabUrl = 'https://gitlab.company.com'
git config --global --list
git config --global http.sslverify false
git config --global user.name "First name Last name"
git config --global user.email "firstname.lastname@company.com"
git config --global credential.$GitLabUrl.username "Git TokenName"
$Account  = 'account@company.com'
$ProxyUrl = 'proxy.company.com:8080'
git config --global http.$GitLabUrl.proxy http://$Account@$ProxyUrl

Gitlab commands:

git clone
git status
git pull
git branch
git checkout
git add
git commit
git push

GitLab Runner

Gitlab-runner commands:

gitlab-runner.exe install
gitlab-runner.exe stop
gitlab-runner.exe start
gitlab-runner.exe verify

GitLab behind a proxy

I forgot when, how exactly and why its needed …

[System.Environment]::SetEnvironmentVariable('HTTPS_PROXY','proxy.company.com:8080',[System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable('NO_PROXY','gitlab.company.com',[System.EnvironmentVariableTarget]::Machine)

Maybee with the register param?

--env HTTP_PROXY=http://my_proxy_url:proxy_port

Maybee in a file named .env?

HTTPS_PROXY=gitlab.company.com:8080
NO_PROXY=gitlab.company.com

Maybee in the config.toml?

[[runners]]
  environment = ["HTTP_PROXY=http://user:passwd@proxy:8080", "HTTPS_PROXY=http://user:passwd@proxy:8080"]
  ...

And also added registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\gitlab-runner Environment REG_MULTI_SZ HTTPS_PROXY=http://user:passwd@proxy:8080

Register the Runner

Register the Runner non-interactive with PowerShell as executor:

$GitLabProjectToken      = 'YourProjectToken'
$GitLabRunnerTag         = 'ps-modules'
$GitLabRunnerDescription = 'MyGitLabRunner' # The Description (Display name) in GitLab under CI/CD Settings, Runners
$GitLabUrl               = 'https://gitlab.company.com/'

gitlab-runner.exe register --non-interactive --registration-token $GitLabProjectToken --url $GitLabUrl --shell powershell --executor shell --tag-list $GitLabRunnerTag --description $GitLabRunnerDescription

Register the Runner non-interactive with PowerShell as executor with a certificate:

$GitLabProjectToken      = 'YourProjectToken'
$GitLabRunnerTag         = 'ps-modules'
$GitLabRunnerDescription = 'MyGitLabRunner' # The Description (Display name) in GitLab under CI/CD Settings, Runners
$GitLabCertificatePath   = 'D:\GitLab\gitlab.cer'
$GitLabUrl               = 'https://gitlab.company.com/'

gitlab-runner.exe register --non-interactive --registration-token $GitLabProjectToken --url $GitLabUrl --shell powershell --executor shell --tag-list $GitLabRunnerTag --description $GitLabRunnerDescription --tls-ca-file $GitLabCertificatePath

Check the GitLab Runner configuration config.toml and make sure that the shell is powershell and pre_build_script = "$ErrorActionPreference = 'Stop'" (for example):

concurrent = 8
check_interval = 0
log_level = "warning"
 
[session_server]
  session_timeout = 1800
 
[[runners]]
  name = "GitLabRunnerFQDN"
  url = "GitLabUrl"
  token = "GitLabToken"
  executor = "shell"
  pre_build_script = "$ErrorActionPreference = 'Stop'"
  shell = "powershell"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]

Runner with a certificate

[[runners]]
  name = "GitLabRunnerFQDN"
  url = "GitLabUrl"
  token = "GitLabToken"
  tls-ca-file = "D:\\GitLab\\gitlab.cer"
  executor = "shell"
  pre_build_script = "$ErrorActionPreference = 'Stop'"
  shell = "powershell"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
    [runners.cache.azure]

Troubleshooting

Verify GitLab-Runner:

gitlab-runner.exe verify

Get-WinEvent -ProviderName gitlab-runner

Error message:

proxyconnect tcp: dial tcp proxy-ip-address:8080:
connectex: A connection attempt failed because the connected party did not properly respond after a
period of time, or established connection failed because connected host has failed to respond.

Ensure that the HTTPS-Proxy is configured if your GitLab is behind a Proxy:

$env:HTTPS_PROXY
[System.Environment]::SetEnvironmentVariable('HTTPS_PROXY','proxy.company.com:8080',[System.EnvironmentVariableTarget]::Machine)

Set NO_PROXY for GitLab if its needed (I forgot when its needed):

$env:NO_PROXY
[System.Environment]::SetEnvironmentVariable('NO_PROXY','gitlab.company.com',[System.EnvironmentVariableTarget]::Machine)

See also

How to set up GitLab Runner on a Windows Server on Ole Rand-Hendriksen’s Blog