From c0393811265bfe9b09d6a337ceb797cf48171184 Mon Sep 17 00:00:00 2001 From: Victor Lyuboslavsky Date: Tue, 7 May 2024 17:05:52 -0500 Subject: [PATCH] Dry-run only on PR (#36) --- .github/gitops-action/action.yml | 5 +++++ .github/workflows/workflow.yml | 9 +++++++++ gitops.sh | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/.github/gitops-action/action.yml b/.github/gitops-action/action.yml index 58ffa8b..6914748 100644 --- a/.github/gitops-action/action.yml +++ b/.github/gitops-action/action.yml @@ -5,6 +5,9 @@ inputs: working-directory: description: 'The working directory, which should be the root of the fleet-gitops repository.' default: './' + dry-run-only: + description: 'Whether to only run the fleetctl gitops commands in dry-run mode.' + default: 'false' runs: using: "composite" @@ -22,4 +25,6 @@ runs: - name: Run fleetctl gitops commands shell: bash working-directory: ${{ inputs.working-directory }} + env: + FLEET_DRY_RUN_ONLY: ${{ inputs.dry-run-only }} run: ./gitops.sh diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 215f985..8a96a6d 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -4,8 +4,14 @@ on: push: branches: - main + pull_request: workflow_dispatch: # allows manual triggering +# Prevent concurrent runs of this workflow. +concurrency: + group: ${{ github.workflow }} + cancel-in-progress: false + defaults: run: shell: bash @@ -23,6 +29,9 @@ jobs: - name: Apply latest configuration to Fleet uses: ./.github/gitops-action + with: + # Run GitOps in dry-run mode for pull requests. + dry-run-only: ${{ github.event_name == 'pull_request' && 'true' || 'false' }} # Add FLEET_URL and FLEET_API_TOKEN to the repository secrets. # In addition, specify or add secrets for all the environment variables that are mentioned in the global/team YAML files. env: diff --git a/gitops.sh b/gitops.sh index 8abda94..d3bb58e 100755 --- a/gitops.sh +++ b/gitops.sh @@ -9,6 +9,7 @@ set -exuo pipefail FLEET_GITOPS_DIR="${FLEET_GITOPS_DIR:-./}" FLEET_GLOBAL_FILE="${FLEET_GLOBAL_FILE:-$FLEET_GITOPS_DIR/default.yml}" FLEETCTL="${FLEETCTL:-fleetctl}" +FLEET_DRY_RUN_ONLY="${FLEET_DRY_RUN_ONLY:-false}" # Validate that global file contains org_settings grep -Exq "^org_settings:.*" "$FLEET_GLOBAL_FILE" @@ -26,6 +27,9 @@ for team_file in "$FLEET_GITOPS_DIR"/teams/*.yml; do $FLEETCTL gitops -f "$team_file" --dry-run fi done +if [ "$FLEET_DRY_RUN_ONLY" = true ]; then + exit 0 +fi # Real run $FLEETCTL gitops -f "$FLEET_GLOBAL_FILE"