Best Practices for SVN/GIT on projects

Incidental nice features: switching from system.util.execute to subprocess.call will include the output of the git command in the wrapper log, and switching from print to a system.util.getLogger object also adds in more hints about the source of the trigger.

INFO   | jvm 1    | 2019/11/27 16:54:11 | I [c.revision_helper             ] [16:54:11]: Updated ignition1:customer project-name=customer, request-origin=1.2.3.4, session-user=jbuser, session-project=customer, session-id=34947258
INFO   | jvm 1    | 2019/11/27 16:54:11 | [master e78f6c9] Updated ignition1:customer
INFO   | jvm 1    | 2019/11/27 16:54:11 |  4 files changed, 11 insertions(+), 8 deletions(-)
I

Hmm, you could try something like this to get your own access to that meta-information. It’s not guaranteed to be available, but you can try:

from org.slf4j import MDC
user = MDC.get("session-user")
if user is not None:
# you have an actual username

Should probably use org.slf4j.MDC instead.

1 Like

In case anyone lands here in the forum first, the IA team just recently put out an excellent whitepaper discussing issues and methods related to deploying Ignition on revision control like git:

Hi everyone! Future me, leaving more breadcrumbs of knowledge for others who follow…

As tempting as it may be, do NOT add resources.json or thumbnail.png to your gitignore, both will cause things to break. (tested circa Ignition 8.1.7)

Ignoring resources.json files will cause (sometimes cyclical) thrashing of the project files whenever you sync to a new checkout, but more importantly will totally break the newer method of saving named SQL queries to disk.

Ignoring thumbnail.png files would’ve been nice to avoid putting binaries in git, but the first time you update a server and gain views without those files, your log will be full of lines like this:

Error creating digest for resourceManifest=data/projects/Support/com.inductiveautomation.perspective/views/Page/Monitoring/resource.json, file=thumbnail.png 
Error reading data for file thumbnail.png for resource at com.inductiveautomation.perspective/views/Page/Monitoring 
###Fatal-An error occurred while reading project resources from disk. Disk-to-memory synchronization will not occur! 

I hope that maybe IA can resolve the second and allow Ignition to sync up projects from disk without PNG files, but we’ve gotta leave them in for now.

No, we have no intention of doing this.

A resource is comprised of everything inside the resource directory. You should ignore nothing.

We are, however, likely going to stop writing back an “externally modified” entry to the resource upon scanning it and noticing the signature is incorrect. We’ll just let it be wrong in the file and the in-memory / UI representation will still indicate there is a mismatch when necessary.

2 Likes

I for one love this. Our devs commit to Azure dev ops and I get the thumbnail of the view along with the code diff. Lots of time I don’t even need to open the designer to review changes. Just signoff right from the git commit.

Any news on the gateway event script? I have been looking for a script to run on gateway (ignition docker filesystem repo + git) for updating GitLab on save etc.

This is not documented in the Best Practice etc, only showing how to work with git on local PC.

Best regards
mwd123

What are you looking for, exactly?
The project update script was updated in 8.1.14 to have additional contextual information; actor and resources:
https://docs.inductiveautomation.com/display/DOC81/Gateway+Event+Scripts#GatewayEventScripts-UpdateScript

What I was looking for actually was this:

logger = system.util.getLogger("GW_UpdateScript")
logger.info("Running git update!")
project = system.util.getProjectName()
path="/usr/local/bin/ignition/data/projects/"
import time
try:
	system.util.execute(["git","config","--global","user.name","XXX - XXX"])
	system.util.execute(["git","config","--global","user.email","XXX@XXX.com"])
	system.util.execute(["git","-C",path+project,"add","."])
		time.sleep(5)
	dateStr=str(system.date.now())
	system.util.execute(["git","-C",path+project,"commit","-m","Designer save @ "+dateStr])
	time.sleep(5)
	system.util.execute(["git","-C",path+project,"push"])
	logger.info("git update done!")
except Exception as e:
	logger.infof("error, errorMsg: \n",e)

Edit: Now added system.util.getProjectName and it seems to work fine.

You are taking your chances with system.util.execute. I don't think it waits for the process to run, so those three commands could execute out of order. Use java's ProcessBuilder to ensure things are sequential (and you can check for success).

{ Also, please format your code properly. Edit your comment, highlight the code, and click the </> button. }

1 Like

Fixed the code highlight, thanks :slight_smile: new to the forum and sadly don't have enough coding experience for doing anything else than a simple bash-script :frowning:

But with that in mind I assume its smarter to call the bash-script instead. Will change to that! Thanks for pointing this out.

Oh, and if you make sure things are sequential, you won't need to use sleep(), which is evil and should never be used in Ignition. /:

time.sleep(5) is in the Best Practices from IA for using git.
Sadly I don't know how to code it on my own, abit surprised that there is no real good working example that covers what I do in the script I posted earlier.

Oy!

Since I don't have to do this myself, I hadn't run across that. IA should fix that. :frowning_face:

What about:

from java.lang import Thread
Thread.sleep(5000)

Etc.. better or worse?
(Saw this in some other post that it could be better)

Not better or worse. Just as evil. The problem is any kind of sleep (or infinite loop) tying up a thread. Event threads are typically part of a limited thread pool, impacting other tasks.

Do you have any example using ProcessBuilder to execute code on the gateway? (like I did with system.util.execute) ?

Thanks in advance
/M

From a quick search for ProcessBuilder on this forum:

Yes I checked all the posts on the forum that contains ProcessBuilder. :slight_smile: (I did't get any wiser doh:))

Will try to give it some more time tomorrow.