Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: ensure we store the versions as gh variables #1405

Merged
merged 1 commit into from
Nov 6, 2024

Conversation

arealmaas
Copy link
Collaborator

@arealmaas arealmaas commented Nov 6, 2024

Description

Related Issue(s)

Fixes issues in the current workflow regarding missing .git-directory https://github.com/digdir/dialogporten/actions/runs/11686732981/job/32543782272

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)

Documentation

  • Documentation is updated (either in docs-directory, Altinnpedia or a separate linked PR in altinn-studio-docs., if applicable)

Summary by CodeRabbit

  • New Features

    • Introduced a new workflow for storing GitHub environment variables, enhancing version management.
    • Updated deployment jobs to conditionally execute based on detected changes, improving deployment efficiency.
  • Bug Fixes

    • Improved error handling and clarity in the CI/CD process.
  • Documentation

    • Added documentation for the new workflow to assist users in understanding its purpose and usage.

@arealmaas arealmaas requested review from a team as code owners November 6, 2024 10:08
Copy link
Contributor

coderabbitai bot commented Nov 6, 2024

📝 Walkthrough

Walkthrough

The changes involve significant updates to CI/CD workflow configurations across multiple environments, including production, staging, and YT01. Key modifications include the refactoring of jobs such as store-infra-version and store-apps-version to utilize a new reusable workflow for managing GitHub environment variables. Additionally, conditional execution logic has been enhanced for deployment jobs, ensuring they only run when relevant changes are detected. A new workflow file for storing environment variables has also been introduced, streamlining the process.

Changes

File Path Change Summary
.github/workflows/ci-cd-prod.yml Updated store-infra-version and store-apps-version to use uses with workflow-store-github-env-variable.yml. Enhanced conditional logic for deployment jobs based on changes.
.github/workflows/ci-cd-staging.yml Modified store-infra-version and store-apps-version to use uses with workflow-store-github-env-variable.yml. Refined conditions for deployment jobs based on changes.
.github/workflows/ci-cd-yt01.yml Refactored store-infra-version and store-apps-version to use uses with workflow-store-github-env-variable.yml. Updated conditions for publish, deploy-infra, and deploy-apps.
.github/workflows/workflow-store-github-env-variable.yml Introduced a new workflow for storing GitHub environment variables with parameters for variable_name, variable_value, and environment.

Possibly related PRs

Suggested reviewers

  • oskogstad
  • elsand

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

sonarqubecloud bot commented Nov 6, 2024

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (3)
.github/workflows/workflow-store-github-env-variable.yml (1)

28-31: Consider removing unnecessary checkout step.

The workflow doesn't appear to use any repository content. The checkout step could be removed to improve workflow execution time.

.github/workflows/ci-cd-staging.yml (1)

100-105: LGTM - Consistent implementation with store-infra-version.

The implementation maintains consistency with the infra version storage pattern, which is good for maintainability.

Consider combining both version storage operations into a single job with multiple steps to reduce workflow execution time and complexity. This could be achieved by:

  1. Creating a new reusable workflow that handles both variables
  2. Using job outputs to pass both version values
  3. Executing the storage operations in parallel within the same job

Example structure:

store-versions:
  needs: [deploy-infra, deploy-apps, get-current-version]
  if: ${{ needs.deploy-infra.result == 'success' && needs.deploy-apps.result == 'success' }}
  uses: ./.github/workflows/workflow-store-github-env-variables.yml
  with:
    variables: |
      LATEST_DEPLOYED_INFRA_VERSION=${{ needs.get-current-version.outputs.version }}
      LATEST_DEPLOYED_APPS_VERSION=${{ needs.get-current-version.outputs.version }}
    environment: staging
.github/workflows/ci-cd-prod.yml (1)

74-79: LGTM! Well-structured version storage for infrastructure deployments.

The implementation correctly uses the reusable workflow pattern to store infrastructure versions, with proper environment isolation and secure token handling.

Consider adding a comment explaining the purpose of RELEASE_VERSION_STORAGE_PAT and its required permissions for future maintainers.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 117c707 and fd96ea2.

📒 Files selected for processing (4)
  • .github/workflows/ci-cd-prod.yml (2 hunks)
  • .github/workflows/ci-cd-staging.yml (2 hunks)
  • .github/workflows/ci-cd-yt01.yml (2 hunks)
  • .github/workflows/workflow-store-github-env-variable.yml (1 hunks)
🔇 Additional comments (7)
.github/workflows/workflow-store-github-env-variable.yml (3)

1-4: LGTM! Well-structured workflow definition.

The workflow name is descriptive and the trigger configuration is appropriate for a reusable workflow.


23-27: LGTM! Proper job configuration.

The job configuration follows best practices with latest runner and proper environment handling.


5-21: Consider adding input validation and explicit token permissions.

While the input and secret definitions are well-documented, consider these security enhancements:

  1. Add explicit permissions required for the GH_TOKEN in the description (e.g., "write:variables")
  2. Consider validating the variable_name format to prevent injection attempts

Let's check if there are any existing variable name validations in the codebase:

.github/workflows/ci-cd-yt01.yml (2)

66-71: LGTM! Well-structured refactor of the infrastructure version storage.

The change effectively utilizes the reusable workflow pattern, maintaining proper job dependencies while simplifying the version storage process.


