Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/end-to-end.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ step-runner-config:

unit-test:
- implementer: NpmTest
- implementer: GradleTest

package:
- implementer: NpmPackage
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ license_files =

[pylint]
ignore = version.py
disable = R0801,R0903
disable = R0801,R0903,W0246,R1735,W0718,R1734,R1735,W0718,C0209,too-many-positional-arguments
max-line-length = 140
output-format = colorized

[tool:pytest]
Expand Down Expand Up @@ -61,3 +62,4 @@ tests =
mock
codecov
tox

14 changes: 13 additions & 1 deletion src/ploigos_step_runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,15 @@
# Required.
# Id to the artifact repository to push the artifact to.
maven-push-artifact-repo-id: ''
- implementer: GradelDeploy
config: {
# Required.
# URL to the artifact repository to push the artifact to.
# maven-push-artifact-repo-url: ''

# Required.
# Id to the artifact repository to push the artifact to.
# maven-push-artifact-repo-id: ''

}

Expand Down Expand Up @@ -733,7 +742,10 @@
# npm-envs:
# ENV_VAR1: VALUE1
# ENV_VAR2: VALUE2


- implementer: GradleTest
config: {}

push-artifacts:
# WARNING: not yet implemented
- implementer: NPM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ploigos_step_runner.step_implementers.generate_metadata.dotnet_generate_metadata import \
DotnetGenerateMetadata
from ploigos_step_runner.step_implementers.generate_metadata.git import Git
from ploigos_step_runner.step_implementers.generate_metadata.gradle import Gradle
from ploigos_step_runner.step_implementers.generate_metadata.jenkins import \
Jenkins
from ploigos_step_runner.step_implementers.generate_metadata.maven import Maven
Expand Down
126 changes: 126 additions & 0 deletions src/ploigos_step_runner/step_implementers/generate_metadata/gradle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
"""`StepImplementer` for the `generate-metadata` step using Gradle.

Step Configuration
------------------
Step configuration expected as input to this step.
Could come from:

* static configuration
* runtime configuration
* previous step results

Configuration Key | Required? | Default | Description
-------------------------------------|-----------|------------------|-----------
`build-file` | Yes | `'build.gradle'` | The build file to read the app version out of

Result Artifacts
----------------
Results artifacts output by this step.

Result Artifact Key | Description
----------------------------------------|------------
`app-version` | Value to use for `version` portion of semantic version \
(https://semver.org/). Uses the version read out of the given build.gradle file.

"""# pylint: disable=line-too-long

from ploigos_step_runner.results import StepResult
# from ploigos_step_runner.exceptions import StepRunnerException
from ploigos_step_runner.step_implementers.shared import GradleGeneric

from ploigos_step_runner.utils.gradle import GradleGroovyParser, GradleGroovyParserException

DEFAULT_CONFIG = {
'build-file': 'app/build.gradle',
}

REQUIRED_CONFIG_OR_PREVIOUS_STEP_RESULT_ARTIFACT_KEYS = [
'build-file'
]


class Gradle(GradleGeneric):
"""`StepImplementer` for the `generate-metadata` step using Gradle.
"""

@staticmethod
def step_implementer_config_defaults():
"""Getter for the StepImplementer's configuration defaults.

Returns
-------
dict
Default values to use for step configuration values.

Notes
-----
These are the lowest precedence configuration values.
"""
return {**GradleGeneric.step_implementer_config_defaults(), **DEFAULT_CONFIG}

@staticmethod
def _required_config_or_result_keys():
"""Getter for step configuration or previous step result artifacts that are required before
running this step.

See Also
--------
_validate_required_config_or_previous_step_result_artifact_keys

Returns
-------
array_list
Array of configuration keys or previous step result artifacts
that are required before running the step.
"""
return REQUIRED_CONFIG_OR_PREVIOUS_STEP_RESULT_ARTIFACT_KEYS

def _validate_required_config_or_previous_step_result_artifact_keys(self):
"""Validates that the required configuration keys or previous step result artifacts
are set and have valid values.

Validates that:
* required configuration is given
* given 'build.gradle' exists

Raises
------
AssertionError
If step configuration or previous step result artifacts have invalid required values
"""
super()._validate_required_config_or_previous_step_result_artifact_keys()

def _run_step(self):

"""Runs the step implemented by this StepImplementer.

Returns
-------
StepResult
Object containing the dictionary results of this step.
"""

step_result = StepResult.from_step_implementer(self)

build_file_path = self.get_value('build-file')

groovy_parser = GradleGroovyParser(build_file_path)

# get the version

try:

project_version = groovy_parser.get_version()

if project_version:
step_result.add_artifact(
name='app-version',
value=project_version
)

except GradleGroovyParserException:
step_result.success = False
step_result.message = f'Could not get project version from given build file' \
f' ({build_file_path})'

return step_result
2 changes: 2 additions & 0 deletions src/ploigos_step_runner/step_implementers/push_artifacts/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# export USER_NAME=momot-svc-acct
# export USER_PASSWORD=7VJK520gtRE7Gu8
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from ploigos_step_runner.step_implementers.push_artifacts.maven_deploy import MavenDeploy
from ploigos_step_runner.step_implementers.push_artifacts.maven import Maven
from ploigos_step_runner.step_implementers.push_artifacts.npm_push_artifacts import NpmPushArtifacts
from ploigos_step_runner.step_implementers.push_artifacts.gradle_deploy import GradleDeploy
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@

