Skip to content

Commit

Permalink
start
Browse files Browse the repository at this point in the history
  • Loading branch information
tvrcgo committed Apr 21, 2020
1 parent a97eaa0 commit b13f5f9
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 2 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

name: Release new version

on:
push:
tags:
- 'v*'

jobs:
publish-ghpkg:
runs-on: ubuntu-latest
steps:
- uses: actions/create-release@v1
id: create_release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
# upload-to-oss
Upload assets to OSS

# Upload to OSS

上传单个文件或文件夹下所有文件(不含子文件夹)到 OSS

## Inputs

- `key-id`: OSS AccessKeyId
- `key-secret`: OSS AccessKeySecret
- `region`: 区域,如 `oss-cn-shenzhen`
- `bucket`:
- `asset-path`: 本地资源路径
- `target-path`: OSS 对象存储路径

## Outputs

- `url`: 文件在 OSS 上的 url。上传多个文件时,多个 url 用逗号隔开。

## Usage

```yaml
- name: Upload to oss
id: upload_to_oss
uses: actions/[email protected]
with:
key-id: ${{ secrets.OSS_KEY_ID }}
key-secret: ${{ secrets.OSS_KEY_SECRET }}
region: oss-cn-shenzhen
bucket: tvrcgo
asset-path: ./
target-path: /github-actions/pika-boilerplate/
```
25 changes: 25 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

name: 'Upload to OSS'
description: 'Upload assets to aliyun OSS'
branding:
icon: 'upload-cloud'
color: 'yellow'
inputs:
region:
required: true
key-id:
required: true
key-secret:
required: true
bucket:
required: true
asset-path:
required: true
target-path:
required: true
outputs:
url:
description: oss url
runs:
using: 'node12'
main: 'index.js'
45 changes: 45 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

const core = require('@actions/core');
const github = require('@actions/github');
const OSS = require('ali-oss');
const fs = require('fs');
const { resolve } = require('path');

(async () => {
try {
const oss = new OSS({
region: core.getInput('region'),
accessKeyId: core.getInput('key-id'),
accessKeySecret: core.getInput('key-secret'),
bucket: core.getInput('bucket')
})

const assetPath = core.getInput('asset-path', { required: true }).replace(/\/+$/g, '')
const targetPath = core.getInput('target-path', { required: true }).replace(/\/+$/g, '')

if (fs.existsSync(assetPath)) {
const stat = fs.lstatSync(assetPath)
// upload file
if (stat.isFile()) {
const filename = assetPath.slice(assetPath.lastIndexOf('/') + 1)
const res = await oss.put(targetPath, assetPath)
core.setOutput('url', res.url)
}
// upload dir
if (stat.isDirectory()) {
const files = fs.readdirSync(resolve(assetPath))
const res = await Promise.all(
files.filter(f => fs.lstatSync(resolve(assetPath, f)).isFile())
.map(file => {
return oss.put(`${targetPath}/${file}`, resolve(assetPath, file))
})
)
core.setOutput('url', res.map(r => r.url).join(','))
}
} else {
core.setFailed('asset_path does not exist.')
}
} catch (err) {
core.setFailed(err.message)
}
})()
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "upload-to-oss",
"version": "0.1.0",
"description": "",
"main": "index.js",
"scripts": {},
"keywords": [],
"author": "tvrcgo",
"license": "ISC",
"dependencies": {
"@actions/core": "^1.2.3",
"@actions/github": "^2.1.1",
"ali-oss": "^6.6.0"
}
}

0 comments on commit b13f5f9

Please sign in to comment.