diff --git a/.github/actions/output-event/output-event.js b/.github/actions/output-event/output-event.js index 20a78af..437b45d 100644 --- a/.github/actions/output-event/output-event.js +++ b/.github/actions/output-event/output-event.js @@ -1,5 +1,45 @@ const core = require('@actions/core'); -const { release } = require(process.env.GITHUB_EVENT_PATH); +const { GitHub } = require('@actions/github'); +const { release: { upload_url } } = require(process.env.GITHUB_EVENT_PATH); +const fs = require('fs'); + +async function run() { + try { + // Get authenticated GitHub client (Ocktokit): https://github.com/actions/toolkit/tree/master/packages/github#usage + const github = new GitHub(process.env.GITHUB_TOKEN); + const assetPath = core.getInput('asset_path', { required: true }); + const assetName = core.getInput('asset_name', { required: true }); + const assetContentType = core.getInput('asset_content_type', { required: true }); + + // Determine content-length for header to upload asset + const contentLength = filePath => fs.statSync(filePath).size; + + // Setup headers for API call, see Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-release-asset for more information + const headers = { 'content-type': assetContentType, 'content-length': contentLength(assetPath) }; + + // Upload a release asset + // API Documentation: https://developer.github.com/v3/repos/releases/#upload-a-release-asset + // Octokit Documentation: https://octokit.github.io/rest.js/#octokit-routes-repos-upload-release-asset + const uploadAssetResponse = await github.repos.uploadReleaseAsset({ + url: upload_url, + headers, + name: assetName, + file: fs.readFileSync(assetPath) + }); + + // Get the browser_download_url for the uploaded release asset from the response + const { + data: { browser_download_url: browserDownloadUrl } + } = uploadAssetResponse; + + // Set the output variable for use by other actions: https://github.com/actions/toolkit/tree/master/packages/core#inputsoutputs + core.setOutput('browser_download_url', browserDownloadUrl); + } catch (error) { + core.setFailed(error.message); + } +} + +module.exports = run; module.exports = () => { Object.entries(release).forEach(([key, value]) => { diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index 20cea6d..2f97e2e 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -17,23 +17,11 @@ jobs: run: npm install - name: Get release event - id: get_release_event + id: upload_existing_release_asset uses: ./.github/actions/output-event - - - name: Upload release - uses: yakuhzi/action-release@v1 - with: - file: ./dist/my-artifact.zip env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - # - name: Upload Release Asset - # id: upload-release-asset - # uses: actions/upload-release-asset@v1.0.1 - # env: - # GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - # with: - # upload_url: ${{ steps.get_release_event.outputs.upload_url }} - # asset_path: ./dist/my-artifact.zip - # asset_name: my-artifact.zip - # asset_content_type: application/zip + with: + asset_path: ./dist/my-artifact.zip + asset_name: my-artifact.zip + asset_content_type: application/zip