If the names of each layer and each group are unique and do not repeat within the same file, then this will not be a problem.
Just start recording the action and perform all the necessary operations (don't forget that the starting point is important for actions, i.e. it's better to select a certain layer with the first command).
You can illustrate your task with screenshots of the expanded layers palette, this will allow you to give more accurate advice.