Skip to content

Commit 6e5e888

Browse files
authored
ci: Add simplified examples and a workflow to run them on PRs (#95)
* add simplified examples and a workflow to run them on PRs * fix gradle build * update to new DevCycle prefix * update variable names to remove dvc
1 parent cb69122 commit 6e5e888

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Test Examples
2+
3+
on:
4+
pull_request:
5+
branches: [ main ]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: actions/setup-java@v3
13+
with:
14+
distribution: temurin
15+
java-version: 11
16+
- name: Setup Gradle
17+
uses: gradle/gradle-build-action@v2
18+
- name: Run local bucketing example
19+
run: ./gradlew runLocalExample
20+
env:
21+
DEVCYCLE_SERVER_SDK_KEY: "${{ secrets.DEVCYCLE_SERVER_SDK_KEY }}"
22+
- name: Run cloud bucketing example
23+
run: ./gradlew runCloudExample
24+
env:
25+
DEVCYCLE_SERVER_SDK_KEY: "${{ secrets.DEVCYCLE_SERVER_SDK_KEY }}"

build.gradle

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,29 @@ dependencies {
169169
testCompileOnly("org.projectlombok:lombok:$lombok_version")
170170
}
171171

172+
// Gradle magic for adding a new "examples" source set that's separate from the main source set
173+
sourceSets {
174+
examples {
175+
java {
176+
compileClasspath += sourceSets.main.output
177+
runtimeClasspath += sourceSets.main.output
178+
}
179+
}
180+
}
181+
182+
configurations {
183+
examplesImplementation.extendsFrom implementation
184+
examplesRuntimeOnly.extendsFrom runtimeOnly
185+
}
186+
187+
task runLocalExample(type: JavaExec) {
188+
description = "Run the local bucketing example"
189+
classpath = sourceSets.examples.runtimeClasspath
190+
main = 'com.devcycle.examples.LocalExample'
191+
}
192+
193+
task runCloudExample(type: JavaExec) {
194+
description = "Run the cloud bucketing example"
195+
classpath = sourceSets.examples.runtimeClasspath
196+
main = 'com.devcycle.examples.CloudExample'
197+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.devcycle.examples;
2+
3+
import com.devcycle.sdk.server.cloud.api.DevCycleCloudClient;
4+
import com.devcycle.sdk.server.cloud.model.DevCycleCloudOptions;
5+
import com.devcycle.sdk.server.common.exception.DevCycleException;
6+
import com.devcycle.sdk.server.common.model.DevCycleUser;
7+
8+
public class CloudExample {
9+
public static String VARIABLE_KEY = "test-boolean-variable";
10+
11+
public static void main(String[] args) throws InterruptedException {
12+
String server_sdk_key = System.getenv("DEVCYCLE_SERVER_SDK_KEY");
13+
if (server_sdk_key == null) {
14+
System.err.println("Please set the DEVCYCLE_SERVER_SDK_KEY environment variable");
15+
System.exit(1);
16+
}
17+
18+
// Create user object
19+
DevCycleUser user = DevCycleUser.builder()
20+
.userId("SOME_USER_ID")
21+
.build();
22+
23+
// The default value can be of type string, boolean, number, or JSON
24+
Boolean defaultValue = false;
25+
26+
DevCycleCloudOptions options = DevCycleCloudOptions.builder().build();
27+
28+
// Initialize DevCycle Client
29+
DevCycleCloudClient client = new DevCycleCloudClient(server_sdk_key, options);
30+
31+
// Fetch variable values using the identifier key, with a default value and user
32+
// object
33+
// The default value can be of type string, boolean, number, or JSON
34+
Boolean variableValue = false;
35+
try {
36+
variableValue = client.variableValue(user, VARIABLE_KEY, defaultValue);
37+
} catch(DevCycleException e) {
38+
System.err.println("Error fetching variable value: " + e.getMessage());
39+
System.exit(1);
40+
}
41+
42+
// Use variable value
43+
if (variableValue) {
44+
System.err.println("feature is enabled");
45+
} else {
46+
System.err.println("feature is NOT enabled");
47+
}
48+
}
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.devcycle.examples;
2+
3+
import com.devcycle.sdk.server.local.api.DevCycleLocalClient;
4+
import com.devcycle.sdk.server.local.model.DevCycleLocalOptions;
5+
import com.devcycle.sdk.server.common.model.DevCycleUser;
6+
7+
public class LocalExample {
8+
public static String VARIABLE_KEY = "test-boolean-variable";
9+
10+
public static void main(String[] args) throws InterruptedException {
11+
String server_sdk_key = System.getenv("DEVCYCLE_SERVER_SDK_KEY");
12+
if (server_sdk_key == null) {
13+
System.err.println("Please set the DEVCYCLE_SERVER_SDK_KEY environment variable");
14+
System.exit(1);
15+
}
16+
17+
// Create user object
18+
DevCycleUser user = DevCycleUser.builder()
19+
.userId("SOME_USER_ID")
20+
.build();
21+
22+
// The default value can be of type string, boolean, number, or JSON
23+
Boolean defaultValue = false;
24+
25+
DevCycleLocalOptions options = DevCycleLocalOptions.builder().configPollingIntervalMs(60000)
26+
.disableAutomaticEventLogging(false).disableCustomEventLogging(false).build();
27+
28+
// Initialize DevCycle Client
29+
DevCycleLocalClient client = new DevCycleLocalClient(server_sdk_key, options);
30+
31+
for (int i = 0; i < 10; i++) {
32+
if(client.isInitialized()) {
33+
break;
34+
}
35+
Thread.sleep(500);
36+
}
37+
38+
// Fetch variable values using the identifier key, with a default value and user
39+
// object
40+
// The default value can be of type string, boolean, number, or JSON
41+
Boolean variableValue = client.variableValue(user, VARIABLE_KEY, defaultValue);
42+
43+
// Use variable value
44+
if (variableValue) {
45+
System.err.println("feature is enabled");
46+
} else {
47+
System.err.println("feature is NOT enabled");
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)