github.com/secure-build/gitlab-runner@v12.5.0+incompatible/docs/shells/index.md (about)

     1  # Shells supported by GitLab Runner
     2  
     3  GitLab Runner implements a few shell script generators that allow to execute
     4  builds on different systems.
     5  
     6  ## Overview
     7  
     8  The shell scripts contain commands to execute all steps of the build:
     9  
    10  1. `git clone`
    11  1. Restore the build cache
    12  1. Build commands
    13  1. Update the build cache
    14  1. Generate and upload the build artifacts
    15  
    16  The shells don't have any configuration options. The build steps are received
    17  from the commands defined in the [`script` directive in `.gitlab-ci.yml`][script].
    18  
    19  The currently supported shells are:
    20  
    21  | Shell         | Status             |  Description |
    22  | --------------| ------------------ |  ----------- |
    23  | `bash`        | Fully Supported    | Bash (Bourne-shell) shell. All commands executed in Bash context (default for all Unix systems) |
    24  | `sh`          | Fully Supported    | Sh (Bourne-shell) shell. All commands executed in Sh context (fallback for `bash` for all Unix systems) |
    25  | `cmd`         | Deprecated         | Windows Batch script. All commands are executed in Batch context. Default when no [`shell`](../configuration/advanced-configuration.md#the-runners-section) is specified. Due for removal on Jun 22, 2020 in favor of PowerShell. |
    26  | `powershell`  | Fully Supported    | Windows PowerShell script. All commands are executed in PowerShell context. Default when registering a new Runner in version 12.0 or newer. |
    27  
    28  If you want to select a particular shell to use other than the default, you will need to [specify the shell](../executors/shell.md#selecting-your-shell) in your `config.toml` file.
    29  
    30  ## Sh/Bash shells
    31  
    32  This is the default shell used on all Unix based systems. The bash script used
    33  in `.gitlab-ci.yml` is executed by piping the shell script to one of the
    34  following commands:
    35  
    36  ```bash
    37  # This command is used if the build should be executed in context
    38  # of another user (the shell executor)
    39  cat generated-bash-script | su --shell /bin/bash --login user
    40  
    41  # This command is used if the build should be executed using
    42  # the current user, but in a login environment
    43  cat generated-bash-script | /bin/bash --login
    44  
    45  # This command is used if the build should be executed in
    46  # a Docker environment
    47  cat generated-bash-script | /bin/bash
    48  ```
    49  
    50  ## Windows Batch
    51  
    52  NOTE: **Note:**
    53  In GitLab 11.11, the Windows Batch executor for the
    54  GitLab Runner was deprecated in favor of the [PowerShell](#powershell)
    55  executor. Support for Windows Batch will be removed in GitLab 13.0 (Jun
    56  22, 2020).
    57  
    58  TIP: **Tip:**
    59  You can execute batch scripts from PowerShell using `Start-Process
    60  "cmd.exe" "/c C:\Path\file.bat"` for old batch scripts not ported to
    61  PowerShell.
    62  
    63  Windows Batch is the default shell used on Windows when
    64  [`shell`](../configuration/advanced-configuration.md#the-runners-section) is not
    65  specified.
    66  
    67  It doesn't support executing the build in context of another user.
    68  
    69  The generated Batch script is executed by saving its content to file and
    70  passing the file name to the following command:
    71  
    72  ```bash
    73  cmd /Q /C generated-windows-batch.cmd
    74  ```
    75  
    76  This is how an example batch script looks like:
    77  
    78  ```bash
    79  @echo off
    80  setlocal enableextensions
    81  setlocal enableDelayedExpansion
    82  set nl=^
    83  
    84  
    85  echo Running on %COMPUTERNAME%...
    86  
    87  call :prescript
    88  IF !errorlevel! NEQ 0 exit /b !errorlevel!
    89  
    90  call :buildscript
    91  IF !errorlevel! NEQ 0 exit /b !errorlevel!
    92  
    93  call :postscript
    94  IF !errorlevel! NEQ 0 exit /b !errorlevel!
    95  
    96  goto :EOF
    97  :prescript
    98  SET CI=true
    99  SET CI_COMMIT_SHA=db45ad9af9d7af5e61b829442fd893d96e31250c
   100  SET CI_COMMIT_BEFORE_SHA=d63117656af6ff57d99e50cc270f854691f335ad
   101  SET CI_COMMIT_REF_NAME=master
   102  SET CI_JOB_ID=1
   103  SET CI_REPOSITORY_URL=http://gitlab.example.com/group/project.git
   104  SET CI_PROJECT_ID=1
   105  SET CI_PROJECT_DIR=Z:\Gitlab\tests\test\builds\0\project-1
   106  SET CI_SERVER=yes
   107  SET CI_SERVER_NAME=GitLab CI
   108  SET CI_SERVER_VERSION=
   109  SET CI_SERVER_REVISION=
   110  SET GITLAB_CI=true
   111  md "C:\\GitLab-Runner\\builds\\0\\project-1.tmp" 2>NUL 1>NUL
   112  echo multiline!nl!tls!nl!chain > C:\GitLab-Runner\builds\0\project-1.tmp\GIT_SSL_CAINFO
   113  SET GIT_SSL_CAINFO=C:\GitLab-Runner\builds\0\project-1.tmp\GIT_SSL_CAINFO
   114  md "C:\\GitLab-Runner\\builds\\0\\project-1.tmp" 2>NUL 1>NUL
   115  echo multiline!nl!tls!nl!chain > C:\GitLab-Runner\builds\0\project-1.tmp\CI_SERVER_TLS_CA_FILE
   116  SET CI_SERVER_TLS_CA_FILE=C:\GitLab-Runner\builds\0\project-1.tmp\CI_SERVER_TLS_CA_FILE
   117  echo Cloning repository...
   118  rd /s /q "C:\GitLab-Runner\builds\0\project-1" 2>NUL 1>NUL
   119  "git" "clone" "http://gitlab.example.com/group/project.git" "Z:\Gitlab\tests\test\builds\0\project-1"
   120  IF !errorlevel! NEQ 0 exit /b !errorlevel!
   121  
   122  cd /D "C:\GitLab-Runner\builds\0\project-1"
   123  IF !errorlevel! NEQ 0 exit /b !errorlevel!
   124  
   125  echo Checking out db45ad9a as master...
   126  "git" "checkout" "db45ad9af9d7af5e61b829442fd893d96e31250c"
   127  IF !errorlevel! NEQ 0 exit /b !errorlevel!
   128  
   129  IF EXIST "..\..\..\cache\project-1\pages\master\cache.tgz" (
   130    echo Restoring cache...
   131    "gitlab-runner-windows-amd64.exe" "extract" "--file" "..\..\..\cache\project-1\pages\master\cache.tgz"
   132    IF !errorlevel! NEQ 0 exit /b !errorlevel!
   133  
   134  ) ELSE (
   135    IF EXIST "..\..\..\cache\project-1\pages\master\cache.tgz" (
   136      echo Restoring cache...
   137      "gitlab-runner-windows-amd64.exe" "extract" "--file" "..\..\..\cache\project-1\pages\master\cache.tgz"
   138      IF !errorlevel! NEQ 0 exit /b !errorlevel!
   139  
   140    )
   141  )
   142  goto :EOF
   143  
   144  :buildscript
   145  SET CI=true
   146  SET CI_COMMIT_SHA=db45ad9af9d7af5e61b829442fd893d96e31250c
   147  SET CI_COMMIT_BEFORE_SHA=d63117656af6ff57d99e50cc270f854691f335ad
   148  SET CI_COMMIT_REF_NAME=master
   149  SET CI_JOB_ID=1
   150  SET CI_REPOSITORY_URL=Z:\Gitlab\tests\test
   151  SET CI_PROJECT_ID=1
   152  SET CI_PROJECT_DIR=Z:\Gitlab\tests\test\builds\0\project-1
   153  SET CI_SERVER=yes
   154  SET CI_SERVER_NAME=GitLab CI
   155  SET CI_SERVER_VERSION=
   156  SET CI_SERVER_REVISION=
   157  SET GITLAB_CI=true
   158  md "C:\\GitLab-Runner\\builds\\0\\project-1.tmp" 2>NUL 1>NUL
   159  echo multiline!nl!tls!nl!chain > C:\GitLab-Runner\builds\0\project-1.tmp\GIT_SSL_CAINFO
   160  SET GIT_SSL_CAINFO=C:\GitLab-Runner\builds\0\project-1.tmp\GIT_SSL_CAINFO
   161  md "C:\\GitLab-Runner\\builds\\0\\project-1.tmp" 2>NUL 1>NUL
   162  echo multiline!nl!tls!nl!chain > C:\GitLab-Runner\builds\0\project-1.tmp\CI_SERVER_TLS_CA_FILE
   163  SET CI_SERVER_TLS_CA_FILE=C:\GitLab-Runner\builds\0\project-1.tmp\CI_SERVER_TLS_CA_FILE
   164  cd /D "C:\GitLab-Runner\builds\0\project-1"
   165  IF !errorlevel! NEQ 0 exit /b !errorlevel!
   166  
   167  echo $ echo true
   168  echo true
   169  goto :EOF
   170  
   171  :postscript
   172  SET CI=true
   173  SET CI_COMMIT_SHA=db45ad9af9d7af5e61b829442fd893d96e31250c
   174  SET CI_COMMIT_BEFORE_SHA=d63117656af6ff57d99e50cc270f854691f335ad
   175  SET CI_COMMIT_REF_NAME=master
   176  SET CI_JOB_ID=1
   177  SET CI_REPOSITORY_URL=Z:\Gitlab\tests\test
   178  SET CI_PROJECT_ID=1
   179  SET CI_PROJECT_DIR=Z:\Gitlab\tests\test\builds\0\project-1
   180  SET CI_SERVER=yes
   181  SET CI_SERVER_NAME=GitLab CI
   182  SET CI_SERVER_VERSION=
   183  SET CI_SERVER_REVISION=
   184  SET GITLAB_CI=true
   185  md "C:\\GitLab-Runner\\builds\\0\\project-1.tmp" 2>NUL 1>NUL
   186  echo multiline!nl!tls!nl!chain > C:\GitLab-Runner\builds\0\project-1.tmp\GIT_SSL_CAINFO
   187  SET GIT_SSL_CAINFO=C:\GitLab-Runner\builds\0\project-1.tmp\GIT_SSL_CAINFO
   188  md "C:\\GitLab-Runner\\builds\\0\\project-1.tmp" 2>NUL 1>NUL
   189  echo multiline!nl!tls!nl!chain > C:\GitLab-Runner\builds\0\project-1.tmp\CI_SERVER_TLS_CA_FILE
   190  SET CI_SERVER_TLS_CA_FILE=C:\GitLab-Runner\builds\0\project-1.tmp\CI_SERVER_TLS_CA_FILE
   191  cd /D "C:\GitLab-Runner\builds\0\project-1"
   192  IF !errorlevel! NEQ 0 exit /b !errorlevel!
   193  
   194  echo Archiving cache...
   195  "gitlab-runner-windows-amd64.exe" "archive" "--file" "..\..\..\cache\project-1\pages\master\cache.tgz" "--path" "vendor"
   196  IF !errorlevel! NEQ 0 exit /b !errorlevel!
   197  
   198  goto :EOF
   199  ```
   200  
   201  ## PowerShell
   202  
   203  The default shell when a new Runner is registed using GitLab Runner
   204  12.0 or newer.
   205  
   206  PowerShell doesn't support executing the build in context of another user.
   207  
   208  The generated PowerShell script is executed by saving it content to a file and
   209  passing the file name to the following command:
   210  
   211  ```bash
   212  powershell -noprofile -noninteractive -executionpolicy Bypass -command generated-windows-powershell.ps1
   213  ```
   214  
   215  This is how an example PowerShell script looks like:
   216  
   217  ```bash
   218  $ErrorActionPreference = "Continue"
   219  
   220  echo "Running on $env:computername..."
   221  
   222  & {
   223    $CI="true"
   224    $env:CI=$CI
   225    $CI_COMMIT_SHA="db45ad9af9d7af5e61b829442fd893d96e31250c"
   226    $env:CI_COMMIT_SHA=$CI_COMMIT_SHA
   227    $CI_COMMIT_BEFORE_SHA="d63117656af6ff57d99e50cc270f854691f335ad"
   228    $env:CI_COMMIT_BEFORE_SHA=$CI_COMMIT_BEFORE_SHA
   229    $CI_COMMIT_REF_NAME="master"
   230    $env:CI_COMMIT_REF_NAME=$CI_COMMIT_REF_NAME
   231    $CI_JOB_ID="1"
   232    $env:CI_JOB_ID=$CI_JOB_ID
   233    $CI_REPOSITORY_URL="Z:\Gitlab\tests\test"
   234    $env:CI_REPOSITORY_URL=$CI_REPOSITORY_URL
   235    $CI_PROJECT_ID="1"
   236    $env:CI_PROJECT_ID=$CI_PROJECT_ID
   237    $CI_PROJECT_DIR="Z:\Gitlab\tests\test\builds\0\project-1"
   238    $env:CI_PROJECT_DIR=$CI_PROJECT_DIR
   239    $CI_SERVER="yes"
   240    $env:CI_SERVER=$CI_SERVER
   241    $CI_SERVER_NAME="GitLab CI"
   242    $env:CI_SERVER_NAME=$CI_SERVER_NAME
   243    $CI_SERVER_VERSION=""
   244    $env:CI_SERVER_VERSION=$CI_SERVER_VERSION
   245    $CI_SERVER_REVISION=""
   246    $env:CI_SERVER_REVISION=$CI_SERVER_REVISION
   247    $GITLAB_CI="true"
   248    $env:GITLAB_CI=$GITLAB_CI
   249    $GIT_SSL_CAINFO=""
   250    md "C:\GitLab-Runner\builds\0\project-1.tmp" -Force | out-null
   251    $GIT_SSL_CAINFO | Out-File "C:\GitLab-Runner\builds\0\project-1.tmp\GIT_SSL_CAINFO"
   252    $GIT_SSL_CAINFO="C:\GitLab-Runner\builds\0\project-1.tmp\GIT_SSL_CAINFO"
   253    $env:GIT_SSL_CAINFO=$GIT_SSL_CAINFO
   254    $CI_SERVER_TLS_CA_FILE=""
   255    md "C:\GitLab-Runner\builds\0\project-1.tmp" -Force | out-null
   256    $CI_SERVER_TLS_CA_FILE | Out-File "C:\GitLab-Runner\builds\0\project-1.tmp\CI_SERVER_TLS_CA_FILE"
   257    $CI_SERVER_TLS_CA_FILE="C:\GitLab-Runner\builds\0\project-1.tmp\CI_SERVER_TLS_CA_FILE"
   258    $env:CI_SERVER_TLS_CA_FILE=$CI_SERVER_TLS_CA_FILE
   259    echo "Cloning repository..."
   260    if( (Get-Command -Name Remove-Item2 -Module NTFSSecurity -ErrorAction SilentlyContinue) -and (Test-Path "C:\GitLab-Runner\builds\0\project-1" -PathType Container) ) {
   261      Remove-Item2 -Force -Recurse "C:\GitLab-Runner\builds\0\project-1"
   262    } elseif(Test-Path "C:\GitLab-Runner\builds\0\project-1") {
   263      Remove-Item -Force -Recurse "C:\GitLab-Runner\builds\0\project-1"
   264    }
   265  
   266    & "git" "clone" "https://gitlab.com/group/project.git" "Z:\Gitlab\tests\test\builds\0\project-1"
   267    if(!$?) { Exit $LASTEXITCODE }
   268  
   269    cd "C:\GitLab-Runner\builds\0\project-1"
   270    if(!$?) { Exit $LASTEXITCODE }
   271  
   272    echo "Checking out db45ad9a as master..."
   273    & "git" "checkout" "db45ad9af9d7af5e61b829442fd893d96e31250c"
   274    if(!$?) { Exit $LASTEXITCODE }
   275  
   276    if(Test-Path "..\..\..\cache\project-1\pages\master\cache.tgz" -PathType Leaf) {
   277      echo "Restoring cache..."
   278      & "gitlab-runner-windows-amd64.exe" "extract" "--file" "..\..\..\cache\project-1\pages\master\cache.tgz"
   279      if(!$?) { Exit $LASTEXITCODE }
   280  
   281    } else {
   282      if(Test-Path "..\..\..\cache\project-1\pages\master\cache.tgz" -PathType Leaf) {
   283        echo "Restoring cache..."
   284        & "gitlab-runner-windows-amd64.exe" "extract" "--file" "..\..\..\cache\project-1\pages\master\cache.tgz"
   285        if(!$?) { Exit $LASTEXITCODE }
   286  
   287      }
   288    }
   289  }
   290  if(!$?) { Exit $LASTEXITCODE }
   291  
   292  & {
   293    $CI="true"
   294    $env:CI=$CI
   295    $CI_COMMIT_SHA="db45ad9af9d7af5e61b829442fd893d96e31250c"
   296    $env:CI_COMMIT_SHA=$CI_COMMIT_SHA
   297    $CI_COMMIT_BEFORE_SHA="d63117656af6ff57d99e50cc270f854691f335ad"
   298    $env:CI_COMMIT_BEFORE_SHA=$CI_COMMIT_BEFORE_SHA
   299    $CI_COMMIT_REF_NAME="master"
   300    $env:CI_COMMIT_REF_NAME=$CI_COMMIT_REF_NAME
   301    $CI_JOB_ID="1"
   302    $env:CI_JOB_ID=$CI_JOB_ID
   303    $CI_REPOSITORY_URL="Z:\Gitlab\tests\test"
   304    $env:CI_REPOSITORY_URL=$CI_REPOSITORY_URL
   305    $CI_PROJECT_ID="1"
   306    $env:CI_PROJECT_ID=$CI_PROJECT_ID
   307    $CI_PROJECT_DIR="Z:\Gitlab\tests\test\builds\0\project-1"
   308    $env:CI_PROJECT_DIR=$CI_PROJECT_DIR
   309    $CI_SERVER="yes"
   310    $env:CI_SERVER=$CI_SERVER
   311    $CI_SERVER_NAME="GitLab CI"
   312    $env:CI_SERVER_NAME=$CI_SERVER_NAME
   313    $CI_SERVER_VERSION=""
   314    $env:CI_SERVER_VERSION=$CI_SERVER_VERSION
   315    $CI_SERVER_REVISION=""
   316    $env:CI_SERVER_REVISION=$CI_SERVER_REVISION
   317    $GITLAB_CI="true"
   318    $env:GITLAB_CI=$GITLAB_CI
   319    $GIT_SSL_CAINFO=""
   320    md "C:\GitLab-Runner\builds\0\project-1.tmp" -Force | out-null
   321    $GIT_SSL_CAINFO | Out-File "C:\GitLab-Runner\builds\0\project-1.tmp\GIT_SSL_CAINFO"
   322    $GIT_SSL_CAINFO="C:\GitLab-Runner\builds\0\project-1.tmp\GIT_SSL_CAINFO"
   323    $env:GIT_SSL_CAINFO=$GIT_SSL_CAINFO
   324    $CI_SERVER_TLS_CA_FILE=""
   325    md "C:\GitLab-Runner\builds\0\project-1.tmp" -Force | out-null
   326    $CI_SERVER_TLS_CA_FILE | Out-File "C:\GitLab-Runner\builds\0\project-1.tmp\CI_SERVER_TLS_CA_FILE"
   327    $CI_SERVER_TLS_CA_FILE="C:\GitLab-Runner\builds\0\project-1.tmp\CI_SERVER_TLS_CA_FILE"
   328    $env:CI_SERVER_TLS_CA_FILE=$CI_SERVER_TLS_CA_FILE
   329    cd "C:\GitLab-Runner\builds\0\project-1"
   330    if(!$?) { Exit $LASTEXITCODE }
   331  
   332    echo "`$ echo true"
   333    echo true
   334  }
   335  if(!$?) { Exit $LASTEXITCODE }
   336  
   337  & {
   338    $CI="true"
   339    $env:CI=$CI
   340    $CI_COMMIT_SHA="db45ad9af9d7af5e61b829442fd893d96e31250c"
   341    $env:CI_COMMIT_SHA=$CI_COMMIT_SHA
   342    $CI_COMMIT_BEFORE_SHA="d63117656af6ff57d99e50cc270f854691f335ad"
   343    $env:CI_COMMIT_BEFORE_SHA=$CI_COMMIT_BEFORE_SHA
   344    $CI_COMMIT_REF_NAME="master"
   345    $env:CI_COMMIT_REF_NAME=$CI_COMMIT_REF_NAME
   346    $CI_JOB_ID="1"
   347    $env:CI_JOB_ID=$CI_JOB_ID
   348    $CI_REPOSITORY_URL="Z:\Gitlab\tests\test"
   349    $env:CI_REPOSITORY_URL=$CI_REPOSITORY_URL
   350    $CI_PROJECT_ID="1"
   351    $env:CI_PROJECT_ID=$CI_PROJECT_ID
   352    $CI_PROJECT_DIR="Z:\Gitlab\tests\test\builds\0\project-1"
   353    $env:CI_PROJECT_DIR=$CI_PROJECT_DIR
   354    $CI_SERVER="yes"
   355    $env:CI_SERVER=$CI_SERVER
   356    $CI_SERVER_NAME="GitLab CI"
   357    $env:CI_SERVER_NAME=$CI_SERVER_NAME
   358    $CI_SERVER_VERSION=""
   359    $env:CI_SERVER_VERSION=$CI_SERVER_VERSION
   360    $CI_SERVER_REVISION=""
   361    $env:CI_SERVER_REVISION=$CI_SERVER_REVISION
   362    $GITLAB_CI="true"
   363    $env:GITLAB_CI=$GITLAB_CI
   364    $GIT_SSL_CAINFO=""
   365    md "C:\GitLab-Runner\builds\0\project-1.tmp" -Force | out-null
   366    $GIT_SSL_CAINFO | Out-File "C:\GitLab-Runner\builds\0\project-1.tmp\GIT_SSL_CAINFO"
   367    $GIT_SSL_CAINFO="C:\GitLab-Runner\builds\0\project-1.tmp\GIT_SSL_CAINFO"
   368    $env:GIT_SSL_CAINFO=$GIT_SSL_CAINFO
   369    $CI_SERVER_TLS_CA_FILE=""
   370    md "C:\GitLab-Runner\builds\0\project-1.tmp" -Force | out-null
   371    $CI_SERVER_TLS_CA_FILE | Out-File "C:\GitLab-Runner\builds\0\project-1.tmp\CI_SERVER_TLS_CA_FILE"
   372    $CI_SERVER_TLS_CA_FILE="C:\GitLab-Runner\builds\0\project-1.tmp\CI_SERVER_TLS_CA_FILE"
   373    $env:CI_SERVER_TLS_CA_FILE=$CI_SERVER_TLS_CA_FILE
   374    cd "C:\GitLab-Runner\builds\0\project-1"
   375    if(!$?) { Exit $LASTEXITCODE }
   376  
   377    echo "Archiving cache..."
   378    & "gitlab-runner-windows-amd64.exe" "archive" "--file" "..\..\..\cache\project-1\pages\master\cache.tgz" "--path" "vendor"
   379    if(!$?) { Exit $LASTEXITCODE }
   380  
   381  }
   382  if(!$?) { Exit $LASTEXITCODE }
   383  ```
   384  
   385  [script]: http://doc.gitlab.com/ce/ci/yaml/README.html#script