-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
artifact.ts
148 lines (127 loc) · 4.38 KB
/
artifact.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import * as s3 from '@aws-cdk/aws-s3';
import { Lazy, Token } from '@aws-cdk/core';
import * as validation from './private/validation';
/**
* An output artifact of an action. Artifacts can be used as input by some actions.
*/
export class Artifact {
/**
* A static factory method used to create instances of the Artifact class.
* Mainly meant to be used from `decdk`.
*
* @param name the (required) name of the Artifact
*/
public static artifact(name: string): Artifact {
return new Artifact(name);
}
private _artifactName?: string;
private readonly metadata: { [key: string]: any } = {};
constructor(artifactName?: string) {
validation.validateArtifactName(artifactName);
this._artifactName = artifactName;
}
public get artifactName(): string | undefined {
return this._artifactName;
}
/**
* Returns an ArtifactPath for a file within this artifact.
* CfnOutput is in the form "<artifact-name>::<file-name>"
* @param fileName The name of the file
*/
public atPath(fileName: string): ArtifactPath {
return new ArtifactPath(this, fileName);
}
/**
* The artifact attribute for the name of the S3 bucket where the artifact is stored.
*/
public get bucketName() {
return artifactAttribute(this, 'BucketName');
}
/**
* The artifact attribute for The name of the .zip file that contains the artifact that is
* generated by AWS CodePipeline, such as 1ABCyZZ.zip.
*/
public get objectKey() {
return artifactAttribute(this, 'ObjectKey');
}
/**
* The artifact attribute of the Amazon Simple Storage Service (Amazon S3) URL of the artifact,
* such as https://s3-us-west-2.amazonaws.com/artifactstorebucket-yivczw8jma0c/test/TemplateSo/1ABCyZZ.zip.
*/
public get url() {
return artifactAttribute(this, 'URL');
}
/**
* Returns a token for a value inside a JSON file within this artifact.
* @param jsonFile The JSON file name.
* @param keyName The hash key.
*/
public getParam(jsonFile: string, keyName: string) {
return artifactGetParam(this, jsonFile, keyName);
}
/**
* Returns the location of the .zip file in S3 that this Artifact represents.
* Used by Lambda's `CfnParametersCode` when being deployed in a CodePipeline.
*/
public get s3Location(): s3.Location {
return {
bucketName: this.bucketName,
objectKey: this.objectKey,
};
}
/**
* Add arbitrary extra payload to the artifact under a given key.
* This can be used by CodePipeline actions to communicate data between themselves.
* If metadata was already present under the given key,
* it will be overwritten with the new value.
*/
public setMetadata(key: string, value: any): void {
this.metadata[key] = value;
}
/**
* Retrieve the metadata stored in this artifact under the given key.
* If there is no metadata stored under the given key,
* null will be returned.
*/
public getMetadata(key: string): any {
return this.metadata[key];
}
public toString() {
return this.artifactName;
}
/** @internal */
protected _setName(name: string) {
if (this._artifactName) {
throw new Error(`Artifact already has name '${this._artifactName}', cannot override it`);
} else {
this._artifactName = name;
}
}
}
/**
* A specific file within an output artifact.
*
* The most common use case for this is specifying the template file
* for a CloudFormation action.
*/
export class ArtifactPath {
public static artifactPath(artifactName: string, fileName: string): ArtifactPath {
return new ArtifactPath(Artifact.artifact(artifactName), fileName);
}
constructor(readonly artifact: Artifact, readonly fileName: string) {
}
public get location() {
const artifactName = this.artifact.artifactName
? this.artifact.artifactName
: Lazy.string({ produce: () => this.artifact.artifactName });
return `${artifactName}::${this.fileName}`;
}
}
function artifactAttribute(artifact: Artifact, attributeName: string) {
const lazyArtifactName = Lazy.string({ produce: () => artifact.artifactName });
return Token.asString({ 'Fn::GetArtifactAtt': [lazyArtifactName, attributeName] });
}
function artifactGetParam(artifact: Artifact, jsonFile: string, keyName: string) {
const lazyArtifactName = Lazy.string({ produce: () => artifact.artifactName });
return Token.asString({ 'Fn::GetParam': [lazyArtifactName, jsonFile, keyName] });
}