Skip to content

Commit

Permalink
ci: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Dec 29, 2023
1 parent 0382c38 commit 0982420
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 20 deletions.
40 changes: 20 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,44 @@ jobs:
key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock', '**/*.patch') }}
- name: Install node modules
run: yarn install --frozen-lockfile
- name: Build artifacts
run: ./scripts/ci/build-artifacts.sh
- name: Comment on github
run: node ./scripts/github/comment.js "[Preview Preparing...](https://github.com/ng-alain/delon/actions/runs/${{ github.run_id }})"
env:
ACCESS_REPO: ${{ github.repository }}
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Comment on github
run: node ./scripts/azure/github-comment.js "[Preview Preparing...](https://github.com/ng-alain/delon/actions/runs/${{ github.run_id }})"
PR: ${{ github.event.number }}
- name: Build artifacts
run: ./scripts/ci/build-artifacts.sh
env:
ACCESS_REPO: ${{ secrets.ACCESS_REPO }}
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SYSTEM_PULLREQUEST_PULLREQUESTNUMBER: ${{ github.event.number }}
- name: Build site
run: yarn run site:build
- name: 'Deploy Site'
run: |
export DEPLOY_DOMAIN=https://preview-${SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}-ng-alain-delon.surge.sh
export DEPLOY_DOMAIN=https://preview-${{ github.repository }}-ng-alain-delon.surge.sh
echo "Deploy to $DEPLOY_DOMAIN"
cp ./src/dist/browser/index.html ./src/dist/browser/404.html
npx surge --project ./src/dist/browser --domain $DEPLOY_DOMAIN
env:
ACCESS_REPO: $(ACCESS_REPO)
ACCESS_TOKEN: $(GITHUB_TOKEN)
SURGE_LOGIN: $(SURGE_LOGIN)
SURGE_TOKEN: $(SURGE_TOKEN)
ACCESS_REPO: ${{ github.repository }}
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SURGE_LOGIN: ${{ secrets.SURGE_LOGIN }}
SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }}
- name: 'Update comment on github'
run: |
export DEPLOY_DOMAIN=https://preview-${SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}-ng-alain-delon.surge.sh
node ./scripts/azure/github-comment.js "[Preview is ready!]($DEPLOY_DOMAIN)"
export DEPLOY_DOMAIN=https://preview-${{ github.repository }}-ng-alain-delon.surge.sh
node ./scripts/github/comment.js "[Preview is ready!]($DEPLOY_DOMAIN)"
env:
ACCESS_REPO: $(ACCESS_REPO)
ACCESS_TOKEN: $(GITHUB_TOKEN)
SYSTEM_PULLREQUEST_PULLREQUESTNUMBER: ${{ github.event.number }}
ACCESS_REPO: ${{ github.repository }}
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR: ${{ github.event.number }}
- name: 'When failed'
if: failure()
run: node ./scripts/azure/github-comment.js "[Preview Build Failed!](https://github.com/ng-alain/delon/actions/runs/${{ github.run_id }})"
run: node ./scripts/github/comment.js "[Preview Build Failed!](https://github.com/ng-alain/delon/actions/runs/${{ github.run_id }})"
env:
ACCESS_REPO: $(ACCESS_REPO)
ACCESS_TOKEN: $(GITHUB_TOKEN)
SYSTEM_PULLREQUEST_PULLREQUESTNUMBER: ${{ github.event.number }}
ACCESS_REPO: ${{ github.repository }}
ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR: ${{ github.event.number }}

test:
runs-on: ubuntu-latest
Expand Down
61 changes: 61 additions & 0 deletions scripts/github/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const REPO = process.env.ACCESS_REPO;
const TOKEN = process.env.ACCESS_TOKEN;
const PR = process.env.PR;
const REPLACE_MARK = '<!-- AZURE_UPDATE_COMMENT -->';

const argv = process.argv;

const comment = argv[argv.length - 1];

const wrappedComment = `
${REPLACE_MARK}
${comment}
`.trim();

async function withGithub(url, json, method) {
const res = await fetch(url, {
method: method || (json ? 'POST' : 'GET'),
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(TOKEN).toString('base64')}`,
},
body: json ? JSON.stringify(json) : undefined,
});

return res.json();
}

(async function run() {
if (PR == null) {
console.log('未获取到PR,忽略处理')
return;
}

const commentUrl = `https://api.github.com/repos/${REPO}/issues/${PR}/comments`;
console.log(`commentUrl`, commentUrl);
const comments = await withGithub(commentUrl);
console.log(`comments data`, comments);

// Find my comment
const updateComment = comments.find(({ body }) => body.includes(REPLACE_MARK));
console.log('Origin comment:', updateComment);

// Update
let res;
if (!updateComment) {
res = await withGithub(commentUrl, {
body: wrappedComment,
});
} else {
res = await withGithub(
`https://api.github.com/repos/${REPO}/issues/comments/${updateComment.id}`,
{
body: wrappedComment,
},
'PATCH',
);
}

console.log(res);
})();

0 comments on commit 0982420

Please sign in to comment.