Skip to content

Commit 765d3b7

Browse files
committed
Initial commit
0 parents  commit 765d3b7

File tree

7 files changed

+254
-0
lines changed

7 files changed

+254
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 kustomize-everything
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# action-gitops
2+
3+
Provides standard GitOps on a Kustomize Environment overlay with GitHub Actions.
4+
Intended to be run by a bot in order to allow DevOps teams to perform Ops quickly
5+
and easily via automated PRs with reduced review requirements for common operations
6+
like scaling replicas.
7+
8+
## Usage
9+
10+
### Pre-requisites
11+
12+
- Github repo where your Kustomize deployment files reside e.g. [kustomize-everything/guestbook-deploy](https://github.com/kustomize-everything/guestbook-deploy)
13+
14+
### Inputs
15+
16+
Refer to [action.yml](./action.yml)
17+
18+
### Outputs
19+
20+
Refer to [action.yml](./action.yml)
21+
22+
### Example Workflow
23+
24+
```yaml
25+
---
26+
name: Ops
27+
on:
28+
workflow_dispatch:
29+
inputs:
30+
service-name:
31+
description: 'Service Name'
32+
type: choice
33+
options:
34+
- my-service
35+
required: true
36+
replicas:
37+
description: 'Desired Number of Replicas'
38+
required: true
39+
environment:
40+
description: 'Environment'
41+
type: choice
42+
options:
43+
- integration
44+
- staging
45+
- production
46+
default: 'integration'
47+
required: true
48+
replica-selector:
49+
description: 'Replica Selector (defaults to Service Name if not provided)'
50+
required: false
51+
52+
jobs:
53+
op:
54+
name: Adjust Replicas for Service
55+
runs-on: ubuntu-latest
56+
steps:
57+
- name: Open PR to Adjust Replicas
58+
uses: kustomize-everything/action-gitops@main
59+
with:
60+
service-name: ${{ github.event.inputs.service-name }}
61+
selector: ${{ github.event.inputs.replica-selector }}
62+
environment:
63+
```
64+
65+
## Contributing
66+
67+
We would love for you to contribute to kustomize-everything/actions-env-build-and-deploy, pull requests are welcome!
68+
69+
## License
70+
71+
The scripts and documentation in this project are released under the [MIT License](LICENSE).

action.yml

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
name: action-gitops
3+
description: |
4+
This action provides a standard way of adjusting replicas in a kustomize
5+
deployment and opening a PR to deploy the changes to a k8s cluster.
6+
7+
Other workflows may be added to this action in the future.
8+
9+
inputs:
10+
operation:
11+
description: The operation to run (adjust-replicas, etc)
12+
required: true
13+
environment:
14+
description: The environment to perform the operation on
15+
required: true
16+
service-name:
17+
description: The name of the service to perform the operation on
18+
required: true
19+
version:
20+
description: Version of Kustomize to use
21+
required: false
22+
default: 5.1.1
23+
sha256-checksum:
24+
description: Checksum of Kustomize version
25+
required: false
26+
default: 3b30477a7ff4fb6547fa77d8117e66d995c2bdd526de0dafbf8b7bcb9556c85d
27+
git-commit-user:
28+
description: Name to add to the Git Commit Message
29+
required: false
30+
default: Kustomize Everything
31+
git-commit-email:
32+
description: Email to add to the Git Commit Message
33+
required: false
34+
default: kustomize-everything@users.noreply.github.com
35+
token:
36+
description: |
37+
The token to use for pushing to the repo. This should be a bot account
38+
with write access to the target repo, as well as PR access if it needs to
39+
open a PR.
40+
required: true
41+
helm-version:
42+
description: Helm Version to install
43+
default: 3.9.4
44+
base-branch:
45+
description: The branch to open the PR against
46+
default: main
47+
replica_selector:
48+
description: The selector to use for adjusting replicas, if not provided, the service-name will be used
49+
runs:
50+
using: composite
51+
steps:
52+
- name: Checkout deployment repo
53+
uses: actions/checkout@v4
54+
with:
55+
fetch-depth: 0
56+
ref: main
57+
token: ${{ inputs.token }}
58+
59+
# These variables are re-used by the run steps.
60+
- name: Shared ENV Setup
61+
shell: bash
62+
working-directory: ${{ inputs.working-directory }}
63+
env:
64+
ENV: ${{ inputs.environment }}
65+
PUSH_ENVIRONMENT_REGEX: ${{ steps.detect-push-environment.outputs.match }}
66+
PR_ENVIRONMENT_REGEX: ${{ steps.detect-pr-environment.outputs.match }}
67+
run: ${{ github.action_path }}/setup-shared-env.sh
68+
69+
# Kustomize setup (this should be abstracted into a separate action repo)
70+
- name: Kustomize Setup
71+
uses: kustomize-everything/action-kustomize@v2.0.2
72+
with:
73+
version: ${{ inputs.version }}
74+
sha256-checksum: ${{ inputs.sha256-checksum }}
75+
76+
- name: Install yq
77+
uses: mikefarah/yq@v4.35.1
78+
79+
- name: Set Git Author
80+
shell: bash
81+
run: |
82+
git config --global user.name ${{ inputs.git-commit-user }}
83+
git config --global user.email ${{ inputs.git-commit-email }}
84+
85+
- name: Adjust Replicas
86+
shell: bash
87+
working-directory: ${{ inputs.working-directory }}
88+
if: ${{ inputs.operation == 'adjust-replicas' }}
89+
env:
90+
SELECTOR: ${{ github.event.inputs.replica_selector || github.event.inputs.service_name }}
91+
run: ${{ github.action_path }}/adjust-replicas.sh
92+
93+
- name: Open ${{ inputs.operation }} PR to ${{ inputs.environment }}
94+
id: open-pr
95+
uses: peter-evans/create-pull-request@v5
96+
with:
97+
title: ${{ inputs.operation }} on ${{ inputs.environment }}
98+
body: "${{ inputs.operation }} for ${{ env.SELECTOR }} to ${{ inputs.replicas }} in ${{ inputs.environment }}"
99+
base: ${{ input.base-branch }}
100+
branch: ${{ env.BRANCH_NAME }}
101+
delete-branch: true
102+
token: ${{ inputs.token }}
103+
104+
- name: PR Opened or Updated
105+
if: ${{ steps.open-pr.outputs.pull-request-number && (steps.open-pr.outputs.pull-request-operation == 'created' || steps.open-pr.outputs.pull-request-operation == 'updated') }}
106+
shell: bash
107+
run: |
108+
echo "The ${{ inputs.operation }} PR for ${{ inputs.environment }} is waiting for
109+
deployment after PR review and merge."
110+
echo "Please review the k8s manifests in this PR and merge if ready
111+
to deploy to ${{ inputs.environment }}."
112+
echo "${{ steps.open-pr.outputs.pull-request-url }}"
113+
114+
- name: PR Closed
115+
if: ${{ steps.open-pr.outputs.pull-request-number && steps.open-pr.outputs.pull-request-operation == 'closed' }}
116+
shell: bash
117+
run: |-
118+
echo "The ${{ inputs.operation }} PR for ${{ inputs.environment }} has been closed as
119+
there are no changes detected."
120+
echo "${{ steps.open-pr.outputs.pull-request-url }}"

adjust-replicas.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
source "${GITHUB_ACTION_PATH}/util.sh"
4+
5+
# Fail on non-zero exit
6+
set -e
7+
8+
echo "SELECTOR=$SELECTOR" >> $GITHUB_ENV
9+
yq e ".replicas[] |= select(.name == \"$SELECTOR\").count = ${REPLICAS}" -i env/${ENVIRONMENT}/${SERVICE_NAME}/kustomization.yaml

renovate.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
3+
"extends": [
4+
"config:base"
5+
]
6+
}

setup-shared-env.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
source "${GITHUB_ACTION_PATH}/util.sh"
4+
5+
# Fail on non-zero exit
6+
set -e
7+
8+
echo "BRANCH_NAME=adjust-replicas-${{ env.SELECTOR }}-$(date +'%Y%m%d%H%M%S')" >> $GITHUB_ENV

util.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
3+
function is_debug() {
4+
if [[ "$RUNNER_DEBUG" == "1" ]]; then
5+
return 0
6+
else
7+
return 1
8+
fi
9+
}
10+
11+
# Output all commands
12+
if is_debug; then
13+
set -x
14+
fi
15+
16+
# Show line numbers
17+
if is_debug; then
18+
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
19+
fi

0 commit comments

Comments
 (0)