Skip to content

Commit 6f23123

Browse files
authored
Merge pull request #629 from reg-viz/feat/remove_create_bucket_on_prepare
feat(s3-plugin): Remove prepare option to create bucket
2 parents 509a8df + 4147b30 commit 6f23123

File tree

9 files changed

+22
-261
lines changed

9 files changed

+22
-261
lines changed

packages/reg-publish-s3-plugin/README.md

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,9 @@ npm i reg-publish-s3-plugin -D
99
reg-suit prepare -p publish-s3
1010
```
1111

12-
## AWS Credentials
12+
## Requirements
1313

14-
This plugin needs AWS credentials to access S3. You can set them by the following 2 methods.
15-
16-
### Environment values
17-
18-
```sh
19-
export AWS_ACCESS_KEY_ID=<your-access-key>
20-
export AWS_SECRET_ACCESS_KEY=<your-secret-key>
21-
```
22-
23-
### Create INI file
24-
25-
Create a file at `~/.aws/credentials` and edit it. For example:
26-
27-
```ini
28-
[default]
29-
aws_access_key_id = <your-access-key>
30-
aws_secret_access_key = <your-secret-key>
31-
```
14+
To use this plugin, you need to create an S3 bucket and configure to allow to access it from your CI.
3215

3316
## Configure
3417

@@ -40,7 +23,7 @@ aws_secret_access_key = <your-secret-key>
4023
sseKMSKeyId?: string;
4124
customDomain?: string;
4225
pathPrefix?: string;
43-
sdkOptions?: S3.Types.ClientConfiguration;
26+
sdkOptions?: S3ClientConfig;
4427
}
4528
```
4629

