forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
84 lines (81 loc) · 3.1 KB
/
replace-env-value.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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}"
git push origin "$BRANCH"
- name: Create Pull Request
if: ${{ env.changes_exist == 'true' }}
id: cpr
uses: actions/[email protected]
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 }}"