102-107: LGTM! Well-structured refactor of the apps version storage.

The change mirrors the infrastructure version storage pattern, ensuring consistency and reducing code duplication through the use of the reusable workflow.

.github/workflows/ci-cd-prod.yml (2)

134-139: LGTM! Consistent implementation for storing application versions.

The implementation maintains consistency with the infrastructure version storage pattern, ensuring a unified approach across the workflow.


Line range hint 74-139: Verify the reusable workflow implementation.

Let's verify the existence and implementation of the referenced reusable workflow.

✅ Verification successful

Reusable workflow implementation is correctly referenced and properly implemented

The workflow .github/workflows/workflow-store-github-env-variable.yml exists and implements all the required functionality:

  • Accepts required inputs: variable_name, variable_value, and environment
  • Requires the necessary GH_TOKEN secret
  • Uses the GitHub CLI to set environment variables with proper scoping
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the reusable workflow exists and check its implementation
echo "Checking for workflow-store-github-env-variable.yml..."
if [ -f ".github/workflows/workflow-store-github-env-variable.yml" ]; then
    echo "Found workflow file. Checking implementation..."
    cat ".github/workflows/workflow-store-github-env-variable.yml"
else
    echo "ERROR: Referenced workflow file not found!"
    # List available workflow files for reference
    echo "Available workflow files:"
    find .github/workflows -type f -name "*.yml"
fi

Length of output: 1431

Comment on lines +32 to +39
- name: Set GitHub variable
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
gh variable set ${{ inputs.variable_name }} \
--body "${{ inputs.variable_value }}" \
--env ${{ inputs.environment }} \
--repo ${{ github.repository }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling and validation.

The current implementation could benefit from these improvements:

  1. Add error handling for the gh CLI command
  2. Validate that the variable was set successfully

Consider this enhanced implementation:

   - name: Set GitHub variable
     env:
       GH_TOKEN: ${{ secrets.GH_TOKEN }}
     run: |
+      if ! command -v gh &> /dev/null; then
+        echo "Error: GitHub CLI not found"
+        exit 1
+      fi
+
       gh variable set ${{ inputs.variable_name }} \
         --body "${{ inputs.variable_value }}" \
         --env ${{ inputs.environment }} \
         --repo ${{ github.repository }}
+
+      # Verify the variable was set
+      if ! gh variable list --env ${{ inputs.environment }} | grep -q "^${{ inputs.variable_name }} "
+      then
+        echo "Error: Failed to verify variable was set"
+        exit 1
+      fi
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- name: Set GitHub variable
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
gh variable set ${{ inputs.variable_name }} \
--body "${{ inputs.variable_value }}" \
--env ${{ inputs.environment }} \
--repo ${{ github.repository }}
- name: Set GitHub variable
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
if ! command -v gh &> /dev/null; then
echo "Error: GitHub CLI not found"
exit 1
fi
gh variable set ${{ inputs.variable_name }} \
--body "${{ inputs.variable_value }}" \
--env ${{ inputs.environment }} \
--repo ${{ github.repository }}
# Verify the variable was set
if ! gh variable list --env ${{ inputs.environment }} | grep -q "^${{ inputs.variable_name }} "
then
echo "Error: Failed to verify variable was set"
exit 1
fi

Comment on lines +55 to +60
uses: ./.github/workflows/workflow-store-github-env-variable.yml
with:
variable_name: LATEST_DEPLOYED_INFRA_VERSION
variable_value: ${{ needs.get-current-version.outputs.version }}
environment: staging
secrets:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Workflow improvements needed for better reliability

The reusable workflow lacks timeout configuration and explicit error handling. While environment protection is in place for staging (with branch policies), the workflow could be more robust.

  • Add timeout-minutes to the store-variable job in .github/workflows/workflow-store-github-env-variable.yml
  • Consider adding error handling by wrapping the variable set command in a conditional check
  • The current implementation correctly uses environment contexts and required inputs

Example fix for the reusable workflow:

jobs:
  store-variable:
    timeout-minutes: 5  # Prevent hanging jobs
    name: Store GitHub Environment Variable
    runs-on: ubuntu-latest
    environment: ${{ inputs.environment }}
🔗 Analysis chain

LGTM with suggestions for enhanced reliability.

The transition to a reusable workflow for storing GitHub variables is a good improvement for maintainability.

Consider these enhancements:

  1. Add a timeout to prevent hanging jobs: timeout-minutes: 5
  2. Add error handling by checking the workflow status in downstream jobs
  3. Verify environment protection rules are in place for the staging environment variables
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify environment protection rules for staging
gh api -H "Accept: application/vnd.github+json" \
  /repos/digdir/dialogporten/environments/staging \
  --jq '.protection_rules[]'

Length of output: 195


Script:

#!/bin/bash
# Get more details about the branch protection rules
gh api -H "Accept: application/vnd.github+json" \
  /repos/digdir/dialogporten/environments/staging \
  --jq '.'

Length of output: 629


Script:

#!/bin/bash
# Check the reusable workflow for timeout and error handling
cat .github/workflows/workflow-store-github-env-variable.yml

Length of output: 1137


Script:

#!/bin/bash
# Check if there are any other jobs depending on store-infra-version
rg "needs.*store-infra-version" .github/workflows/ -A 5

Length of output: 57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant