Skip to content

Commit

Permalink
feat: add github action workflow to build and publish snap
Browse files Browse the repository at this point in the history
  • Loading branch information
flawiddsouza committed Apr 7, 2023
1 parent 7237a29 commit 8620430
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 1 deletion.
33 changes: 32 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
95 changes: 95 additions & 0 deletions scripts/upload-file-to-latest-draft-release.js
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 8620430

Please sign in to comment.