Skip to content

Commit

Permalink
added automation for system-agent version upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
vardhaman22 authored and chiukapoor committed Oct 14, 2024
1 parent 2172146 commit 0871d2b
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
111 changes: 111 additions & 0 deletions .github/workflows/system-agent-upgrade.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: System Agent Upgrade
on:
workflow_dispatch:
inputs:
system_agent_version:
type: string
description: "system agent version to update"
source_author:
type: string
description: "Username of the source for this workflow run"
source_url:
type: string
description: "URL of the source for this workflow run"

env:
SYSTEM_AGENT_VERSION: ${{ github.event.inputs.system_agent_version }}
INPUT_SOURCE_AUTHOR: ${{ github.event.inputs.source_author }}
INPUT_SOURCE_URL: ${{ github.event.inputs.source_url }}

permissions:
contents: write
pull-requests: write
jobs:
system-agent-upgrade:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v4
- name: Update system agent in files
run: ./scripts/system-agent-upgrade
- name: Check for repository changes
run: |
if git diff --name-only --exit-code; then
echo "No changes found in repository after 'updating system agent version'"
echo "changes_exist=false" >> $GITHUB_ENV
else
echo "Changes found in repository after 'updating system agent version':"
git diff --name-only
echo "changes_exist=true" >> $GITHUB_ENV
fi
- name: Create branch, commit and push
if: ${{ env.changes_exist == 'true' }}
id: branch
run: |
BRANCH="githubaction-system-agent-upgrade-$(date +%Y-%m-%d-%H-%M-%S)"
echo "::set-output name=branch::$BRANCH"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git checkout -b "$BRANCH"
git commit -a -m "updated system-agent to ${SYSTEM_AGENT_VERSION}"
git push origin "$BRANCH"
- name: Read App Secrets
uses: rancher-eio/read-vault-secrets@main
with:
secrets: |
secret/data/github/repo/${{ github.repository }}/github/app-credentials appId | APP_ID ;
secret/data/github/repo/${{ github.repository }}/github/app-credentials privateKey | PRIVATE_KEY
- name: Create App Token
uses: actions/create-github-app-token@v1
id: app-token
with:
app-id: ${{ env.APP_ID }}
private-key: ${{ env.PRIVATE_KEY }}

- name: Create Pull Request
if: ${{ env.changes_exist == 'true' }}
id: cpr
uses: actions/github-script@v7
env:
SOURCE_BRANCH: ${{ steps.branch.outputs.branch }}
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
with:
github-token: ${{ env.GITHUB_TOKEN }}
script: |
const { SYSTEM_AGENT_VERSION } = process.env
let body = 'Auto-generated by GitHub Actions\n\n'
if ( `${ process.env.INPUT_SOURCE_URL }` ) {
body += `\nSource URL: ${ process.env.INPUT_SOURCE_URL }`
}
if ( `${ process.env.INPUT_SOURCE_AUTHOR }` ) {
body += `\nSource AUTHOR: @${ process.env.INPUT_SOURCE_AUTHOR}`
}
const { data: pr } = await github.rest.pulls.create({
title: `[${{ github.ref_name }}] update system-agent to ${SYSTEM_AGENT_VERSION}`,
body: body,
owner: context.repo.owner,
repo: context.repo.repo,
base: "${{ github.ref_name }}",
head: `${ process.env.SOURCE_BRANCH }`
});
await github.rest.issues.addLabels({
...context.repo,
issue_number: pr.number,
labels: ["status/auto-created"],
});
if ( `${ process.env.INPUT_SOURCE_AUTHOR }` ) {
await github.rest.issues.addAssignees({
...context.repo,
issue_number: pr.number,
assignees: [`${ process.env.INPUT_SOURCE_AUTHOR}`],
});
}
console.log('Created new pull request');
return pr.html_url;
- name: Check outputs
if: ${{ env.changes_exist == 'true' }}
run: |
echo "Pull Request URL - ${{ steps.cpr.outputs.result }}"
10 changes: 10 additions & 0 deletions scripts/system-agent-upgrade
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
set -e
VERSION=$SYSTEM_AGENT_VERSION

updated_install_script_url="https://github.com/rancher/system-agent/releases/download/${VERSION}/install.sh" # update it to regular expression
existing_install_script_url=$(grep -E -o 'https:\/\/github.com\/rancher\/system-agent\/releases\/download\/[^/]+\/install.sh' pkg/settings/setting.go)
sed -i "s|$existing_install_script_url|$updated_install_script_url|g" pkg/settings/setting.go

sed -i "s|^ENV CATTLE_SYSTEM_AGENT_VERSION .\+$|ENV CATTLE_SYSTEM_AGENT_VERSION ${VERSION}|g" package/Dockerfile # try to use regex to avoid extracting the string from the file
sed -i "s|^ENV CATTLE_SYSTEM_AGENT_VERSION .\+$|ENV CATTLE_SYSTEM_AGENT_VERSION ${VERSION}|g" tests/v2/codecoverage/package/Dockerfile # try to use regex to avoid extracting the string from the file

0 comments on commit 0871d2b

Please sign in to comment.