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

Adds new RC012 that checks how environment variables passed as parameters are being used #247

Merged
merged 1 commit into from
Dec 18, 2024

Conversation

david-montano-circleci
Copy link
Contributor

@david-montano-circleci david-montano-circleci commented Dec 17, 2024

Summary

This PR is meant to prevent errors like this from happening again.
Tackles issue #246 .

Steps

  1. identify which parameters are defined as Environment Variables
  2. search where those parameters are being used directly inside key entries in the yml files
  3. show the user information about incorrect usage of those parameters

Tests

Using a yml like the following:

description: "Save Go modules to cache."
parameters:
  key:
    description: "User-configurable component for cache key. Useful for avoiding collisions in complex workflows."
    type: string
    default: ""
  path:
    description: "Path to cache."
    type: string
    # /home/circleci/go is the GOPATH in the cimg/go Docker image
    default: "/home/circleci/go/pkg/mod"
  project-path:
    description: |
      The path to the directory containing your Go project files: go.mod, go.sum.
      Defaults to $CIRCLE_WORKING_DIRECTORY.
    type: string
    default: $CIRCLE_WORKING_DIRECTORY
steps:
  - save_cache:
      key: v1-<< parameters.key >>-go-mod-{{ arch }}-{{ checksum "go.sum" }}
      key: v1-<< parameters.key >>-go-mod-{{ arch }}-{{ checksum "<< parameters.project-path >>/go.sum" }}
      paths:
        - << parameters.path >>
  - run:
      name: thing
      command: echo "<< parameters.project-path >>"
  - run: echo "<< parameters.project-path >>"

The review step outputs this:

$ bats -f "RC012"  --show-output-of-passing-tests src/scripts/review.bats

review.bats
 ✗ RC012: Incorrect usage of environment variables set as default parameter detected.
   (in test file src/scripts/review.bats, line 324)
     `exit 1' failed
   
   File: "./src/jobs/david.yml"
   Parameter definition line: 17
   Parameter name: project-path
   Parameter value: $CIRCLE_WORKING_DIRECTORY
   
   The parameter "project-path" was initialized as an Environment Variable: "$CIRCLE_WORKING_DIRECTORY"
   It is being used by an orb key parameter ("steps.0.save_cache.key") in line 21 which is not permitted.
   ---
   v1-<< parameters.key >>-go-mod-{{ arch }}-{{ checksum "<< parameters.project-path >>/go.sum" }}
   ---
   
   "default" parameters referencing environment variables cannot be used directly in key properties.

1 test, 1 failure

@orb-publisher
Copy link

Your development orb(s) have been published. It will expire in 90 days.
You can preview what this will look like on the CircleCI Orb Registry at the following link(s):
https://circleci.com/developer/orbs/orb/circleci/orb-tools?version=dev:587ac32c011305edccbd02894bab9816c7701da0
https://circleci.com/developer/orbs/orb/circleci/orb-tools?version=dev:alpha

@david-montano-circleci david-montano-circleci merged commit 4aa8d7c into master Dec 18, 2024
2 checks passed
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.

3 participants