-1

I am trying to update contents of a file from a variable with sed during Gitlab CI job. The variable comes from artifacts of the previous stage version. If simplified, my job looks something like this:

build-android-dev:
  stage: build
  dependencies:
    - version
  only:
    - mybranch
  before_script:
    - PUBSPEC_VERSION="`cat app-pubspec-version`"
    - echo "Pubspec version - $PUBSPEC_VERSION"
  script:
    - >
      sed -i -E "s/^(version: )(.+)$/\1${PUBSPEC_VERSION}/g" pubspec.yaml
    - cat pubspec.yaml | grep version
  interruptible: true
  tags:
    - macmini-flutter

Unfortunatelly, the job fails with the following error message:

$ sed -i -E "s/^(version: )(.+)$/\1${PUBSPEC_VERSION}/g" pubspec.yaml
sed: 1: "s/^(version: )(.+)$/\14 ...": \1 not defined in the RE
Cleaning up project directory and file based variables
00:00
ERROR: Job failed: exit status 1

PUBSPEC_VERSION coming from artifacts is the following:

$ echo "Pubspec version - $PUBSPEC_VERSION"
Pubspec version - 4.0.0+2

I am able to execute the command on my local Ubuntu (Linux) machine without any issues:

$ export PUBSPEC_VERSION=4.0.0+2
$ sed -i -E "s/^(version: )(.+)$/\1${PUBSPEC_VERSION}/g" pubspec.yaml
$ cat pubspec.yaml | grep version
version: 4.0.0+2

The remote machine where Gitlab Runner is started is MacOS. Not sure whether it matters.

As you can see, I also use folding style in my CI configuration like proposed here in order to avoid inproper colon interpretation.

I googled for solutions to solve the issue but it seems that I don't need to escape (though I also tried) group parentheses in my regular expression because I use extended regular expression.

So I'm stuck on it...

P.S. I don't have access to the shell of remote MacOS.

ezze
  • 3,933
  • 2
  • 34
  • 58
  • 1
    Does this answer your question? [In-place edits with sed on OS X](https://stackoverflow.com/questions/7573368/in-place-edits-with-sed-on-os-x) – Wiktor Stribiżew May 17 '22 at 08:12
  • @WiktorStribiżew This question is related but it doesn't explain the difference between commands in MacOS and Linux . – ezze May 17 '22 at 10:49

1 Answers1

2

is MacOS.

-i takes a suffix argument, so -E is the backup suffix to create. Yuo would want:

- sed -i '' -E 's/...'
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Excellent answer. Suggesting to check the documentation as well: https://www.unix.com/man-page/osx/1/sed/ – Dudi Boy May 17 '22 at 08:06
  • Thanks, it works in MacOS now but it breaks the command in Linux: `sed: can't read s/^(version: )(.+)$/\14.0.0+2/g: No such file or directory`. Should I distinguish it manually? – ezze May 17 '22 at 08:22
  • 1
    `Should I distinguish it manually?` yes, they are different. – KamilCuk May 17 '22 at 10:38