Skip to content

Commit 7a1a573

Browse files
committed
EML-000: add support for gitops dev branch
1 parent 49e76ac commit 7a1a573

6 files changed

Lines changed: 42 additions & 21 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ When you want to use this GitHub Action your GitHub repository should have a `de
1010
should use tags for
1111
releases.
1212

13-
- For the `dev` branch we will change the files specified under `gitops-dev`.
14-
- For the `master` / `main` branch we will change the files specified under `gitops-stage`.
15-
- For a new tag the files under `gitops-prod` will be used.
13+
- For the `dev` branch we will change the files specified under `gitops-dev`. These changes are committed to the `dev` branch of the GitOps repository.
14+
- For the `master` / `main` branch we will change the files specified under `gitops-stage`. These changes are committed to the default branch (`main`) of the GitOps repository.
15+
- For a new tag the files under `gitops-prod` will be used. These changes are committed to the default branch (`main`) of the GitOps repository.
1616

1717
This GitOps setup should be the default for all your repositories. However, if you have a special case, you can
1818
leave `gitops-dev`, `gitops-stage` and `gitops-prod` undefined, then those steps will be skipped.

action.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ inputs:
7070
description: 'GitHub Repository for GitOps'
7171
required: true
7272
default: 'mops'
73-
gitops-repository-branch:
74-
description: 'GitHub Repository Branch for GitOps'
75-
required: true
76-
default: 'main'
7773
gitops-user:
7874
description: 'GitHub User for GitOps'
7975
required: true
@@ -188,7 +184,6 @@ runs:
188184
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
189185
with:
190186
repository: ${{ inputs.gitops-organization }}/${{ inputs.gitops-repository }}
191-
ref: ${{ inputs.gitops-repository-branch }}
192187
token: ${{ inputs.gitops-token }}
193188
path: .github/${{ inputs.gitops-repository }}
194189

@@ -207,7 +202,6 @@ runs:
207202
INPUT_GITOPS_TOKEN: ${{ inputs.gitops-token }}
208203
INPUT_GITOPS_ORGANIZATION: ${{ inputs.gitops-organization }}
209204
INPUT_GITOPS_REPOSITORY: ${{ inputs.gitops-repository }}
210-
INPUT_GITOPS_REPOSITORY_BRANCH: ${{ inputs.gitops-repository-branch }}
211205
INPUT_GITOPS_DEV: ${{ inputs.gitops-dev }}
212206
INPUT_GITOPS_STAGE: ${{ inputs.gitops-stage }}
213207
INPUT_GITOPS_PROD: ${{ inputs.gitops-prod }}

scripts/lib/gitops-functions.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
# INPUT_DOCKER_REGISTRY, INPUT_DOCKER_IMAGE, INPUT_TAG, INPUT_PUSH,
77
# INPUT_GITOPS_USER, INPUT_GITOPS_TOKEN,
88
# INPUT_GITOPS_ORGANIZATION, INPUT_GITOPS_REPOSITORY,
9-
# INPUT_GITOPS_REPOSITORY_BRANCH,
9+
# GITOPS_BRANCH (target branch on the GitOps repo, e.g. main / dev),
1010
# GITHUB_REPOSITORY, GITHUB_SHA, IMAGE
1111

1212
push_to_gitops_repo() {
13-
git pull --rebase "https://${INPUT_GITOPS_USER}:${INPUT_GITOPS_TOKEN}@github.com/${INPUT_GITOPS_ORGANIZATION}/${INPUT_GITOPS_REPOSITORY}.git ${INPUT_GITOPS_REPOSITORY_BRANCH}"
14-
git push "https://${INPUT_GITOPS_USER}:${INPUT_GITOPS_TOKEN}@github.com/${INPUT_GITOPS_ORGANIZATION}/${INPUT_GITOPS_REPOSITORY}.git ${INPUT_GITOPS_REPOSITORY_BRANCH}:${INPUT_GITOPS_REPOSITORY_BRANCH}"
13+
local branch="${GITOPS_BRANCH:-main}"
14+
git pull --rebase "https://${INPUT_GITOPS_USER}:${INPUT_GITOPS_TOKEN}@github.com/${INPUT_GITOPS_ORGANIZATION}/${INPUT_GITOPS_REPOSITORY}.git" "${branch}"
15+
git push "https://${INPUT_GITOPS_USER}:${INPUT_GITOPS_TOKEN}@github.com/${INPUT_GITOPS_ORGANIZATION}/${INPUT_GITOPS_REPOSITORY}.git" "HEAD:${branch}"
1516
}
1617

1718
commit_changes() {

scripts/update-gitops.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
# Required env vars: GITHUB_REF, GITHUB_SHA, GITHUB_REPOSITORY,
77
# INPUT_DOCKER_REGISTRY, INPUT_DOCKER_IMAGE, INPUT_TAG, INPUT_PUSH,
88
# INPUT_GITOPS_USER, INPUT_GITOPS_EMAIL,
9-
# INPUT_GITOPS_TOKEN, INPUT_GITOPS_ORGANIZATION, INPUT_GITOPS_REPOSITORY,
10-
# INPUT_GITOPS_REPOSITORY_BRANCH
9+
# INPUT_GITOPS_TOKEN, INPUT_GITOPS_ORGANIZATION, INPUT_GITOPS_REPOSITORY
1110
# Optional env vars: INPUT_GITOPS_DEV, INPUT_GITOPS_STAGE, INPUT_GITOPS_PROD
1211

1312
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -25,12 +24,16 @@ require_env INPUT_GITOPS_EMAIL
2524
require_env INPUT_GITOPS_TOKEN
2625
require_env INPUT_GITOPS_ORGANIZATION
2726
require_env INPUT_GITOPS_REPOSITORY
28-
require_env INPUT_GITOPS_REPOSITORY_BRANCH
2927

3028
# Used by gitops-functions.sh (process_file_updates -> update_file)
3129
# shellcheck disable=SC2034
3230
IMAGE="${INPUT_DOCKER_REGISTRY}/${INPUT_DOCKER_IMAGE}:${INPUT_TAG}"
3331

32+
# Branch on the GitOps repo to commit & push to.
33+
# Defaults to "main"; DEV updates target the "dev" branch.
34+
# shellcheck disable=SC2034
35+
GITOPS_BRANCH="main"
36+
3437
# Configure git user
3538
git config --global user.email "${INPUT_GITOPS_EMAIL}" && git config --global user.name "${INPUT_GITOPS_USER}"
3639

@@ -40,6 +43,9 @@ if [[ ( $GITHUB_REF == refs/heads/master || $GITHUB_REF == refs/heads/main ) &&
4043

4144
elif [[ $GITHUB_REF == refs/heads/dev && -n "${INPUT_GITOPS_DEV:-}" ]]; then
4245
log_info "Run update for DEV"
46+
GITOPS_BRANCH="dev"
47+
git fetch origin dev
48+
git checkout -B dev origin/dev
4349
process_file_updates "$INPUT_GITOPS_DEV" "true"
4450

4551
elif [[ $GITHUB_REF == refs/tags/* && -n "${INPUT_GITOPS_PROD:-}" ]]; then

tests/lib-gitops-functions.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ setup() {
1717
export INPUT_GITOPS_TOKEN="fake-token"
1818
export INPUT_GITOPS_ORGANIZATION="Staffbase"
1919
export INPUT_GITOPS_REPOSITORY="mops"
20-
export INPUT_GITOPS_REPOSITORY_BRANCH="main"
20+
export GITOPS_BRANCH="main"
2121
export IMAGE="registry.staffbase.com/my-service:main-abcdef12"
2222

2323
# Create mock yq

tests/update-gitops.bats

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ setup() {
1717
export INPUT_GITOPS_TOKEN="fake-token"
1818
export INPUT_GITOPS_ORGANIZATION="Staffbase"
1919
export INPUT_GITOPS_REPOSITORY="mops"
20-
export INPUT_GITOPS_REPOSITORY_BRANCH="main"
2120
export INPUT_GITOPS_DEV=""
2221
export INPUT_GITOPS_STAGE=""
2322
export INPUT_GITOPS_PROD=""
@@ -114,17 +113,38 @@ teardown() {
114113
! grep -q 'git commit' "${TEST_TEMP_DIR}/git_calls.log" 2>/dev/null || true
115114
}
116115

117-
# --- Simulate on gitops branch ---
116+
# --- GitOps target branch ---
118117

119-
@test "updates DEV update on DEV devops branch" {
118+
@test "DEV update fetches and pushes to dev branch on gitops repo" {
120119
export GITHUB_REF="refs/heads/dev"
121-
export INPUT_GITOPS_REPOSITORY_BRANCH="dev"
122120
export INPUT_GITOPS_DEV="kubernetes/namespaces/svc/dev/de1/deploy.yaml spec.image"
123121
run "$SCRIPT"
124122

125123
assert_success
126124
assert_output --partial "Run update for DEV"
127-
grep -q "${INPUT_GITOPS_REPOSITORY_BRANCH}" "${TEST_TEMP_DIR}/git_calls.log" 2>/dev/null || true
125+
grep -q "git fetch origin dev" "${TEST_TEMP_DIR}/git_calls.log"
126+
grep -q "git checkout -B dev origin/dev" "${TEST_TEMP_DIR}/git_calls.log"
127+
grep -q "git pull --rebase .* dev$" "${TEST_TEMP_DIR}/git_calls.log"
128+
grep -q "git push .* HEAD:dev" "${TEST_TEMP_DIR}/git_calls.log"
129+
}
130+
131+
@test "STAGE update pushes to main branch on gitops repo" {
132+
export GITHUB_REF="refs/heads/main"
133+
export INPUT_GITOPS_STAGE="kubernetes/namespaces/svc/stage/de1/deploy.yaml spec.image"
134+
run "$SCRIPT"
135+
136+
assert_success
137+
grep -q "git push .* HEAD:main" "${TEST_TEMP_DIR}/git_calls.log"
138+
! grep -q "git checkout -B dev" "${TEST_TEMP_DIR}/git_calls.log"
139+
}
140+
141+
@test "PROD update pushes to main branch on gitops repo" {
142+
export GITHUB_REF="refs/tags/v1.0.0"
143+
export INPUT_GITOPS_PROD="kubernetes/namespaces/svc/prod/de1/deploy.yaml spec.image"
144+
run "$SCRIPT"
145+
146+
assert_success
147+
grep -q "git push .* HEAD:main" "${TEST_TEMP_DIR}/git_calls.log"
128148
}
129149

130150
# --- No files configured ---

0 commit comments

Comments
 (0)