Skip to content

Commit

Permalink
Merge pull request #34 from ayeressian/add_upload_flag
Browse files Browse the repository at this point in the history
Fix API description, version and added upload flag
  • Loading branch information
ayeressian authored May 16, 2023
2 parents 82a6a26 + 841f8a2 commit 965a9c6
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 35 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ jobs:
storagePassword: "${{ secrets.STORAGE_PASSWORD_TEST }}"
accessKey: "${{ secrets.ACCESS_KEY_TEST }}"
pullZoneId: "${{ secrets.ZONE_ID_TEST }}"
upload: "true",
remove: "true"
purge: "true"
42 changes: 26 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
# bunnycdn-storage-deploy

This action deploys selected directory to BunnyCDN storage.
This action performs 3 operations.
* Uploads files and folders to storage.
* Removes all the files from storage.
* Purges pull zone.

Each operation can be activated with their respective upload, remove and purge flags.

## Inputs

### `source`
### `upload`

**Required** The source directory folder.
It will upload files and folders if "true" provided. source, storageZoneName and storagePassword inputs should be provided.

### `storageZoneName`
### `remove`

**Required** The name of your storage zone where you are connecting to.
It will remove all the files from storage before uploading if "true" provided. storageZoneName and storagePassword inputs should be provided.

### `accessKey`
### `purge`

It will purge the pull zone if "true" provided. pullZoneId and accessKey inputs should be provided.

**Required** The API key.
### `source`

The source directory that should be uploaded.

### `storageZoneName`

The name of storage zone where you are connecting to.

### `storageEndpoint`

Expand All @@ -24,29 +37,26 @@ The storage endpoint. Default value is storage.bunnycdn.com

The storage password. It should be read and write capable.

### `pullZoneId`

Necessary for purging pull zone.

### `purge`
### `accessKey`

It will purge the pull zone if true. pullZoneId and pullZoneAccessKey should be provided.
The API key.

### `remove`
### `pullZoneId`

It will remove the files from storage before uploading if "true" provided.
Pull zone ID.

## Example usage

```
- name: Deploy to BunnyCDN
uses: ayeressian/bunnycdn-storage-deploy@v1.1.6
uses: ayeressian/bunnycdn-storage-deploy@v=2.0.0
with:
source: "dist"
storageZoneName: "${{ secrets.STORAGE_NAME }}"
storagePassword: "${{ secrets.STORAGE_PASSWORD }}"
accessKey: "${{ secrets.STORAGE_KEY }}"
pullZoneId: "${{ secrets.ZONE_ID }}"
upload: "true"
remove: "true"
purge: "true"
```
2 changes: 0 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ inputs:
required: true
storagePassword:
description: "The storage password. It should be read and write capable."
required: true
storageEndpoint:
description: "The storage endpoint"
default: "storage.bunnycdn.com"
accessKey:
description: "The API key."
required: true
pullZoneId:
description: "Is required for purging."
purge:
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bunnycdn-storage-deploy",
"version": "1.1.6",
"version": "2.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
Expand Down
48 changes: 40 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,55 @@ const run = async () => {
getInput("storageEndpoint") ?? "storage.bunnycdn.com";
const storagePassword = getInput("storagePassword");
const accessKey = getInput("accessKey");
const removeFlag = getInput("remove");
const pullZoneId = getInput("pullZoneId");
const purgeFlag = getInput("purge");

const purgePullZoneFlag = getInput("purgePullZone");
const removeFlag = getInput("remove");
const uploadFlag = getInput("upload");

if (removeFlag === "true") {
if (!storageZoneName) {
throw new Error("Can't remove, storageZoneName was not set.");
}
if (!storagePassword) {
throw new Error("Can't remove, storagePassword was not set.");
}
info(`Deleting files from storage ${storageZoneName}`);
await remove(storageZoneName, storagePassword, storageEndpoint);
}

if (storageZoneName && storagePassword) {
info(`Deploying ${source} folder/file to storage ${storageZoneName}`);
await uploader(source, storageZoneName, storagePassword, storageEndpoint);
if (uploadFlag === "true") {
if (!source) {
throw new Error("Can't upload, source was not set.");
}
if (!storageZoneName) {
throw new Error("Can't upload, storageZoneName was not set.");
}
if (!storagePassword) {
throw new Error("Can't upload, storagePassword was not set.");
}
if (storageZoneName && storagePassword) {
info(`Uploading ${source} folder/file to storage ${storageZoneName}`);
await uploader(
source,
storageZoneName,
storagePassword,
storageEndpoint
);
}
}

if (pullZoneId && accessKey && purgeFlag) {
info(`Purging pull zone with the id ${pullZoneId}`);
await purge(pullZoneId, accessKey);
if (purgePullZoneFlag == "true") {
if (!pullZoneId) {
throw new Error("Can't purge, pullZoneId was not set.");
}
if (!accessKey) {
throw new Error("Can't upload, accessKey was not set.");
}
if (pullZoneId && accessKey) {
info(`Purging pull zone with the id ${pullZoneId}`);
await purge(pullZoneId, accessKey);
}
}
} catch (error) {
setFailed(error as string | Error);
Expand Down
4 changes: 2 additions & 2 deletions src/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import fetch, { Response } from "node-fetch";

const remove = async (
storageName: string,
accessKey: string,
storagePassword: string,
storageEndpoint: string
): Promise<Response> => {
const url = `https://${storageEndpoint}/${storageName}/`;
info(`Removing storage data with ${url}`);
const response = await fetch(url, {
method: "DELETE",
headers: {
AccessKey: accessKey,
AccessKey: storagePassword,
},
});
// THERE IS A BUG IN API 400 IS VALID SOMETIMES
Expand Down
8 changes: 4 additions & 4 deletions src/uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { info } from "@actions/core";
const uploadFile = async (
entry: readdirp.EntryInfo,
storageName: string,
accessKey: string,
storagePassword: string,
storageEndpoint: string
) => {
const readStream = fs.createReadStream(entry.fullPath);
Expand All @@ -18,7 +18,7 @@ const uploadFile = async (
{
method: "PUT",
headers: {
AccessKey: accessKey,
AccessKey: storagePassword,
},
body: readStream,
}
Expand All @@ -37,10 +37,10 @@ const uploadFile = async (
export default async function run(
path: string,
storageName: string,
accessKey: string,
storagePassword: string,
storageEndpoint: string
): Promise<void> {
for await (const entry of readdirp(path)) {
await uploadFile(entry, storageName, accessKey, storageEndpoint);
await uploadFile(entry, storageName, storagePassword, storageEndpoint);
}
}

0 comments on commit 965a9c6

Please sign in to comment.