From 86204309edbe74c52ffc4159124dfb324997328b Mon Sep 17 00:00:00 2001 From: Flawid DSouza Date: Fri, 7 Apr 2023 18:06:36 +0530 Subject: [PATCH] feat: add github action workflow to build and publish snap --- .github/workflows/main.yml | 33 ++++++- .../upload-file-to-latest-draft-release.js | 95 +++++++++++++++++++ 2 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 scripts/upload-file-to-latest-draft-release.js diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4563f270..3ab0bcf1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -73,6 +73,37 @@ jobs: run: | npm ci npm run publish + build-electron-snap: + needs: [build-electron-linux] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Setup Node 16 + uses: actions/setup-node@v3 + with: + node-version: 16 + - name: Build UI + working-directory: ./packages/ui + run: | + npm ci + npm run build-desktop + - name: Build snap package + working-directory: ./packages/electron + run: | + npm ci + npm run build-snap + - name: Install Snapcraft + run: sudo apt-get install snapd snapcraft + - name: Set up Snap Store credentials & publish snap package + run: | + echo "${{ secrets.SNAP_STORE_TOKEN }}" | base64 -d > credentials.snapcraft + export SNAPCRAFT_STORE_CREDENTIALS=$(cat credentials.snapcraft) + # snapcraft push --release=stable ./packages/electron/dist/*.snap + - name: Set asset name + run: echo "SNAP_FILE_NAME=$(basename $(ls ./packages/electron/dist/*.snap))" >> $GITHUB_ENV + - name: Upload snap to GitHub release + run: | + node scripts/upload-file-to-latest-draft-release.js ./packages/electron/dist/${{ env.SNAP_FILE_NAME }} build-docker-image: runs-on: ubuntu-latest steps: @@ -101,7 +132,7 @@ jobs: run: | docker push flawiddsouza/restfox:${{ env.VERSION }} update-release-notes: - needs: [build-electron-linux, build-electron-windows, build-electron-mac, build-docker-image] + needs: [build-electron-linux] runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 diff --git a/scripts/upload-file-to-latest-draft-release.js b/scripts/upload-file-to-latest-draft-release.js new file mode 100644 index 00000000..ba8b1ccf --- /dev/null +++ b/scripts/upload-file-to-latest-draft-release.js @@ -0,0 +1,95 @@ +const https = require('https') +const fs = require('fs') +const path = require('path') + +const accessToken = process.env.GITHUB_TOKEN +const repository = process.env.GITHUB_REPOSITORY +const [owner, repo] = repository.split('/') +const tag = 'draft' +const filePath = process.argv[2] + +if (!filePath) { + console.error('Please provide the file path as a command line argument!') + process.exit(1) +} + +const makeRequest = async (options, payload = null) => { + return new Promise((resolve, reject) => { + const req = https.request(options, (res) => { + let data = '' + + res.on('data', (chunk) => { + data += chunk + }) + + res.on('end', () => { + resolve({ + statusCode: res.statusCode, + headers: res.headers, + data: JSON.parse(data), + }) + }) + }) + + req.on('error', (error) => { + reject(error) + }) + + if (payload) { + req.write(payload) + } + + req.end() + }) +} + +const getAllReleasesOptions = { + hostname: 'api.github.com', + path: `/repos/${owner}/${repo}/releases`, + method: 'GET', + headers: { + Authorization: `token ${accessToken}`, + Accept: 'application/vnd.github+json', + 'User-Agent': 'Node.js', + }, +} + +const main = async () => { + try { + const response = await makeRequest(getAllReleasesOptions) + const releases = response.data + const draftReleases = releases.filter((release) => { + return release.draft === true + }) + + if (draftReleases.length === 0) { + throw new Error('No draft releases found!') + } + + const release = draftReleases[0] + const releaseId = release.id + + const fileContent = fs.readFileSync(filePath) + const fileName = path.basename(filePath) + + const uploadOptions = { + hostname: 'uploads.github.com', + path: `/repos/${owner}/${repo}/releases/${releaseId}/assets?name=${fileName}`, + method: 'POST', + headers: { + 'User-Agent': 'Node.js', + 'Authorization': `Bearer ${accessToken}`, + 'Content-Type': 'application/octet-stream', + 'Content-Length': fileContent.length, + }, + } + + const uploadResponse = await makeRequest(uploadOptions, fileContent) + console.log(`Uploaded ${filePath} to ${repo} ${tag}!`) + console.log(uploadResponse) + } catch (error) { + console.error(`Error updating release: ${error.message}`) + } +} + +main()