@@ -29,6 +29,7 @@ package environment
2929import (
3030 "errors"
3131 "fmt"
32+ "strconv"
3233 "strings"
3334 "sync"
3435 "time"
@@ -127,6 +128,21 @@ func newEnvironment(userVars map[string]string) (env *Environment, err error) {
127128 },
128129 fsm.Callbacks {
129130 "before_event" : func (e * fsm.Event ) {
131+ // If the event is START_ACTIVITY, we set up a new run number early on.
132+ // This used to be done inside the transition_startactivity, but then the new RN isn't available to the
133+ // before_START_ACTIVITY hooks. By setting it up here, we ensure the run number is available especially
134+ // to plugin hooks.
135+ if e .Event == "START_ACTIVITY" {
136+ runNumber , rnErr := the .ConfSvc ().NewRunNumber ()
137+ if rnErr != nil {
138+ e .Cancel (rnErr )
139+ return
140+ }
141+ env .currentRunNumber = runNumber
142+ rnString := strconv .FormatUint (uint64 (runNumber ), 10 )
143+ env .workflow .GetVars ().Set ("run_number" , rnString )
144+ env .workflow .GetVars ().Set ("runNumber" , rnString )
145+ }
130146 errHooks := env .handleHooks (env .Workflow (), fmt .Sprintf ("before_%s" , e .Event ))
131147 if errHooks != nil {
132148 e .Cancel (errHooks )
@@ -173,6 +189,13 @@ func newEnvironment(userVars map[string]string) (env *Environment, err error) {
173189 e .Event ,
174190 )
175191 }
192+
193+ // If the event is STOP_ACTIVITY, we remove the active run number after all hooks are done.
194+ if e .Event == "STOP_ACTIVITY" {
195+ env .currentRunNumber = 0
196+ env .workflow .GetVars ().Del ("run_number" )
197+ env .workflow .GetVars ().Del ("runNumber" )
198+ }
176199 },
177200 },
178201 )
0 commit comments