How to use multiline command in 'script:' with YAML?
What you are looking for is |
. Here is a very clear example to refer to.
build:
commands:
- echo "${CODEBUILD_BUILD_ARN}"
- |
if expr "${CODEBUILD_BUILD_ARN}" : ".*build/MyProjectDev-" >/dev/null; then
yarn run build-dev;
fi
- |
if expr "${CODEBUILD_BUILD_ARN}" : ".*build/MyProject-" >/dev/null; then
yarn run build-prod;
fi
I use the folded block style to achieve exactly the desired effect:
script:
- >
valgrind
--read-var-info=yes
--error-exitcode=1
--fullpath-after=
--track-origins=yes
--leak-check=full
--num-callers=20
--suppressions=$(pwd)/tests/zephir_parser.3.7.0.sup
$(phpenv which php)
-d variables_order=EGPCS
run-tests.php
-p $(which php)
-d extension=$(pwd)/modules/zephir_parser.so
-d variables_order=EGPCS
-g "FAIL,XFAIL,BORK,WARN,LEAK,SKIP"
--offline
--show-diff
--set-timeout 120
Note: Using Yaml folded block style, each line break is replaced by a space. The indention in each line will be ignored. A line break will be inserted at the end. Thus, you must not use \
here:
script:
- >
valgrind \ <- wrong
--read-var-info=yes
Otherwise it will be treated as valgrind \ --read-var-info=yes
.
Another working example is Line Folding. Consider the following example of the Line Folding which is supported by GitHub Actions:
- name: Configure (x64 Debug)
run: >-
cmake
-S .
-Bbuild
-DCMAKE_BUILD_TYPE=Debug
-DCPPCHECK=ON
-DWARNINGS_AS_ERRORS=ON
-DCMAKE_INSTALL_PREFIX=/opt/my-program
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
Note: In this example we use >-
right after run:
and lines are left aligned, using the same indentation at the beginning of the line.
In YAML you can specify newlines in a scalar by using ""
quoting and escaping the newlines (\n
), or, and that is more natural for your case, by using a literal style block scalar:
script:
- |
vim -Nu <(cat <<-EOF
set nocompatible |
filetype off
EOF
) -c 'Script' > /dev/null
This is a scalar starting with a line with a |
(pipe symbol), followed by multiple lines for which the line-breaks are preserved.
- The lines are normally indented (exception: a single top-level literal style block scalar).
- After the
|
there can be modifiers:1
-9
, used when your first line starts with spaces;+
,-
to influence stripping of final newlines (normally collapsed into one).