mirror of
https://gitee.com/rancher/rancher.git
synced 2025-12-06 07:49:17 +08:00
Add workflow to change env vars
This commit is contained in:
84
.github/workflows/replace-env-value.yml
vendored
Normal file
84
.github/workflows/replace-env-value.yml
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
name: Replace environment variable in file
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
filepath:
|
||||
type: string
|
||||
description: "What file needs to be edited?"
|
||||
envvar:
|
||||
type: string
|
||||
description: "What environment variable needs to be edited?"
|
||||
envvalue:
|
||||
type: string
|
||||
description: "What environment variable value needs to be used?"
|
||||
permissions:
|
||||
contents: write
|
||||
pull-requests: write
|
||||
|
||||
env:
|
||||
INPUT_FILEPATH: ${{ github.event.inputs.filepath }}
|
||||
INPUT_ENVVAR: ${{ github.event.inputs.envvar }}
|
||||
INPUT_ENVVALUE: ${{ github.event.inputs.envvalue }}
|
||||
|
||||
jobs:
|
||||
tag:
|
||||
name: Replace environment variable in file
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v2
|
||||
- name: Replace tags
|
||||
run: |
|
||||
bash ./scripts/replace-env-value-in-file.sh ${INPUT_FILEPATH} ${INPUT_ENVVAR} ${INPUT_ENVVALUE}
|
||||
- name: Check for repository changes
|
||||
run: |
|
||||
if git diff --name-only --exit-code; then
|
||||
echo "No changes found in repository"
|
||||
echo "changes_exist=false" >> $GITHUB_ENV
|
||||
else
|
||||
echo "Changes found in repository"
|
||||
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-replace-envvar-$(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 "change env var ${INPUT_ENVVAR} to ${INPUT_ENVVALUE} [CI SKIP]"
|
||||
git push origin "$BRANCH"
|
||||
- name: Create Pull Request
|
||||
if: ${{ env.changes_exist == 'true' }}
|
||||
id: cpr
|
||||
uses: actions/github-script@v5.0.0
|
||||
env:
|
||||
SOURCE_BRANCH: ${{ steps.branch.outputs.branch }}
|
||||
with:
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
script: |
|
||||
const { INPUT_ENVVAR, INPUT_ENVVALUE } = process.env
|
||||
|
||||
const { data: pr } = await github.rest.pulls.create({
|
||||
title: `[${{ github.ref_name }}] change env var ${INPUT_ENVVAR} to ${INPUT_ENVVALUE}`,
|
||||
body: 'Auto-generated by GitHub Actions',
|
||||
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"],
|
||||
});
|
||||
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 }}"
|
||||
echo "::notice file=.github,line=1,col=1::Pull Request URL - ${{ steps.cpr.outputs.result }}"
|
||||
18
scripts/replace-env-value-in-file.sh
Executable file
18
scripts/replace-env-value-in-file.sh
Executable file
@@ -0,0 +1,18 @@
|
||||
if [ $# -ne 3 ]; then
|
||||
echo "Usage: $0 <filepath> <env_var_to_replace> <new_value>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
FILEPATH=$1
|
||||
REPLACE_ENV=$2
|
||||
REPLACE_VALUE=$3
|
||||
|
||||
TMPFILE=$(mktemp)
|
||||
|
||||
# Check if equal sign is present
|
||||
if grep "ENV ${REPLACE_ENV}" $FILEPATH | grep -q '='; then
|
||||
awk -F'=' 'BEGIN {OFS = FS} /ENV '"${REPLACE_ENV}"'/{$NF="'"${REPLACE_VALUE}"'"} 1' $FILEPATH > $TMPFILE && mv $TMPFILE $FILEPATH
|
||||
else
|
||||
awk '/ENV '"${REPLACE_ENV}"'/{$NF="'"${REPLACE_VALUE}"'"} 1' $FILEPATH > $TMPFILE && mv $TMPFILE $FILEPATH
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user