Skip to content

Commit f055d4a

Browse files
feat: support offline mode (#2128)
* Add: Actions Offline Mode * Add: Actions Offline Mode --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent f7a846d commit f055d4a

File tree

6 files changed

+31
-19
lines changed

6 files changed

+31
-19
lines changed

cmd/input.go

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type Input struct {
5555
replaceGheActionTokenWithGithubCom string
5656
matrix []string
5757
actionCachePath string
58+
actionOfflineMode bool
5859
logPrefixJobID bool
5960
networkName string
6061
useNewActionCache bool

cmd/root.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func Execute(ctx context.Context, version string) {
9797
rootCmd.PersistentFlags().StringVarP(&input.cacheServerAddr, "cache-server-addr", "", common.GetOutboundIP().String(), "Defines the address to which the cache server binds.")
9898
rootCmd.PersistentFlags().Uint16VarP(&input.cacheServerPort, "cache-server-port", "", 0, "Defines the port where the artifact server listens. 0 means a randomly available port.")
9999
rootCmd.PersistentFlags().StringVarP(&input.actionCachePath, "action-cache-path", "", filepath.Join(CacheHomeDir, "act"), "Defines the path where the actions get cached and host workspaces created.")
100+
rootCmd.PersistentFlags().BoolVarP(&input.actionOfflineMode, "action-offline-mode", "", false, "If action contents exists, it will not be fetch and pull again. If turn on this,will turn off force pull")
100101
rootCmd.PersistentFlags().StringVarP(&input.networkName, "network", "", "host", "Sets a docker network name. Defaults to host.")
101102
rootCmd.PersistentFlags().BoolVarP(&input.useNewActionCache, "use-new-action-cache", "", false, "Enable using the new Action Cache for storing Actions locally")
102103
rootCmd.SetArgs(args())
@@ -582,11 +583,12 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
582583
EventName: eventName,
583584
EventPath: input.EventPath(),
584585
DefaultBranch: defaultbranch,
585-
ForcePull: input.forcePull,
586+
ForcePull: !input.actionOfflineMode && input.forcePull,
586587
ForceRebuild: input.forceRebuild,
587588
ReuseContainers: input.reuseContainers,
588589
Workdir: input.Workdir(),
589590
ActionCacheDir: input.actionCachePath,
591+
ActionOfflineMode: input.actionOfflineMode,
590592
BindWorkdir: input.bindWorkdir,
591593
LogOutput: !input.noOutput,
592594
JSONLogger: input.jsonLogger,

pkg/common/git/git.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,11 @@ func findGitSlug(url string, githubInstance string) (string, string, error) {
221221

222222
// NewGitCloneExecutorInput the input for the NewGitCloneExecutor
223223
type NewGitCloneExecutorInput struct {
224-
URL string
225-
Ref string
226-
Dir string
227-
Token string
224+
URL string
225+
Ref string
226+
Dir string
227+
Token string
228+
OfflineMode bool
228229
}
229230

230231
// CloneIfRequired ...
@@ -302,12 +303,16 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) common.Executor {
302303
return err
303304
}
304305

306+
isOfflineMode := input.OfflineMode
307+
305308
// fetch latest changes
306309
fetchOptions, pullOptions := gitOptions(input.Token)
307310

308-
err = r.Fetch(&fetchOptions)
309-
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
310-
return err
311+
if !isOfflineMode {
312+
err = r.Fetch(&fetchOptions)
313+
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
314+
return err
315+
}
311316
}
312317

313318
var hash *plumbing.Hash
@@ -367,9 +372,10 @@ func NewGitCloneExecutor(input NewGitCloneExecutorInput) common.Executor {
367372
return err
368373
}
369374
}
370-
371-
if err = w.Pull(&pullOptions); err != nil && err != git.NoErrAlreadyUpToDate {
372-
logger.Debugf("Unable to pull %s: %v", refName, err)
375+
if !isOfflineMode {
376+
if err = w.Pull(&pullOptions); err != nil && err != git.NoErrAlreadyUpToDate {
377+
logger.Debugf("Unable to pull %s: %v", refName, err)
378+
}
373379
}
374380
logger.Debugf("Cloned %s to %s", input.URL, input.Dir)
375381

pkg/runner/reusable_workflow.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,11 @@ func cloneIfRequired(rc *RunContext, remoteReusableWorkflow remoteReusableWorkfl
102102
func(ctx context.Context) error {
103103
remoteReusableWorkflow.URL = rc.getGithubContext(ctx).ServerURL
104104
return git.NewGitCloneExecutor(git.NewGitCloneExecutorInput{
105-
URL: remoteReusableWorkflow.CloneURL(),
106-
Ref: remoteReusableWorkflow.Ref,
107-
Dir: targetDirectory,
108-
Token: rc.Config.Token,
105+
URL: remoteReusableWorkflow.CloneURL(),
106+
Ref: remoteReusableWorkflow.Ref,
107+
Dir: targetDirectory,
108+
Token: rc.Config.Token,
109+
OfflineMode: rc.Config.ActionOfflineMode,
109110
})(ctx)
110111
},
111112
nil,

pkg/runner/runner.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Config struct {
2323
Actor string // the user that triggered the event
2424
Workdir string // path to working directory
2525
ActionCacheDir string // path used for caching action contents
26+
ActionOfflineMode bool // when offline, use caching action contents
2627
BindWorkdir bool // bind the workdir to the job container
2728
EventName string // name of event to run
2829
EventPath string // path to JSON file to use for event.json in containers

pkg/runner/step_action_remote.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ func (sar *stepActionRemote) prepareActionExecutor() common.Executor {
106106

107107
actionDir := fmt.Sprintf("%s/%s", sar.RunContext.ActionCacheDir(), safeFilename(sar.Step.Uses))
108108
gitClone := stepActionRemoteNewCloneExecutor(git.NewGitCloneExecutorInput{
109-
URL: sar.remoteAction.CloneURL(),
110-
Ref: sar.remoteAction.Ref,
111-
Dir: actionDir,
112-
Token: github.Token,
109+
URL: sar.remoteAction.CloneURL(),
110+
Ref: sar.remoteAction.Ref,
111+
Dir: actionDir,
112+
Token: github.Token,
113+
OfflineMode: sar.RunContext.Config.ActionOfflineMode,
113114
})
114115
var ntErr common.Executor
115116
if err := gitClone(ctx); err != nil {

0 commit comments

Comments
 (0)