Skip to content

Commit c7fd0c7

Browse files
Cache fix (#54)
* Some debug log * new user input: verbose_log * Using warning instead of error if there is any error during the Cachefile check/compare. * add more debug log * add `Cartfile.resolved` to the `BITRISE_CACHE_INCLUDE_PATHS` env, so the `Cache push` step will push it automatically. With that, the user does not need to do it manually 🎉 * update readme: removed the `./Carthage -> ./Carthage/Cachefile` step, because it will be handled by the `Carthage` step itself. * clean * Revert "add `Cartfile.resolved` to the `BITRISE_CACHE_INCLUDE_PATHS` env, so the `Cache push` step will push it automatically. With that, the user does not need to do it manually 🎉" This reverts commit b08d51b. * Update warnings about cache process * temporary branch change for the sample app to test the cache mechanism on CI * Revert "temporary branch change for the sample app to test the cache mechanism on CI" This reverts commit 0f74976. * PR clean - fix * clean + path fix * PR: log ifxes * dep -update github.com/bitrise-io/go-steputils
1 parent 4fd1c3c commit c7fd0c7

File tree

9 files changed

+131
-133
lines changed

9 files changed

+131
-133
lines changed

Gopkg.lock

Lines changed: 14 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
[[constraint]]
1010
branch = "master"
11-
name = "github.com/bitrise-tools/go-steputils"
11+
name = "github.com/bitrise-io/go-steputils"
1212

1313
[[constraint]]
1414
branch = "master"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Plain simple build step for setting up your project with [Carthage](https://gith
66

77
You can cache the result of `carthage bootstrap` to make Bitrise faster when building unchanged dependencies. For that you'll have to [set up caching](http://devcenter.bitrise.io/caching/about-caching/) in your Bitrise workflow.
88

9-
1. add a cache pull step before your Carthage step,
10-
2. set up a cache push step after your Carthage step and make sure to add `./Carthage -> ./Carthage/Cachefile`.
9+
1. add a `Cache pull` step **before** your `Carthage` step,
10+
2. add a `Cache push` step **after** your `Carthage` step,
1111

1212
The `Cachefile` stores a Swift version you ran `carthage bootstrap` the last time and the content of your `Cartfile.resolved`. Until either of these information is not changed between builds, Bitrise will ignore the `bootstrap` call and use the cached content of your `Carthage/Build` directory for building your project. If you have changes in your `Cartfile.resolved`, or changed the stack to one with a different Swift version, it will run `carthage bootstrap` to make sure the cache is only used when it's 100% compatible.
1313

main.go

Lines changed: 104 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ package main
33
// ConfigsModel ...
44
import (
55
"fmt"
6+
"io/ioutil"
67
"os"
78
"path/filepath"
89
"strings"
910

11+
"github.com/bitrise-io/go-steputils/cache"
12+
"github.com/bitrise-io/go-steputils/stepconf"
1013
"github.com/bitrise-io/go-utils/command"
1114
"github.com/bitrise-io/go-utils/fileutil"
1215
"github.com/bitrise-io/go-utils/log"
1316
"github.com/bitrise-io/go-utils/pathutil"
14-
"github.com/bitrise-tools/go-steputils/cache"
15-
"github.com/bitrise-tools/go-steputils/stepconf"
1617
version "github.com/hashicorp/go-version"
1718
"github.com/kballard/go-shellquote"
1819
)
@@ -32,6 +33,9 @@ type Config struct {
3233
CarthageCommand string `env:"carthage_command,required"`
3334
CarthageOptions string `env:"carthage_options"`
3435
SourceDir string `env:"BITRISE_SOURCE_DIR"`
36+
37+
// Debug
38+
VerboseLog bool `env:"verbose_log,opt[yes,no]"`
3539
}
3640

3741
func fail(format string, v ...interface{}) {
@@ -66,63 +70,119 @@ func contentOfFile(pth string) (string, error) {
6670
if exist, err := pathutil.IsPathExists(pth); err != nil {
6771
return "", err
6872
} else if !exist {
69-
return "", nil
73+
return "", fmt.Errorf("file not exists: %s", pth)
7074
}
7175

7276
return fileutil.ReadStringFromFile(pth)
7377
}
7478

75-
func isDirEmpty(dirPth string) (bool, error) {
76-
if exist, err := pathutil.IsDirExists(dirPth); err != nil {
77-
return false, err
78-
} else if !exist {
79-
return false, nil
80-
}
81-
82-
pattern := filepath.Join(dirPth, "*")
83-
files, err := filepath.Glob(pattern)
84-
if err != nil {
85-
return false, err
86-
}
87-
88-
return (len(files) == 0), nil
89-
}
90-
9179
func isCacheAvailable(srcDir string, swiftVersion string) (bool, error) {
80+
log.Printf("Check cache specific files")
81+
buildDirAvailable, cacheFileAvailable, resolvedFileAvailable := true, true, true
82+
9283
// check for built dependencies (Carthage/Build/*)
84+
fmt.Print("- Carthage/Build directory: ")
9385
carthageDir := filepath.Join(srcDir, carthageDirName)
9486
carthageBuildDir := filepath.Join(carthageDir, buildDirName)
95-
if empty, err := isDirEmpty(carthageBuildDir); err != nil {
96-
return false, err
97-
} else if empty {
98-
return false, nil
87+
88+
files, err := ioutil.ReadDir(carthageBuildDir)
89+
if err != nil {
90+
buildDirAvailable = false
91+
log.Errorf("not found")
92+
} else if len(files) == 0 {
93+
buildDirAvailable = false
94+
log.Errorf("empty")
95+
} else {
96+
log.Successf("found")
9997
}
98+
10099
// ---
101100

102101
// read cache indicator file (Carthage/Cachefile)
102+
fmt.Print("- Cachfile: ")
103+
var cacheFileContent string
104+
103105
cacheFilePth := filepath.Join(carthageDir, cacheFileName)
104-
cacheFileContent, err := contentOfFile(cacheFilePth)
105-
if err != nil {
106+
if exist, err := pathutil.IsPathExists(cacheFilePth); err != nil {
106107
return false, err
108+
} else if !exist {
109+
cacheFileAvailable = false
110+
log.Errorf("not available yet")
111+
} else {
112+
log.Successf("found")
113+
log.Debugf(cacheFileName + " exists in " + cacheFilePth + "\n")
114+
115+
cacheFileContent, err = contentOfFile(cacheFilePth)
116+
if err != nil {
117+
return false, err
118+
} else if cacheFileContent == "" {
119+
log.Errorf("Cachfile is empty")
120+
} else {
121+
log.Debugf(cacheFileName + " content: " + cacheFileContent + "\n")
122+
}
107123
}
108-
if cacheFileContent == "" {
109-
return false, nil
110-
}
124+
111125
// --
112126

113127
// read Cartfile.resolved
128+
fmt.Print("- Cartfile.resolved: ")
129+
var resolvedFileContent string
130+
114131
resolvedFilePath := filepath.Join(srcDir, resolvedFileName)
115-
resolvedFileContent, err := contentOfFile(resolvedFilePath)
116-
if err != nil {
132+
if exist, err := pathutil.IsPathExists(resolvedFileName); err != nil {
117133
return false, err
134+
} else if !exist {
135+
log.Errorf("not found")
136+
resolvedFileAvailable = false
137+
} else {
138+
log.Successf("found")
139+
140+
resolvedFileContent, err = contentOfFile(resolvedFilePath)
141+
if err != nil {
142+
return false, err
143+
144+
} else if resolvedFileContent == "" {
145+
return false, fmt.Errorf("Catfile.resolved is empty")
146+
}
147+
log.Debugf(resolvedFileName + " content: " + resolvedFileContent + "\n")
118148
}
119-
if resolvedFileContent == "" {
120-
return false, nil
121-
}
149+
122150
// ---
123151

124-
desiredCacheContent := fmt.Sprintf("--Swift version: %s --Swift version \n --%s: %s --%s", swiftVersion, resolvedFileName, resolvedFileContent, resolvedFileName)
125-
return cacheFileContent == desiredCacheContent, nil
152+
// Warn messages about the missing files
153+
154+
// Print the warning about the missing Cachefile only if the other required file (Cartfile.resolved) is available.
155+
// If the Cartfile.resolved is not found, then we don't want to mislead the user with this warning.
156+
if !cacheFileAvailable && resolvedFileAvailable {
157+
fmt.Println()
158+
log.Warnf("The " + cacheFileName + " is generated by the step. Probably cache not initialised yet (first cache push initialises the cache), nothing to worry about ;)")
159+
160+
}
161+
162+
if !resolvedFileAvailable {
163+
fmt.Println()
164+
log.Warnf("No "+resolvedFileName+" found at: %s", resolvedFilePath)
165+
log.Warnf("Make sure it's committed into your repository!")
166+
log.Warnf(resolvedFileName + " presence ensures that Bitrise will use exactly the same versions of dependencies as you in your local environment. ")
167+
log.Warnf("The dependencies will not be cached until the " + resolvedFileName + " file presents in the repository.")
168+
}
169+
170+
if buildDirAvailable && cacheFileAvailable && resolvedFileAvailable {
171+
desiredCacheContent := fmt.Sprintf("--Swift version: %s --Swift version \n --%s: %s --%s", swiftVersion, resolvedFileName, resolvedFileContent, resolvedFileName)
172+
if cacheFileContent != desiredCacheContent {
173+
log.Debugf(
174+
"Cachefile is not valid.\n" +
175+
"Desired cache content:\n" +
176+
desiredCacheContent +
177+
"CacheFile content:\n" +
178+
cacheFileContent,
179+
)
180+
181+
return false, nil
182+
}
183+
}
184+
185+
return (buildDirAvailable && cacheFileAvailable && resolvedFileAvailable), nil
126186
}
127187

128188
func collectCarthageCache(projectDir string) error {
@@ -153,6 +213,8 @@ func main() {
153213
}
154214
stepconf.Print(configs)
155215

216+
log.SetEnableDebugLog(configs.VerboseLog)
217+
156218
// Environment
157219
fmt.Println()
158220
log.Infof("Environment:")
@@ -167,7 +229,7 @@ func main() {
167229
if err != nil {
168230
fail("Failed to get swift version, error: %s", err)
169231
}
170-
log.Printf("- SwiftVersion: %s", strings.Replace(swiftVersion, "\n", " - ", -1))
232+
log.Printf("- SwiftVersion: %s", strings.Replace(swiftVersion, "\n", "- ", -1))
171233
// --
172234

173235
// Parse options
@@ -208,10 +270,14 @@ func main() {
208270

209271
cacheAvailable, err := isCacheAvailable(projectDir, swiftVersion)
210272
if err != nil {
211-
fail("Failed to check if cached is available, error: %s", err)
273+
log.Warnf("Failed to check if cached is available, error: %s", err)
212274
}
213275

214-
log.Printf("cache available: %v", cacheAvailable)
276+
if cacheAvailable {
277+
log.Successf("Cache available")
278+
} else {
279+
log.Errorf("Cache not available")
280+
}
215281

216282
if cacheAvailable {
217283
if err := collectCarthageCache(projectDir); err != nil {

step.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,12 @@ inputs:
6060
6161
__UNCHECK EVERY SCOPE BOX__ when creating this token. There is no reason this token needs access to private information.
6262
is_sensitive: true
63+
- verbose_log: "no"
64+
opts:
65+
category: Debug
66+
title: "Enable verbose logging?"
67+
description: Enable verbose logging?
68+
is_required: true
69+
value_options:
70+
- "yes"
71+
- "no"

vendor/github.com/bitrise-tools/go-steputils/cache/cache.go renamed to vendor/github.com/bitrise-io/go-steputils/cache/cache.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/bitrise-tools/go-steputils/stepconf/stepconf.go renamed to vendor/github.com/bitrise-io/go-steputils/stepconf/stepconf.go

File renamed without changes.

vendor/github.com/bitrise-tools/go-steputils/tools/tools.go renamed to vendor/github.com/bitrise-io/go-steputils/tools/tools.go

File renamed without changes.

vendor/github.com/bitrise-tools/go-steputils/input/input.go

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)