@@ -51,19 +34,19 @@ aws_secret_access_key = <your-secret-key>
5134
- `sseKMSKeyId` - _Optional_ - Specify server-side encryption KMS KEY ID. If provided, is passed as SSEKMSKeyId to s3.putObject.
5235
- `customDomain` - _Optional_ - Set if you have your domain and host S3 on it. If set, the HTML report will be published with this custom domain(e.g. `https://your-sub.example.com/...`).
5336
- `pathPrefix` - _Optional_ - Specify paths. For example if you set `some_dir`, the report is published with URL such as `https://your-backet-name.s3.amazonaws.com/some_dir/xxxxxxxxx/index.html`.
54-
- `sdkOptions` - _Optional_ - Specify SDK options to pass to the S3 client. For details about the options, refer to the [AWS JavaScript SDK docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor_details).
37+
- `sdkOptions` - _Optional_ - Specify options to pass to `S3Client` constructor. For details about the options, refer to the [AWS JavaScript SDK docs](https://www.npmjs.com/package/@aws-sdk/client-s3#usage).
5538

5639
## IAM Role Policy
5740

58-
This plugin needs follwings role policy.
41+
This plugin needs following role policy.
5942

6043
```
61-
"Action": [
62-
"s3:DeleteObject",
63-
"s3:GetObject",
64-
"s3:GetObjectAcl",
65-
"s3:PutObject",
66-
"s3:PutObjectAcl",
67-
"s3:ListBucket"
68-
]
44+
"Action": [
45+
"s3:DeleteObject",
46+
"s3:GetObject",
47+
"s3:GetObjectAcl",
48+
"s3:PutObject",
49+
"s3:PutObjectAcl",
50+
"s3:ListObject"
51+
]
6952
```

packages/reg-publish-s3-plugin/e2e/clean-up-buckets.sh

Lines changed: 0 additions & 7 deletions
This file was deleted.
Binary file not shown.
Binary file not shown.

packages/reg-publish-s3-plugin/e2e/report-fixture/index.html

Lines changed: 0 additions & 16 deletions
This file was deleted.

packages/reg-publish-s3-plugin/e2e/script.ts

Lines changed: 0 additions & 122 deletions
This file was deleted.

packages/reg-publish-s3-plugin/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
"main": "lib/index.js",
99
"scripts": {
1010
"prepublish": "tsc -p tsconfig.build.json",
11-
"clean:e2e": "rimraf e2e/report-fixture-expected",
12-
"test.bk": "npm run clean:e2e && ts-node e2e/script.ts",
1311
"test": "echo skip"
1412
},
1513
"keywords": [
Lines changed: 8 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,29 @@
1-
import { v4 as uuid } from "uuid";
2-
import { CreateBucketCommand, PutBucketPolicyCommand, S3Client } from "@aws-sdk/client-s3";
31
import { PluginPreparer, PluginCreateOptions, PluginLogger } from "reg-suit-interface";
42
import { PluginConfig } from "./s3-publisher-plugin";
53

64
export interface SetupInquireResult {
7-
createBucket: boolean;
8-
bucketName?: string;
5+
bucketName: string;
96
}
107

11-
function createPolicy(bucketName: string) {
12-
return {
13-
Version: "2012-10-17",
14-
Id: "Policy1498486961145",
15-
Statement: [
16-
{
17-
Sid: "Stmt1498486956732",
18-
Effect: "Allow",
19-
Principal: "*",
20-
Action: "s3:GetObject",
21-
Resource: `arn:aws:s3:::${bucketName}/*`,
22-
},
23-
],
24-
};
25-
}
26-
27-
const BUCKET_PREFIX = "reg-publish-bucket";
28-
298
export class S3BucketPreparer implements PluginPreparer<SetupInquireResult, PluginConfig> {
30-
private _s3client = new S3Client();
319
_logger!: PluginLogger;
3210

3311
inquire() {
3412
return [
35-
{
36-
name: "createBucket",
37-
type: "confirm",
38-
message: "Create a new S3 bucket",
39-
default: true,
40-
},
4113
{
4214
name: "bucketName",
4315
type: "input",
44-
message: "Existing bucket name",
45-
when: (ctx: any) => !(ctx as { createBucket: boolean }).createBucket,
16+
message: "Bucket name",
4617
},
4718
];
4819
}
4920

50-
prepare(config: PluginCreateOptions<SetupInquireResult>) {
21+
async prepare(config: PluginCreateOptions<SetupInquireResult>) {
5122
this._logger = config.logger;
52-
const ir = config.options;
53-
if (!ir.createBucket) {
54-
return Promise.resolve({
55-
bucketName: ir.bucketName as string,
56-
});
57-
} else {
58-
const id = uuid();
59-
const bucketName = `${BUCKET_PREFIX}-${id}`;
60-
if (config.noEmit) {
61-
this._logger.info(`Skip to create S3 bucket ${bucketName} because noEmit option.`);
62-
return Promise.resolve({ bucketName });
63-
}
64-
this._logger.info(`Create new S3 bucket: ${this._logger.colors.magenta(bucketName)}`);
65-
const spinner = this._logger.getSpinner(`creating bucket...`);
66-
spinner.start();
67-
return this._createBucket(bucketName)
68-
.then(bucketName => {
69-
return this._updatePolicy(bucketName);
70-
})
71-
.then(bucketName => {
72-
spinner.stop();
73-
return { bucketName };
74-
});
75-
}
76-
}
77-
78-
_updatePolicy(bucketName: string) {
79-
return new Promise<string>((resolve, reject) => {
80-
this._s3client
81-
.send(
82-
new PutBucketPolicyCommand({
83-
Bucket: bucketName,
84-
Policy: JSON.stringify(createPolicy(bucketName)),
85-
}),
86-
)
87-
.then(() => resolve(bucketName))
88-
.catch(err => reject(err));
89-
});
90-
}
91-
92-
_createBucket(bucketName: string) {
93-
return new Promise<string>((resolve, reject) => {
94-
this._s3client
95-
.send(
96-
new CreateBucketCommand({
97-
Bucket: bucketName,
98-
}),
99-
)
100-
.then(() => resolve(bucketName))
101-
.catch(err => reject(err));
102-
});
23+
const { bucketName } = config.options;
24+
const pluginConfig: PluginConfig = {
25+
bucketName,
26+
};
27+
return pluginConfig;
10328
}
10429
}

packages/reg-suit-core-testing/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"dependencies": {
1010
"reg-notify-gitlab-plugin": "^0.12.3",
1111
"reg-notify-slack-plugin": "^0.12.2",
12-
"reg-publish-gcs-plugin": "^0.12.2",
12+
"reg-publish-s3-plugin": "^0.12.2",
1313
"reg-simple-keygen-plugin": "^0.12.2",
1414
"reg-suit-core": "^0.12.2"
1515
},

0 commit comments

Comments
 (0)