2

I have a bash script which is deployed with cloud init, my bash script contains the following part of code

GO111MODULE=on go install . ./cmd/...

when running my bash script directly in the terminal of the deplyed server, it works as expected. But when i run it with runcmd in the cloud config, this part of the script:

GO111MODULE=on go install . ./cmd/...

does not get executed, anyone knows why?

    runcmd:
  - [ bash, /usr/local/bin/myscript.sh ]
iFadi
  • 915
  • 2
  • 12
  • 20

2 Answers2

2

A proper shell execution in runcmd would be (as seen in Cloud config examples):

- [ bash, -c, /usr/local/bin/myscript.sh ]

or:

- [ /usr/local/bin/myscript.sh ]

Assuming your script starts with a shebang #!/bin/bash

Plus, you need to add any environment variable inside the script, as Cloud config examples do not include any obvious way to set them.

#!/bin/bash
export GO111MODULE=on
export ...
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • myscript.sh gets executed with runcmd, but the part with GO111MODULE=on, does not get executed, or it does get executed but with wrong paths, not sure!! running myscript.sh directly on the server, works fine – iFadi Nov 23 '21 at 12:00
  • @iFadi I would add `export GO111MODULE=on` *inside* your script `myscript.sh`, and make sure said script is called with `bash -c`. – VonC Nov 23 '21 at 12:08
  • thx for the tip, still it did not work, i found under /var/log/syslog the following error after the GO command gets executed: `build cache is required, but could not be located: GOCACHE is not defined and neither $XDG_CACHE_HOME nor $HOME are defined` i'm not sure how to fix it. – iFadi Nov 23 '21 at 12:15
  • @iFadi I have edited my answer to include environment variables. – VonC Nov 23 '21 at 13:08
1

Thanks to the tip from VonC, i was able to fix the issue. i added the following to myscript.sh

GOCACHE=/root/.cache/go-build
export GOCACHE
export GO111MODULE=on
go install . ./cmd/...


runcmd:
- [ bash, -c, /usr/local/bin/myscript.sh ]

the script now deploys and runs from cloud-init.

iFadi
  • 915
  • 2
  • 12
  • 20