Skip to content

Commit

Permalink
[semver:minor] Merge pull request #44 from ricardo-ch/maven_verify_ve…
Browse files Browse the repository at this point in the history
…rsion_pattern

wip: maven_assert_expression
  • Loading branch information
cesarfm authored Oct 7, 2022
2 parents a36106e + 38121cf commit 267603e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/commands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,28 @@ steps:
#...
```

### Command to assert that a Maven expression matches a pattern (for Java applications)

**Name**: maven_assert_expression

**Parameters**:
- **expression**: An expression to evaluate, following the Maven expression syntax but without any surrounding ${...}.
- **pattern**: The regex pattern to match against the expression.
- **pom_file**: The pom file to use. Default: "pom.xml"

Example:

Assert that the project version finishes with '-SNAPSHOT'

```yaml
#...
steps:
- ric-orb/maven_assert_expression:
expression: 'project.version'
pattern: '^.*-SNAPSHOT$'
#...
```

## See:

- [Orb Author Intro](https://circleci.com/docs/2.0/orb-author-intro/#section=configuration)
Expand Down
28 changes: 28 additions & 0 deletions src/commands/maven_assert_expression.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
description: >
Checks that some expression evaluated from the Maven project matches with a certain pattern.
This job will succeed if the expression contains the pattern, and fail otherwise.
Thus it can be used to allow/block flows depending on the value of the expression.
parameters:
expression:
description: 'An expression to evaluate, following the Maven expression syntax but without any surrounding ${...}'
type: string
pattern:
description: 'The regex pattern to match against the expression'
type: string
pom_file:
description: 'The pom file to use'
type: string
default: 'pom.xml'

steps:
- run:
name: Verify that expression contains pattern
command: |
EXPR=<< parameters.expression >>
PATTERN=<< parameters.pattern >>
RESULT=$(./mvnw help:evaluate -Dexpression=$EXPR -q -DforceStdout -f << parameters.pom_file >>)
if [[ ! "$RESULT" =~ $PATTERN ]]; then
echo "The expression $EXPR=[$RESULT] does not match with the pattern $PATTERN"
exit 1
fi
20 changes: 20 additions & 0 deletions src/jobs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,26 @@ jobs:
cache_key_prefix: "myrepo"
```

### Assert that the project version is a SNAPSHOT (for Java applications)

**Name**: maven_assert_snapshot_version

Checks that the Maven project (of the provided pom file) has a SNAPSHOT version, otherwise it fails.
Typically used to force that branch builds of library modules have a -SNAPSHOT version when manually deployed.

**Parameters**:
- **pom_file**: The pom file to use. Default: "pom.xml"

Example:

```yaml
#...
jobs:
- ric-orb/maven_assert_snapshot_version:
context: dev
#...
```

### No-Op dummy job

**Name**: no_op
Expand Down
19 changes: 19 additions & 0 deletions src/jobs/maven_assert_snapshot_version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
description: >
Checks that the Maven project (of the provided pom file) has a SNAPSHOT version, otherwise it fails.
Typically used to force that branch builds of library modules have a -SNAPSHOT version when manually deployed.
parameters:
pom_file:
description: 'The pom file to use'
type: string
default: 'pom.xml'

executor:
name: 'maven_docker'

steps:
- checkout
- maven_assert_expression:
expression: 'project.version'
pattern: '^.*-SNAPSHOT$'
pom_file: << parameters.pom_file >>

0 comments on commit 267603e

Please sign in to comment.