Skip to content

Commit

Permalink
GH Action: draft-pr
Browse files Browse the repository at this point in the history
Sync agent-helm release with the agent one
SCALRCORE-27249
  • Loading branch information
vmotso committed Sep 13, 2023
1 parent 7c506be commit f144cce
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
name: Update appVersion
name: Sync appVersion
description: |-
This action update appVersion in all charts. It gets the appVersion from the input
and sets it in each Chart.yaml and changes the version of Chart.yaml itself. Charts
versions is updated using the sem-version patch stategy. All changes are made in $GITHUB_WORKSPACE.
inputs:
app_version:
description: The new appVersion value
description: appVersion value
required: true
upstream:
description: Upsteam ID
required: true

outputs:
app_version:
description: The update appVersion value

chart_version:
description: The updated Chart.yaml version value

runs:
using: node16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10232,6 +10232,8 @@ const fs = __nccwpck_require__(7147);
const path = __nccwpck_require__(1017);

const chartsDir = path.join(process.env.GITHUB_WORKSPACE, 'charts');
const appVersion = core.getInput('app_version', { required: true });
const upstream = core.getInput('upstream', { required: true });

function getCharts() {
const files = fs.readdirSync(chartsDir);
Expand All @@ -10243,21 +10245,35 @@ function getCharts() {
return directories;
}

function update_charts() {
const charts = getCharts()
core.info(`The appVersion ${appVersion}`);
charts.forEach(function (chart) {
chartPath = path.join(chartsDir, chart, "Chart.yaml")
const chartData = yaml.load(fs.readFileSync(chartPath, 'utf8'))

chartData.appVersion = appVersion;
chartData.version = semver.inc(chartData.version, 'patch');
const updatedYaml = yaml.dump(chartData);
fs.writeFileSync(chartPath, updatedYaml, 'utf8');
core.info(`The new version of ${chart} is ${chartData.version}`);
});

}
async function push_changes() {
await exec.exec('git config user.name "github-actions[bot]"');
await exec.exec('git config user.email "github-actions[bot]@users.noreply.github.com"');
await exec.exec(`git checkout -b ${process.env.PR_BRANCH}`);
await exec.exec('git add charts');
await exec.exec(`git commit -m "Sync appVersion: ${appVersion}. Triggered by ${upstream}"`);
await exec.exec(`git push origin ${process.env.PR_BRANCH} --force`);
}

async function run() {
try {
const charts = getCharts()
const appVersion = core.getInput('app_version', { required: true });
core.info(`The appVersion ${appVersion}`);
charts.forEach(function (chart) {
chartPath = path.join(chartsDir, chart, "Chart.yaml")
const chartData = yaml.load(fs.readFileSync(chartPath, 'utf8'))

chartData.appVersion = appVersion;
chartData.version = semver.inc(chartData.version, 'patch');
const updatedYaml = yaml.dump(chartData);
fs.writeFileSync(chartPath, updatedYaml, 'utf8');
core.info(`The new version of ${chart} is ${chartData.version}`);
});
update_charts()
push_changes()
// draft_pr()
} catch (err) {
return core.setFailed(`Error: ${err}`)
}
Expand Down
56 changes: 56 additions & 0 deletions .github/actions/sync-app-version/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const yaml = require('js-yaml');
const core = require('@actions/core');
const semver = require('semver');
const fs = require('fs');
const path = require('path');

const chartsDir = path.join(process.env.GITHUB_WORKSPACE, 'charts');
const appVersion = core.getInput('app_version', { required: true });
const upstream = core.getInput('upstream', { required: true });

function getCharts() {
const files = fs.readdirSync(chartsDir);
const directories = files.filter((file) => {
const filePath = path.join(chartsDir, file);
return fs.statSync(filePath).isDirectory();
});
core.debug(`Charts: ${directories}`);
return directories;
}

function update_charts() {
const charts = getCharts()
core.info(`The appVersion ${appVersion}`);
charts.forEach(function (chart) {
chartPath = path.join(chartsDir, chart, "Chart.yaml")
const chartData = yaml.load(fs.readFileSync(chartPath, 'utf8'))

chartData.appVersion = appVersion;
chartData.version = semver.inc(chartData.version, 'patch');
const updatedYaml = yaml.dump(chartData);
fs.writeFileSync(chartPath, updatedYaml, 'utf8');
core.info(`The new version of ${chart} is ${chartData.version}`);
});

}
async function push_changes() {
await exec.exec('git config user.name "github-actions[bot]"');
await exec.exec('git config user.email "github-actions[bot]@users.noreply.github.com"');
await exec.exec(`git checkout -b ${process.env.PR_BRANCH}`);
await exec.exec('git add charts');
await exec.exec(`git commit -m "Sync appVersion: ${appVersion}. Triggered by ${upstream}"`);
await exec.exec(`git push origin ${process.env.PR_BRANCH} --force`);
}

async function run() {
try {
update_charts()
push_changes()
// draft_pr()
} catch (err) {
return core.setFailed(`Error: ${err}`)
}

}

run();
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "update-app-version-action",
"name": "sync-app-version-action",
"version": "1.0.0",
"description": "Update appVersion and version in Chart.yaml",
"description": "Main action fro sync-app-version.yaml workflow",
"main": "index.js",
"scripts": {
"lint": "eslint .",
Expand Down
40 changes: 0 additions & 40 deletions .github/actions/update-app-version/index.js

This file was deleted.

129 changes: 71 additions & 58 deletions .github/workflows/sync-app-version.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
name: Sync appVersion
# TODO: add description of this action

on:
push:
branches:
- SCALRCORE-27249
workflow_dispatch:
inputs:
app_version:
description: Scalr Agent version in format x.y.z (without `agent/` prefix).
required: true
type: string
upstream:
description: |
This input is used to link the dispatch with the upstream. It will be
as prefix in PR name.
required: true
type: string


env:
PR_BRANCH: 'actions/sync-app-version-${{ inputs.app_version }}'
Expand All @@ -18,71 +29,73 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Update appVersion
id: update
uses: ./.github/actions/update-app-version
with:
# app_version: ${{inputs.app_version}}
app_version: "0.42.0"

- name: Commit Changes
- name: printenv
shell: bash
run: |-
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
run: printenv

- name: Main
uses: ./.github/actions/sync-app-version
with:
app_version: ${{inputs.app_version}}
upstream: ${{inputs.upstream}}

git checkout -b $PR_BRANCH
# - name: Commit Changes
# shell: bash
# run: |-
# git config user.name "github-actions[bot]"
# git config user.email "github-actions[bot]@users.noreply.github.com"

git status
git add .
git commit -m "Release: ${{steps.update.outputs.version}}"
git push origin $PR_BRANCH --force
# git checkout -b $PR_BRANCH

# git status
# git add .
# git commit -m "Release: ${{steps.update.outputs.version}}"
# git push origin $PR_BRANCH --force

- name: 'Create Pull Request'
uses: 'actions/github-script@v6'
with:
script: |-
const tag = "v" + process.env.NEW_VERSION;
# - name: 'Create Pull Request'
# uses: 'actions/github-script@v6'
# with:
# script: |-
# const tag = "v" + process.env.NEW_VERSION;

try {
const listResponse = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
head: context.repo.owner + ":" + process.env.PR_BRANCH,
base: process.env.GITHUB_REF_NAME,
});
# try {
# const listResponse = await github.rest.pulls.list({
# owner: context.repo.owner,
# repo: context.repo.repo,
# state: "open",
# head: context.repo.owner + ":" + process.env.PR_BRANCH,
# base: process.env.GITHUB_REF_NAME,
# });

core.isDebug() && console.log(listResponse);
# core.isDebug() && console.log(listResponse);

if (!listResponse.data.length) {
const createResponse = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: "Release: " + tag,
body: process.env.RELEASE_NOTES,
head: process.env.PR_BRANCH,
base: process.env.GITHUB_REF_NAME,
});
# if (!listResponse.data.length) {
# const createResponse = await github.rest.pulls.create({
# owner: context.repo.owner,
# repo: context.repo.repo,
# title: "Release: " + tag,
# body: process.env.RELEASE_NOTES,
# head: process.env.PR_BRANCH,
# base: process.env.GITHUB_REF_NAME,
# });

core.info(
`Created PR #${createResponse.data.number} at ${createResponse.data.html_url}`
);
} else {
const updateResponse = await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: listResponse.data[0].number,
title: "Release: " + tag,
body: process.env.RELEASE_NOTES,
});
# core.info(
# `Created PR #${createResponse.data.number} at ${createResponse.data.html_url}`
# );
# } else {
# const updateResponse = await github.rest.pulls.update({
# owner: context.repo.owner,
# repo: context.repo.repo,
# pull_number: listResponse.data[0].number,
# title: "Release: " + tag,
# body: process.env.RELEASE_NOTES,
# });

core.info(
`Updated PR #${updateResponse.data.number} at ${updateResponse.data.html_url}`
);
}
} catch (err) {
console.error(err);
core.setFailed(`Failed to create pull request: ${err}`);
}
# core.info(
# `Updated PR #${updateResponse.data.number} at ${updateResponse.data.html_url}`
# );
# }
# } catch (err) {
# console.error(err);
# core.setFailed(`Failed to create pull request: ${err}`);
# }

0 comments on commit f144cce

Please sign in to comment.