发布1.0版本

This commit is contained in:
ilovintit
2025-06-05 10:52:03 +08:00
parent bbab50bce7
commit e7060a31d2
7 changed files with 171726 additions and 354 deletions
+61 -6
View File
@@ -1,4 +1,15 @@
import * as core from '@actions/core'
import * as k8s from '@kubernetes/client-node'
import * as fs from 'fs'
import * as path from 'path'
import { filter, clone } from 'lodash'
interface Config {
branch: string
cluster: string
namespace: string
deployment: string
}
/**
* The main function for the action.
@@ -7,13 +18,57 @@ import * as core from '@actions/core'
*/
export async function run(): Promise<void> {
try {
const image: string = core.getInput('image')
// Debug logs are only output if the `ACTIONS_STEP_DEBUG` secret is true
console.log('image', image)
//
const configPath = core.getInput('config-path')
console.log('configPath:', configPath)
//
const clusterPath = core.getInput('cluster-path')
console.log('clusterPath:', clusterPath)
//
const deployImage: string = core.getInput('image')
console.log('image:', deployImage)
//
const branch: string = String(process.env.GITHUB_REF_NAME)
console.log('branch', branch)
// Log the current timestamp, wait, then log the new timestamp
core.debug(new Date().toTimeString())
console.log('branch:', branch)
//
const configJson = fs.readFileSync(path.join(__dirname, configPath), 'utf8')
console.log('configJson:', configJson)
const deployConfigArr = JSON.parse(configJson) as Config[]
console.log('deployConfigArr:', deployConfigArr)
const filterDeployConfig = filter(deployConfigArr, { branch: branch })
for (const deployConfig of filterDeployConfig) {
console.log('deployConfig:', deployConfig)
const kc = new k8s.KubeConfig()
kc.loadFromFile(
path.join(__dirname, clusterPath + deployConfig.cluster + '.yaml')
)
const k8sApi = kc.makeApiClient(k8s.AppsV1Api)
const nowDeployment = await k8sApi.readNamespacedDeployment({
name: deployConfig.deployment,
namespace: deployConfig.namespace
})
console.log('nowDeployment:', nowDeployment)
const deployment = clone(nowDeployment)
if (deployment.spec === undefined) {
console.log('deployment.spec is missing')
continue
}
if (deployment.spec.template.spec === undefined) {
console.log('deployment.spec.template.spec is missing')
continue
}
if (deployment.spec.template.spec.containers.length === 0) {
console.log('deployment.spec.template.spec.containers is zero')
continue
}
deployment.spec.template.spec.containers[0].image = deployImage
const deployRes = await k8sApi.patchNamespacedDeployment({
name: deployConfig.deployment,
namespace: deployConfig.namespace,
body: deployment
})
console.log('deployRes:', deployRes)
}
} catch (error) {
// Fail the workflow run if an error occurs
if (error instanceof Error) core.setFailed(error.message)