Skip to content

trigger-publish-provisioning-libs #86

trigger-publish-provisioning-libs

trigger-publish-provisioning-libs #86

name: Publish provisioning Libraries
on:
repository_dispatch:
types: [trigger-publish-provisioning-libs]
permissions:
contents: write
pull-requests: write
jobs:
publish-libs:
if: startsWith(github.event.client_payload.branch, 'release_') || github.event.client_payload.branch == 'main'
runs-on: ubuntu-latest
concurrency:
group: ${{ github.event.client_payload.branch }}-publish-provisioning-libs
cancel-in-progress: true
env:
PLATFORMS: '["MG24", "MGM24", "MG26", "SI917", "SI917_PSA"]'
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.client_payload.branch }}
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install required packages
run: npm install unzipper @actions/core
- name: Define downloadAndExtractArtifact function
id: define-function
run: |
echo "module.exports = async function downloadAndExtractArtifact(github, core, artifactName, downloadPath, filePath, branch) {
const fs = require('fs');
const path = require('path');
const unzipper = require('unzipper');
const provisioningRepo = 'matter_provisioning';
const repoOwner = 'SiliconLabsSoftware';
const artifacts = await github.rest.actions.listArtifactsForRepo({
owner: repoOwner,
repo: provisioningRepo,
ref: branch
});
const filteredArtifacts = artifacts.data.artifacts
.filter(a => a.name === artifactName)
.sort((a, b) => new Date(b.created_at) - new Date(a.created_at));
if (filteredArtifacts.length === 0) {
core.setFailed(`Artifact ${artifactName} not found`);
return;
}
const latestArtifact = filteredArtifacts[0];
const { data: artifactData } = await github.rest.actions.downloadArtifact({
owner: repoOwner,
repo: provisioningRepo,
artifact_id: latestArtifact.id,
archive_format: 'zip'
});
fs.mkdirSync(downloadPath, { recursive: true });
fs.writeFileSync(filePath, Buffer.from(artifactData));
fs.createReadStream(filePath)
.pipe(unzipper.Extract({ path: downloadPath }))
.on('close', () => {
console.log(`Downloaded and extracted ${artifactName} to ${downloadPath}`);
fs.unlinkSync(filePath); // Delete the .zip file after extraction
})
.on('error', (error) => {
console.error(`Error extracting ${artifactName}: ${error.message}`);
core.setFailed(`Failed to extract ${artifactName}: ${error.message}`);
});
}" > downloadAndExtractArtifactSource.js
- name: Download Built Libraries
uses: actions/github-script@v7
with:
github-token: ${{ secrets.WORKFLOW_TOKEN }}
script: |
const path = require('path');
const downloadAndExtractArtifact = require('./downloadAndExtractArtifactSource');
const platforms = JSON.parse(process.env.PLATFORMS);
const branchName = context.payload.client_payload.branch;
for (const platform of platforms) {
const downloadPath = path.join(process.env.GITHUB_WORKSPACE, 'provision/libs');
const filePath = path.join(downloadPath, `built-libraries-${platform}.zip`);
await downloadAndExtractArtifact(github, core, `built-libraries-${platform}`, downloadPath, filePath, branchName);
}
- name: Download Headers
uses: actions/github-script@v7
with:
github-token: ${{ secrets.WORKFLOW_TOKEN }}
script: |
const path = require('path');
const downloadAndExtractArtifact = require('./downloadAndExtractArtifactSource');
const branchName = context.payload.client_payload.branch;
const downloadPath = path.join(process.env.GITHUB_WORKSPACE, 'provision/headers');
const filePath = path.join(downloadPath, 'headers.zip');
await downloadAndExtractArtifact(github, core, 'headers', downloadPath, filePath, branchName);
- name: Create Pull Request
id: create-pull-request
uses: peter-evans/create-pull-request@v7
with:
title: "Update provisioning libraries and headers for ${{ github.event.client_payload.branch }}"
body: "This PR updates the provisioning libraries and headers for the ${{ github.event.client_payload.branch }} branch."
commit-message: "Update provisioning libraries and headers"
base: ${{ github.event.client_payload.branch }}
branch: automation/update_${{ github.event.client_payload.branch }}_provisioning
add-paths: 'provision'
author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
committer: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
token: ${{ secrets.GITHUB_TOKEN }}
# This step is necessary since the bot doesn't have the necessary permissions to trigger the CI
- name: Close and re-open PR to trigger the CI
uses: actions/github-script@v7
with:
github-token: ${{ secrets.WORKFLOW_TOKEN }}
script: |
const prNumber = ${{ steps.create-pull-request.outputs.pull-request-number }};
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
state: 'closed'
});
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
state: 'open'
});
console.log(`PR #${prNumber} has been closed and reopened to trigger the CI.`);