It's probably better to do this the other way around, first build (but don't save) the action, and then do the publish which will save everything in one go.  This avoids doing 2 writes.