"""PSR Step for pushing artifact with Gradle to artifactory"""
import os
from ploigos_step_runner.exceptions import StepRunnerException
from ploigos_step_runner.results.step_result import StepResult
from ploigos_step_runner.step_implementers.shared.gradle_generic import GradleGeneric

DEFAULT_CONFIG = {
"build-file": "app/build.gradle",
}

REQUIRED_CONFIG_OR_PREVIOUS_STEP_RESULT_ARTIFACT_KEYS = [
"build-file",
"gradle-token",
"gradle-token-alpha",
]


class GradleDeploy(GradleGeneric):
"""`StepImplementer` for the `uat` step using Gradle by invoking the 'test` gradle phase."""

def __init__(
self, workflow_result, parent_work_dir_path, config, environment=None, gradle_tasks=None
): # pylint: disable=too-many-arguments

super().__init__(
workflow_result=workflow_result,
parent_work_dir_path=parent_work_dir_path,
config=config,
environment=environment,
gradle_tasks=["artifactoryPublish"],
)

print('GradleDeploy: ')
print(f"environment : {environment}")
print(f"config : {config}")
print(f"working_dir : {parent_work_dir_path}")
print(f"gradle_tasks : {gradle_tasks}")

@staticmethod
def step_implementer_config_defaults():
"""Getter for the StepImplementer's configuration defaults.

Returns
-------
dict
Default values to use for step configuration values.

Notes
-----
These are the lowest precedence configuration values.
"""
return {**GradleGeneric.step_implementer_config_defaults(), **DEFAULT_CONFIG}

@staticmethod
def _required_config_or_result_keys():
"""Getter for step configuration or previous step result artifacts that are required before
running this step.

See Also
--------
_validate_required_config_or_previous_step_result_artifact_keys

Returns
-------
array_list
Array of configuration keys or previous step result artifacts
that are required before running the step.
"""
return REQUIRED_CONFIG_OR_PREVIOUS_STEP_RESULT_ARTIFACT_KEYS

def read_and_replace_password(self, app_dir):

"""Read a properties file, replace the Artifactory password, and save the changes."""
properties = {}

print('Updating Password for Gradle Deploy.')

print('Application Dirctory: ' + str(app_dir))

properties_file = os.path.join(app_dir, 'gradle.properties')

if not os.path.exists(properties_file):

properties_contents = 'version=1.0\nartifactory_user=user\nartifactory_password=empty\n'

with open(properties_file, 'w', encoding='utf-8') as outf:
outf.write(properties_contents)
outf.close()

artifactory_password = self.get_value("gradle-token-alpha")

# # Read the properties file

with open(properties_file, "r", encoding="utf8") as file:
for line in file:
# Skip comments and empty lines
line = line.strip()
if line and not line.startswith("#"):
key, value = line.split("=", 1) # Split on the first '='
properties[key] = value

# Replace the Artifactory password value
if "artifactory_password" in properties:
properties["artifactory_password"] = artifactory_password

with open(properties_file, "w", encoding="utf8") as file:
for key, value in properties.items():
file.write(f"{key}={value}\n")

# print out the properties file

with open(properties_file, "r", encoding="utf8") as file:
content = file.read()
print("\n build.properties file: ")
print(content)

def _run_step(self):
"""Runs the step implemented by this StepImplementer.

Returns
-------
StepResult
Object containing the dictionary results of this step.
"""

parent_work_dir_path = super().work_dir_path

print('Work Directory Path: ' + parent_work_dir_path)

build_fn = self.get_value("build-file")

print('Build File: ' + build_fn)

app_dir = os.path.dirname(build_fn)

print('Updating gradle.properties file.')

self.read_and_replace_password(os.path.join(parent_work_dir_path, app_dir))

step_result = StepResult.from_step_implementer(self)

# push the artifacts
gradle_output_file_path = self.write_working_file("gradle_deploy_output.txt")

try:
# execute Gradle Artifactory publish step (params come from config)
print("Push packaged gradle artifacts")

self._run_gradle_step(gradle_output_file_path=gradle_output_file_path)

except StepRunnerException as error:
step_result.success = False
step_result.message = (
"Error running 'gradle deploy' to push artifacts. "
f"More details maybe found in 'gradle-output' report artifact: {error}"
)
step_result.message = f"environment : {self.environment}"
step_result.message = f"config : {self.config}"

finally:
step_result.add_artifact(
description="Standard out and standard error from running gradle to "
"push artifacts to repository.",
name="gradle-push-artifacts-output",
value=gradle_output_file_path,
)

return step_result
3 changes: 3 additions & 0 deletions src/ploigos_step_runner/step_implementers/shared/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from ploigos_step_runner.step_implementers.shared.container_deploy_mixin import \
ContainerDeployMixin
from ploigos_step_runner.step_implementers.shared.git_mixin import GitMixin
from ploigos_step_runner.step_implementers.shared.gradle_generic import GradleGeneric
from ploigos_step_runner.step_implementers.shared.gradle_test_reporting_mixin import \
GradleTestReportingMixin
from ploigos_step_runner.step_implementers.shared.maven_generic import \
MavenGeneric
from ploigos_step_runner.step_implementers.shared.maven_test_reporting_mixin import \
Expand Down
Loading
Loading