Skip to content

Commit

Permalink
feat(auto-pr): update setup-node for auto create assets-pr
Browse files Browse the repository at this point in the history
  • Loading branch information
49659410+tx0c committed May 1, 2024
1 parent 530f8d9 commit 849a219
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
32 changes: 24 additions & 8 deletions .github/workflows/checkin-assets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Billboard Rounds - Create PR to update latest round
on: workflow_dispatch

jobs:
create-pull-request:
ci-round-assets:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
Expand All @@ -12,21 +12,37 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
node-version: '>=20'

- name: Install Dependencies
run: npm install @aws-sdk/client-s3 ## the only dependency for ./bin/sync-up-latest-round.mjs

- run: ./bin/sync-up-latest-round.mjs
- name: Sync up Latest Round
run: "./bin/sync-up-latest-round.mjs | tee message.log"
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: ap-southeast-1

- uses: gr2m/create-or-update-pull-request-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request@v6
with:
path: "./public/static"
## token: ${{ secrets.BILLBOARD_GITHUB_TOKEN }}
commit-message: auto checkin latest Round assets as Preview
base: main
signoff: false
branch: auto/round-assets
delete-branch: true
# path: public/static
add-paths: public/static
title: check in latest round Assets
body-path: message.log
body: |
Update preview Assets
- Updated with *today's* date
- Auto-generated by [create-pull-request][1]
[1]: https://github.com/peter-evans/create-pull-request
labels: assets, automated pr
draft: true
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)

[![Billboard Rounds - Create PR to update latest round](https://github.com/thematters/billboard-app/actions/workflows/checkin-assets.yaml/badge.svg)](https://github.com/thematters/billboard-app/actions/workflows/checkin-assets.yaml)

## Getting started

### Start local dev
Expand Down
28 changes: 20 additions & 8 deletions bin/sync-up-latest-round.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,43 @@ const client = new S3Client() // use credentials from environment

const AssetsDir = './public/static'

export const main = async () => {
export const main = async (branch = 'prod') => {
const controller = new AbortController()
const { signal } = controller
const developPrefix = branch !== 'prod' ? 'web-develop/' : ''

console.log('```console')
try {
// The Body object also has 'transformToByteArray' and 'transformToWebStream' methods.
const roundsContent = await (
await client.send(
new GetObjectCommand({
Bucket: 'matters-billboard',
Key: 'rounds/rounds.json',
Key: `${developPrefix}rounds/rounds.json`,
})
)
).Body.transformToString()
const rounds = JSON.parse(roundsContent)
const lastRound = rounds[rounds.length - 1]

console.log(
new Date(),
`got ${rounds.length} rounds with latest:`,
rounds[rounds.length - 1]
lastRound
)
await writeFile(`${AssetsDir}/rounds.json`, roundsContent, { signal })
if (lastRound.draft) {
delete lastRound.draft // finalize the last round if still draft
// roundsContent = JSON.stringify(rounds);
}

await writeFile(`${AssetsDir}/rounds.json`, JSON.stringify(rounds), {
signal,
})
console.log(
new Date(),
`written rounds.json with ${(roundsContent.length / 1024).toFixed(1)} KBytes.`
)

const lastRound = rounds[rounds.length - 1]

console.log(new Date(), 'latest round:', lastRound.id, lastRound.dirpath)

const createDir = await mkdir(`${AssetsDir}/${lastRound.dirpath}`, {
Expand All @@ -47,7 +56,7 @@ export const main = async () => {
await client.send(
new GetObjectCommand({
Bucket: 'matters-billboard',
Key: `rounds/${lastRound.dirpath}/${file}`,
Key: `${developPrefix}rounds/${lastRound.dirpath}/${file}`,
})
)
).Body.transformToString()
Expand All @@ -66,6 +75,9 @@ export const main = async () => {
} catch (err) {
console.error(new Date(), `ERROR:`, err)
}

console.log('```')
}

await main()
// console.log('run with argv:', process.argv)
await main(process.argv?.[2] || 'prod')

0 comments on commit 849a219

Please sign in to comment.