Skip to content

Commit 26afe97

Browse files
authored
Merge pull request #67 from bitrise-io/ACI-3019
Add write gradle verification command
2 parents 8102e5c + 71e2420 commit 26afe97

File tree

10 files changed

+214
-14
lines changed

10 files changed

+214
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
bin/
22
.DS_Store
3+
bitrise-build-cache-cli
34
.bitrise.secrets.yml
45
dist/
56
_tmp/
67
.idea/*
78
!.idea/codeStyles
89
.idea/codeStyles/*
910
!.idea/codeStyles/Project.xml
11+
gradle_verification_sample/gradle/verification-metadata.xml

.goreleaser.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ release:
1414
### Gradle verification
1515
For reference, the verification metadata is available as an attached assset.
1616
extra_files:
17-
- glob: ./gradle_verification_sample/gradle/verification-metadata.xml
17+
- glob: ./cmd/asset/verification-metadata.xml
1818

1919

2020
before:

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ When `enable-for gradle` or `enable-for bazel` is called:
9494

9595
## Release process
9696

97-
### 1: Create release
97+
### 1: Update Gradle verification reference file
98+
99+
Run the `bitrise test` or `bitrise run check_gradle_verification` command locally, and commit changes (workflows will fail if there are any) to the repository. In case the CI workflow is still failing, copy the generated metadata from the `Generate Gradle verification reference` Step's log.
100+
101+
### 2: Create release
98102

99103
To release a new version we use [goreleaser](https://github.com/goreleaser/goreleaser).
100104

@@ -121,7 +125,8 @@ This way you can test the new version by specifying it for the installer script,
121125
looks good you can edit the release and change it to `Set as the latest release`,
122126
so it'll be the version downloaded by the installer when the installer is called without a specified version.
123127

124-
### 2: Update downloader
128+
129+
### 3: Update downloader
125130

126131
Now that the new release is available run `godownloader` to update the
127132
installer script.
@@ -136,7 +141,7 @@ godownloader .goreleaser.yaml > ./install/installer.sh
136141
If only the timestamp is changed at the top of `./install/installer.sh` you don't have to commit
137142
the change, just discard it. If something else also changes then commit and push the updated `installer.sh`.
138143

139-
### 3: Update depending repos (steps, ...)
144+
### 4: Update depending repos (steps, ...)
140145

141146
The Bitrise Build Cache CLI is platform independent, but we have platform specific "adapters".
142147
These adapters depend on the Bitrise Build Cache CLI, either by importing it as a Go

bitrise.yml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,32 +118,36 @@ step_bundles:
118118
go build -o /tmp/bin/bitrise-build-cache-cli
119119
120120
SAMPLE_PATH=$(realpath ./gradle_verification_sample)
121+
GRADLE_VERIFICATION_REF_PATH=$SAMPLE_PATH/gradle/verification-metadata.xml
122+
GRADLE_VERIFICATION_REF_TARGET_PATH=$(realpath ./cmd/asset/verification-metadata.xml)
121123
122124
# add plugin with CLI
123125
cd "$SAMPLE_PATH"
124-
/tmp/bin/bitrise-build-cache-cli add-gradle-deps
126+
/tmp/bin/bitrise-build-cache-cli gradle-verification add-reference-deps
125127
126128
# generate xml
127129
rm -f "$SAMPLE_PATH/gradle/verification-metadata.xml"
128130
./gradlew --write-verification-metadata sha256 help
129131
130-
GRADLE_VERIFICATION_REF_PATH=$SAMPLE_PATH/gradle/verification-metadata.xml
131132
cat $GRADLE_VERIFICATION_REF_PATH
132133
echo "Generated verification xml: $GRADLE_VERIFICATION_REF_PATH"
133-
envman add --key GRADLE_VERIFICATION_REF_PATH --value "$GRADLE_VERIFICATION_REF_PATH"
134+
135+
# Copy metadata to be included in the CLI binary
136+
mv -f "$GRADLE_VERIFICATION_REF_PATH" "$GRADLE_VERIFICATION_REF_TARGET_PATH"
137+
envman add --key GRADLE_VERIFICATION_REF_TARGET_PATH --value "$GRADLE_VERIFICATION_REF_TARGET_PATH"
134138
- script@1:
135139
title: Check if local updates to verification-metadata.xml are not commited
136140
inputs:
137141
- content: |-
138142
set -x
139143
140-
git update-index --refresh | grep "verification-metadata.xml"
144+
git update-index --refresh | grep "cmd/asset/verification-metadata.xml"
141145
if [[ $? == 0 ]]; then
142146
echo "Please manually commit the changes to the repo:"
143-
echo "$GRADLE_VERIFICATION_REF_PATH"
147+
echo "$GRADLE_VERIFICATION_REF_TARGET_PATH"
144148
exit 1
145149
else
146-
echo "The files have no unstaged changes: $GRADLE_VERIFICATION_REF_PATH"
150+
echo "The files have no unstaged changes: $GRADLE_VERIFICATION_REF_TARGET_PATH"
147151
exit 0
148152
fi
149153

cmd/addGradleDeps.go renamed to cmd/gradleVerificationAddDeps.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"github.com/spf13/cobra"
1212
)
1313

14-
// addGradleDeps represents the gradle command
15-
var addGradleDeps = &cobra.Command{ //nolint:gochecknoglobals
16-
Use: "add-gradle-deps",
14+
// addGradleVerificationReferenceDeps represents the gradle command
15+
var addGradleVerificationReferenceDeps = &cobra.Command{ //nolint:gochecknoglobals
16+
Use: "add-reference-deps",
1717
Short: "Add Bitrise Build Cache plugins to the project (but do not enable it)",
1818
Long: `Add Bitrise Build Cache plugins to the project (but do not enable it)
1919
This command will:
@@ -44,7 +44,7 @@ This command will:
4444
}
4545

4646
func init() {
47-
rootCmd.AddCommand(addGradleDeps)
47+
gradleVerification.AddCommand(addGradleVerificationReferenceDeps)
4848
}
4949

5050
func addGradlePluginsFn(logger log.Logger, gradleHomePath string, envProvider func(string) string) error {

cmd/gradleVerificationWrite.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
_ "embed"
6+
"fmt"
7+
"io"
8+
"os"
9+
10+
"github.com/beevik/etree"
11+
"github.com/bitrise-io/go-utils/v2/log"
12+
"github.com/bitrise-io/go-utils/v2/pathutil"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
//go:embed "asset/verification-metadata.xml"
17+
var referenceVerificationDepsContent string
18+
19+
//nolint:gochecknoglobals
20+
var metadataPath string
21+
22+
// gradleVerification ...
23+
var gradleVerification = &cobra.Command{ //nolint:gochecknoglobals
24+
Use: "gradle-verification",
25+
Short: "Bitrise build cache support for projects using Gradle verification",
26+
Long: `Bitrise build cache support for projects using Gradle verification
27+
See https://docs.gradle.org/current/userguide/dependency_verification.html for more information.`,
28+
SilenceUsage: true,
29+
RunE: func(_ *cobra.Command, _ []string) error {
30+
return nil
31+
},
32+
}
33+
34+
var writeGradleVerificationDeps = &cobra.Command{ //nolint:gochecknoglobals
35+
Use: "write",
36+
Short: "Add Bitrise build cache dependencies to verification-metadata.xml",
37+
Long: `Add Bitrise build cache dependencies to verification-metadata.xml
38+
Missing dependencies of Bitrise build cache are appended.
39+
`,
40+
SilenceUsage: true,
41+
RunE: func(_ *cobra.Command, _ []string) error {
42+
logger := log.NewLogger()
43+
44+
logger.TInfof("Adding Gradle verification dependencies")
45+
46+
projectMetadataPath, err := pathutil.NewPathModifier().AbsPath(metadataPath)
47+
if err != nil {
48+
return fmt.Errorf("expand metadata path (%s): %w", metadataPath, err)
49+
}
50+
51+
if err := addGradleVerification(logger, projectMetadataPath, os.Getenv); err != nil {
52+
return fmt.Errorf("adding Gradle verification: %w", err)
53+
}
54+
55+
logger.TInfof("✅ Updated verification-metadata.xml")
56+
57+
return nil
58+
},
59+
}
60+
61+
func init() {
62+
rootCmd.AddCommand(gradleVerification)
63+
gradleVerification.AddCommand(writeGradleVerificationDeps)
64+
writeGradleVerificationDeps.Flags().StringVar(&metadataPath, "metadata-path", "", "Path of verification-metadata.xml")
65+
}
66+
67+
func addGradleVerification(logger log.Logger, projectMetadataPath string, _ func(string) string) error {
68+
logger.Infof("(i) Checking parameters")
69+
logger.Infof("(i) Metadata path: %s", projectMetadataPath)
70+
71+
referenceDepsReader := bytes.NewBufferString(referenceVerificationDepsContent)
72+
projectDepsReader, err := os.Open(projectMetadataPath)
73+
if err != nil {
74+
return fmt.Errorf("failed to open project verification-metadata.xml: %w", err)
75+
}
76+
77+
resultProjectMetadata, err := writeVerificationDeps(logger, referenceDepsReader, projectDepsReader)
78+
if err != nil {
79+
return err
80+
}
81+
82+
if err = os.WriteFile(projectMetadataPath, []byte(resultProjectMetadata), os.ModeAppend); err != nil {
83+
return fmt.Errorf("failed to write project verification-metadata.xml: %w", err)
84+
}
85+
86+
logger.Debugf("Updated contents: %s", resultProjectMetadata)
87+
88+
return nil
89+
}
90+
91+
func writeVerificationDeps(logger log.Logger, referenceDepsReader io.Reader, projectDepsReader io.Reader) (string, error) {
92+
referenceDeps := etree.NewDocument()
93+
if _, err := referenceDeps.ReadFrom(referenceDepsReader); err != nil {
94+
return "", fmt.Errorf("failed to parse reference verification-metadata.xml: %w", err)
95+
}
96+
97+
projectDeps := etree.NewDocument()
98+
if _, err := projectDeps.ReadFrom(projectDepsReader); err != nil {
99+
return "", fmt.Errorf("failed to parse project verification-metadata.xml: %w", err)
100+
}
101+
102+
projectRoot := projectDeps.SelectElement("verification-metadata")
103+
if projectRoot == nil {
104+
return "", fmt.Errorf("project metadata missing verification-metadata")
105+
}
106+
107+
projectComponents := projectRoot.SelectElement("components")
108+
if projectComponents == nil {
109+
return "", fmt.Errorf("project metadata missing components")
110+
}
111+
112+
referenceRoot := referenceDeps.SelectElement("verification-metadata")
113+
if referenceRoot == nil {
114+
return "", fmt.Errorf("reference metadata missing verification-metadata")
115+
}
116+
117+
referenceComponents := referenceRoot.SelectElement("components")
118+
if referenceComponents == nil {
119+
return "", fmt.Errorf("reference metadata missing components")
120+
}
121+
122+
referenceComponentList := referenceComponents.SelectElements("component")
123+
if referenceComponentList == nil {
124+
return "", fmt.Errorf("reference metadata has no components")
125+
}
126+
127+
for _, e := range referenceComponentList {
128+
projectComponents.AddChild(e)
129+
}
130+
131+
logger.Infof("Added %d dependecies to verification-metadata.xml", len(referenceComponentList))
132+
133+
result, err := projectDeps.WriteToString()
134+
if err != nil {
135+
return "", fmt.Errorf("failed to serialize updated verification-metadata.xml: %w", err)
136+
}
137+
138+
return result, nil
139+
}

e2e/bitrise.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,53 @@ workflows:
5959
- module: app
6060
- variant: debug
6161

62+
test_gradle_verification_gradle7:
63+
envs:
64+
- TEST_APP_URL: https://github.com/bitrise-io/android-demo-app
65+
- BRANCH: main
66+
after_run:
67+
- _setup
68+
- _test_gradle_verification
69+
70+
test_gradle_verification_gradle8:
71+
envs:
72+
- TEST_APP_URL: https://github.com/bitrise-io/android-demo-app
73+
- BRANCH: gradle8
74+
after_run:
75+
- _setup
76+
- _test_gradle_verification
77+
78+
_test_gradle_verification:
79+
steps:
80+
- script:
81+
title: build CLI
82+
inputs:
83+
- content: |-
84+
set -ex
85+
go build -o /tmp/bin/bitrise-build-cache-cli
86+
- change-workdir:
87+
title: Switch working dir to _tmp
88+
inputs:
89+
- path: ./_tmp
90+
- script:
91+
title: update metadata
92+
inputs:
93+
- content: |-
94+
set -ex
95+
96+
if [[ -f ~/.gradle/init.d/bitrise-build-cache.init.gradle.kts ]]; then
97+
rm ~/.gradle/init.d/bitrise-build-cache.init.gradle.kts
98+
fi
99+
./gradlew assemble --write-verification-metadata=sha256
100+
101+
METADATA_PATH=./gradle/verification-metadata.xml
102+
103+
/tmp/bin/bitrise-build-cache-cli gradle-verification add-reference-deps
104+
/tmp/bin/bitrise-build-cache-cli gradle-verification write --metadata-path=$METADATA_PATH
105+
106+
./gradlew assemble
107+
108+
62109
_setup:
63110
steps:
64111
- script:

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22.3
55
require (
66
cloud.google.com/go/longrunning v0.6.0
77
github.com/bazelbuild/remote-apis v0.0.0-20240703191324-0d21f29acdb9
8+
github.com/beevik/etree v1.4.1
89
github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.33
910
github.com/bitrise-io/go-utils v1.0.13
1011
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.23

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ cloud.google.com/go/longrunning v0.6.0/go.mod h1:uHzSZqW89h7/pasCWNYdUpwGz3PcVWh
44
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
55
github.com/bazelbuild/remote-apis v0.0.0-20240703191324-0d21f29acdb9 h1:Ma3IQbZmIVd8ROR7TZNqN8YnSYKXWtW5t5uUc35pftI=
66
github.com/bazelbuild/remote-apis v0.0.0-20240703191324-0d21f29acdb9/go.mod h1:ry8Y6CkQqCVcYsjPOlLXDX2iRVjOnjogdNwhvHmRcz8=
7+
github.com/beevik/etree v1.4.1 h1:PmQJDDYahBGNKDcpdX8uPy1xRCwoCGVUiW669MEirVI=
8+
github.com/beevik/etree v1.4.1/go.mod h1:gPNJNaBGVZ9AwsidazFZyygnd+0pAU38N4D+WemwKNs=
79
github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.33 h1:BxTsbaY9hwC/3a2y4V8vHlKAu80DAEuv/rvEEAvaq8o=
810
github.com/bitrise-io/go-steputils/v2 v2.0.0-alpha.33/go.mod h1:Wu1/fiQqxr7A7a9aphh/Jxpof6WV+LEO6vGfBQ9T99A=
911
github.com/bitrise-io/go-utils v1.0.13 h1:1QENhTS/JlKH9F7+/nB+TtbTcor6jGrE6cQ4CJWfp5U=

0 commit comments

Comments
 (0)