Finally everything seems to work now … took me forever to (snap)craft everything i.e. two weeks.
@sergiusens - thanks, SNAPCRAFT_PROJECT_DIR was exactly what I was looking for. But it was not available until I added base: core to snapcraft.yaml - took me few hours/days to discover that - yes, I know that there is a warning that it is missing but would like to have more explicit warning that I’m trying to use something that will not work until add base core.
Next problem was that the VM memory was not enough to build everything. And the build miserably failed with random errors not related to the lack of free memory at all. It would be good to monitor VM free memory and warn when it fails below 10%. Running snapcraft with SNAPCRAFT_BUILD_ENVIRONMENT_MEMORY=8G snapcraft solves the problem but I would like to put that directly into snapcraft.yaml because it is mandatory for that snap - something like:
build-environment:
- memory: 8G
- disk: 200G
Next problem was with nodejs plugin - current npm implementation doesn’t work well when it is running as root - sometimes it tries to impersonate as root parent user which causes confusing ownership errors. That was real pain to be debugged and fixed - took me more than week - solution was to set explicitly correct SUDO_UID, SUDO_GID, SUDO_USER to make root parent of root. Final working part is:
parts:
treedys-controller:
plugin: nodejs
nodejs-version: '10.13.0'
nodejs-package-manager: 'npm'
source: controller/app # For checking source updates only
build-environment:
- SUDO_UID: '0'
- SUDO_GID: '0'
- SUDO_USER: 'root'
override-pull: |
snapcraftctl pull # call snapcraft to fetch node binaries and app source
# cleaning package.json from `devDependencies` and `scripts`
cat package.json | jq '{name, version, description, main, bin, author, license, private, dependencies, optionalDependencies}' > new.json
mv new.json package.json
# remove npm artefacts
rm -rf .gitignore .npmignore .npmrc node_modules package package-lock.json
# remove build artefacts
rm -rf app.webpack.config.js src webpack.config.js www
build-packages:
- 'build-essential'
- 'jq'
- 'git'
- 'python'
At the beginning I was trying to export SUDO_UID=0 but it was not working when npm is invoked by snapcraftctl pull - just because it executes from the context of snapcraft not from the override-pull - this is not obvious and needs to documented to avoid any confusion + adding example how to pass environment variables to snapcraftctl. I would like to be able to set different environment variables for each step (pull, build, stage, etc) that can be overridden. Another pain was that nodejs needs additional build-packages to work properly with native modules - worth to be added by default (build-essential, git, python, etc).