Search code examples
permissionstravis-cisudo

How to run golang test on travis with filesystem permission (beside sudo, if possible?)


I am trying to build my golang cli project with TravisCI. One of the problem found is when running test which create files, I got permission error.

enter image description here

But when trying to run the test with sudo, it changed to sudo: go: command not found

Here is the error snippet from travis:

enter image description here

Anyone can help with a solution?

Notes:

  1. I dont want to change my test to use sudo in the code, because later when it is used the user should decide by himself whether to use sudo or not

Solution

  • Answering my own question

    Compiling from the comments, I tried 2 options

    The first is to update the current working directory permission, with chmod 777 . (yes I set all, dont want to really bother with travis specific behavior), but turns out it doesn't work. Don't know for sure the cause this time, probably the dynamically generated binary also have permission issue

    The solution that works for me is reading the go binary path, then re-run it as sudo, as shown below

    script: 
      - GO_EXECUTABLE_PATH=$(which go)
      - echo $GO_EXECUTABLE_PATH
      - sudo $GO_EXECUTABLE_PATH test ./...