diff --git a/jenkins_jobs/modules/publishers.py b/jenkins_jobs/modules/publishers.py index 2392e5483..6a8fbf96d 100644 --- a/jenkins_jobs/modules/publishers.py +++ b/jenkins_jobs/modules/publishers.py @@ -3533,6 +3533,79 @@ def artifact_deployer(parser, xml_parent, data): XML.SubElement(deployer, 'deployEvenBuildFail').text = deploy_if_fail +def s3(parser, xml_parent, data): + """yaml: s3 + Upload build artifacts to Amazon S3. + + Requires the Jenkins `S3 plugin. + `_ + + :arg str s3-profile: Globally-defined S3 profile to use + :arg list entries: + :entries: + * **destination-bucket** (`str`) - Destination S3 bucket + * **source-files** (`str`) - Source files (Ant glob syntax) + * **storage-class** (`str`) - S3 storage class; one of "STANDARD" + or "REDUCED_REDUNDANCY" + * **bucket-region** (`str`) - S3 bucket region (capitalized with + underscores) + * **upload-on-failure** (`bool`) - Upload files even if the build + failed (Default: False) + * **upload-from-slave** (`bool`) - Perform the upload directly from + the Jenkins slave rather than the master node. (Default: False) + * **managed-artifacts** (`bool`) - Let Jenkins fully manage the + published artifacts, similar to when artifacts are published to + the Jenkins master. (Default: False) + :arg list metadata-tags: + :metadata-tags: + * **key** Metadata key for files from this build. It will be + prefixed by "x-amz-meta-" when uploaded to S3. Can contain macros + (e.g. environment variables). + * **value** Metadata value associated with the key. Can contain macros. + + Example: + + .. literalinclude:: /../../tests/publishers/fixtures/s3001.yaml + :language: yaml + """ + deployer = XML.SubElement(xml_parent, + 'hudson.plugins.s3.S3BucketPublisher') + if data is None or not data.get('entries'): + raise JenkinsJobsException('No filesets defined.') + + XML.SubElement(deployer, 'profileName').text = data.get('s3-profile') + + entries = XML.SubElement(deployer, 'entries') + + for entry in data.get('entries'): + fileset = XML.SubElement(entries, 'hudson.plugins.s3.Entry') + + # xml keys -> yaml keys + settings = [('bucket', 'destination-bucket', ''), + ('sourceFile', 'source-files', ''), + ('storageClass', 'storage-class', ''), + ('selectedRegion', 'bucket-region', ''), + ('noUploadOnFailure', 'upload-on-failure', False), + ('uploadFromSlave', 'upload-from-slave', False), + ('managedArtifacts', 'managed-artifacts', False)] + + for xml_key, yaml_key, default in settings: + xml_config = XML.SubElement(fileset, xml_key) + config_value = entry.get(yaml_key, default) + if xml_key == 'noUploadOnFailure': + xml_config.text = str(not config_value).lower() + elif isinstance(default, bool): + xml_config.text = str(config_value).lower() + else: + xml_config.text = str(config_value) + + metadata = XML.SubElement(deployer, 'userMetadata') + for tag in data.get('metadata-tags', []): + pair = XML.SubElement(metadata, 'hudson.plugins.s3.MetadataPair') + XML.SubElement(pair, 'key').text = tag.get('key') + XML.SubElement(pair, 'value').text = tag.get('value') + + def ruby_metrics(parser, xml_parent, data): """yaml: ruby-metrics Rcov plugin parses rcov html report files and diff --git a/setup.cfg b/setup.cfg index 58f5290c0..c5fb1f4bf 100644 --- a/setup.cfg +++ b/setup.cfg @@ -155,6 +155,7 @@ jenkins_jobs.publishers = rich-text-publisher=jenkins_jobs.modules.publishers:rich_text_publisher robot=jenkins_jobs.modules.publishers:robot ruby-metrics=jenkins_jobs.modules.publishers:ruby_metrics + s3=jenkins_jobs.modules.publishers:s3 scp=jenkins_jobs.modules.publishers:scp sitemonitor=jenkins_jobs.modules.publishers:sitemonitor sloccount=jenkins_jobs.modules.publishers:sloccount diff --git a/tests/publishers/fixtures/s3001.xml b/tests/publishers/fixtures/s3001.xml new file mode 100644 index 000000000..a3f29d98f --- /dev/null +++ b/tests/publishers/fixtures/s3001.xml @@ -0,0 +1,29 @@ + + + + + banana + + + herp-derp + bargle_${BUILD_ID}.tgz + STANDARD + US_EAST_1 + true + true + true + + + + + warbl ${garbl} + herp derp weevils + + + hurrdurr + wharrgarbl blee ${FANCY_VARIABLE} + + + + + diff --git a/tests/publishers/fixtures/s3001.yaml b/tests/publishers/fixtures/s3001.yaml new file mode 100644 index 000000000..76772a815 --- /dev/null +++ b/tests/publishers/fixtures/s3001.yaml @@ -0,0 +1,16 @@ +publishers: + - s3: + s3-profile: banana + entries: + - destination-bucket: herp-derp + source-files: 'bargle_${BUILD_ID}.tgz' + storage-class: STANDARD + bucket-region: US_EAST_1 + upload-on-failure: false + upload-from-slave: true + managed-artifacts: true + metadata-tags: + - key: warbl ${garbl} + value: herp derp weevils + - key: hurrdurr + value: wharrgarbl blee ${FANCY_VARIABLE}