Featuer/gitops extra command #47
@@ -17,6 +17,9 @@ on:
|
||||
GITOPS_REPO:
|
||||
required: true
|
||||
type: string
|
||||
GITOPS_EXTRA_CMD:
|
||||
required: false
|
||||
type: string
|
||||
secrets:
|
||||
GITOPS_DISPATCH_TOKEN:
|
||||
required: true
|
||||
@@ -34,6 +37,7 @@ env:
|
||||
GITOPS_SOURCE_COMMIT: ${{ github.sha }}
|
||||
GITEA_API_URL: ${{ fromJson(inputs.env_json).GITEA_API_URL }}
|
||||
GITOPS_TAG_PREFIX: ${{ fromJson(inputs.env_json).GIT_TAG_PREFIX || '' }}
|
||||
GITOPS_EXTRA_CMD: ${{ inputs.GITOPS_EXTRA_CMD || '' }}
|
||||
GITOPS_WORKFLOW: gitops-service.yaml
|
||||
GITOPS_DISPATCH_TIMEOUT: 30
|
||||
|
||||
@@ -61,7 +65,8 @@ jobs:
|
||||
--arg source_repo "$GITOPS_SOURCE_REPO" \
|
||||
--arg source_commit "$GITOPS_SOURCE_COMMIT" \
|
||||
--arg git_tag_prefix "${GITOPS_TAG_PREFIX:-}" \
|
||||
'{dispatch_id: $dispatch_id, file: $file, yq_tpl: $yq_tpl, version: $version, source_repo: $source_repo, source_commit: $source_commit, git_tag_prefix: $git_tag_prefix}')
|
||||
--arg extra_cmd "${GITOPS_EXTRA_CMD:-}" \
|
||||
'{dispatch_id: $dispatch_id, file: $file, yq_tpl: $yq_tpl, version: $version, source_repo: $source_repo, source_commit: $source_commit, git_tag_prefix: $git_tag_prefix, extra_cmd: $extra_cmd}')
|
||||
curl -s -X POST \
|
||||
"${GITEA_API_URL}/api/v1/repos/${GITOPS_REPO}/actions/workflows/${GITOPS_WORKFLOW}/dispatches" \
|
||||
-H "Authorization: token $GITEA_TOKEN" \
|
||||
|
||||
@@ -22,7 +22,8 @@ INPUTS=$(jq -nc \
|
||||
--arg source_repo "$GITOPS_SOURCE_REPO" \
|
||||
--arg source_commit "$GITOPS_SOURCE_COMMIT" \
|
||||
--arg git_tag_prefix "${GITOPS_TAG_PREFIX:-}" \
|
||||
'{file: $file, yq_tpl: $yq_tpl, version: $version, source_repo: $source_repo, source_commit: $source_commit, git_tag_prefix: $git_tag_prefix}')
|
||||
--arg extra_cmd "${GITOPS_EXTRA_CMD:-}" \
|
||||
'{file: $file, yq_tpl: $yq_tpl, version: $version, source_repo: $source_repo, source_commit: $source_commit, git_tag_prefix: $git_tag_prefix, extra_cmd: $extra_cmd}')
|
||||
|
||||
DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
echo "gitops-dispatch: dispatching to $GITOPS_REPO/$GITOPS_WORKFLOW..."
|
||||
|
||||
@@ -84,7 +84,12 @@ _gitops_update() {
|
||||
|
||||
cd "${CLONE_DIR}" || _gitops_fail "Failed to enter clone directory"
|
||||
yq eval -i "${YQ_EXPR}" "${INPUT_FILE}" || _gitops_fail "Failed to update ${INPUT_FILE}"
|
||||
if [ -n "${GITOPS_EXTRA_CMD:-}" ]; then
|
||||
eval "${GITOPS_EXTRA_CMD}" || _gitops_fail "Extra command failed: ${GITOPS_EXTRA_CMD}"
|
||||
git add -A || _gitops_fail "Failed to stage all changes"
|
||||
else
|
||||
git add "${INPUT_FILE}" || _gitops_fail "Failed to stage ${INPUT_FILE}"
|
||||
fi
|
||||
|
||||
if git diff --cached --quiet; then
|
||||
echo "No changes — ${INPUT_FILE} already at ${VERSION}"
|
||||
|
||||
@@ -56,6 +56,9 @@ on:
|
||||
git_tag_prefix:
|
||||
required: false
|
||||
type: string
|
||||
extra_cmd:
|
||||
required: false
|
||||
type: string
|
||||
|
||||
env:
|
||||
INPUT_FILE: ${{ inputs.file }}
|
||||
@@ -66,6 +69,7 @@ env:
|
||||
GITOPS_REPO: ${{ github.repository }}
|
||||
GITEA_API_URL: ${{ gitea.server_url }}
|
||||
GIT_TAG_PREFIX: ${{ inputs.git_tag_prefix || '' }}
|
||||
GITOPS_EXTRA_CMD: ${{ inputs.extra_cmd || '' }}
|
||||
|
||||
jobs:
|
||||
update:
|
||||
@@ -130,15 +134,35 @@ gitops-update:
|
||||
|
||||
This single job handles: dispatch → poll → find commit SHA → set commit-status on your commit → produce `GITOPS_SUMMARY` output.
|
||||
|
||||
To run extra commands (e.g. `helm dependency update`) after the version bump and before the commit:
|
||||
|
||||
```yaml
|
||||
gitops-update:
|
||||
needs: [load-config, check-version, helm-build-push]
|
||||
if: success()
|
||||
uses: niko/gitea-ci-library/.gitea/workflows/gitops-dispatch.yml@v1
|
||||
secrets: inherit
|
||||
with:
|
||||
env_json: ${{ needs.load-config.outputs.env_json }}
|
||||
version: ${{ needs.check-version.outputs.version }}
|
||||
GITOPS_FILE: Chart.yaml
|
||||
GITOPS_YQ_TPL: '(.dependencies[] | select(.name == "agent-platform-helm") | .version) = "{{VERSION}}"'
|
||||
GITOPS_REPO: niko/agent-platform-gitops
|
||||
GITOPS_EXTRA_CMD: helm dependency update
|
||||
```
|
||||
|
||||
When `GITOPS_EXTRA_CMD` is set, the script runs it after `yq` and stages all changes (`git add -A`) instead of only the input file — so any files generated by the extra command (e.g. `Chart.lock`, `charts/`) are included in the commit.
|
||||
|
||||
### 2.3 Parameters
|
||||
|
||||
| Input | Required | Description |
|
||||
|---|---|---|
|
||||
|---|---|---|---|
|
||||
| `env_json` | Yes | Config JSON with `GITEA_API_URL`, optional `GIT_TAG_PREFIX` (for multi-component repos) |
|
||||
| `version` | Yes | Version to write (e.g. `0.2.3`) |
|
||||
| `GITOPS_FILE` | Yes | Path in GitOps repo (e.g. `dev/Chart.yaml`) |
|
||||
| `GITOPS_YQ_TPL` | Yes | yq expression, `{{VERSION}}` is replaced at runtime |
|
||||
| `GITOPS_REPO` | Yes | GitOps repo slug (e.g. `niko/agent-platform-gitops`) |
|
||||
| `GITOPS_EXTRA_CMD` | No | Shell command to run after yq update, before git commit (e.g. `helm dependency update`) |
|
||||
|
||||
### 2.4 Output
|
||||
|
||||
@@ -177,7 +201,7 @@ report-summary:
|
||||
## 4. What happens at runtime
|
||||
|
||||
1. Consumer's `gitops-dispatch.yml` generates a unique `dispatch_id` and POSTs it to the GitOps repo
|
||||
2. GitOps workflow clones its own repo, applies `yq`, commits + pushes
|
||||
2. GitOps workflow clones its own repo, applies `yq`, runs `GITOPS_EXTRA_CMD` if set, then commits + pushes
|
||||
3. Consumer polls the GitOps repo's runs until the workflow completes
|
||||
4. Consumer lists recent commits and finds the matching one by commit message `"gitops: update version to X.Y.Z"`
|
||||
5. Consumer sets commit-status `gitops/{repo}[/{prefix}]` on its own commit with a link to the exact GitOps commit
|
||||
|
||||
Reference in New Issue
Block a user