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

feat: add jenkins support #611

Merged
merged 2 commits into from
May 3, 2021

Conversation

sinabakh
Copy link
Contributor

@sinabakh sinabakh commented Apr 23, 2021

The jenkins_diff.sh does relatively the same task as the other diff scripts, here is a list of changes:

  • path variable is changed to iac_path
  • Remove support for deprecated flags
  • Remove post_condition
  • Add fail_condition with the percentage_threshold field to fail the pipeline if the change is over the threshold
  • Add a function to wrap the diff output in a simple HTML page
  • Add few utility functions to make the script work with Jenkins
  • Diff-output is always printed in the output
  • Save the diff output HTML in a file for further use

Here's a sample Jenkinsfile config:

pipeline {
    agent any
    stages {
        stage('infracost-breakdown') {
            agent {
                docker {
                  image 'infracost-local'
                  args '--user=root --entrypoint='
                }
            }
            environment {
              INFRACOST_API_KEY = credentials('jenkins-infracost-api-key')
              IAC_PATH = 'terraform'
              FAIL_CONDITION = '{"percentage_threshold": 10}'
            }

            steps {
                sh '/scripts/ci/jenkins_diff.sh'

                publishHTML (target: [
                    	allowMissing: false,
      		    	alwaysLinkToLastBuild: false,
      			keepAll: true,
      			reportDir: './',
      			reportFiles: 'infracost_diff_output.html',
      			reportName: "Infracost Diff Output"
    		])

            }
        }
    }
}

Here's the pipeline's "Output consone":
Screen Shot 2021-04-23 at 9 46 53 PM

And the output get's its own sub-menu:
Screen Shot 2021-04-22 at 1 19 09 AM

Screen Shot 2021-04-23 at 9 49 46 PM

@sinabakh
Copy link
Contributor Author

It would be great if you could take a quick look at it for suggestions or changes before I write the actual documentation.
@alikhajeh1 @aliscott

@alikhajeh1 alikhajeh1 self-assigned this Apr 26, 2021
@alikhajeh1
Copy link
Member

Thanks @sinabakh! Will review this today :)

@alikhajeh1 alikhajeh1 assigned sinabakh and unassigned alikhajeh1 Apr 26, 2021
@alikhajeh1
Copy link
Member

This is great! To keep the style of fail_condition similar to post_condition, should we also have this option:
'{"never": true}': never fail the Jenkins build. This is the default behavior.

See this comment and the following 2 comments about the pros/cons of why we picked that style.

@sinabakh
Copy link
Contributor Author

Thanks for the review @alikhajeh1. Regarding the suggestion, I think it can't be applied to such a config. I mean, posting a comment all the time makes sense but failing a build is not something that somebody wants unless a specific condition is met. So adding the never is somehow a duplication.

Let me know if I'm missing a point or it would helo maintain the same standard.

@alikhajeh1
Copy link
Member

alikhajeh1 commented Apr 28, 2021

Good point! Makes sense. What would be the behavior if fail_condition:

  1. is not defined? I guess we'd never fail the build so there's no point in having an explicit never option.
  2. is set to '{"percentage_threshold": 0}', we'd fail the build if any cost diff is present, so it enables a safety net to be present if the cost spikes a lot or drops a lot (e.g. someone deletes something).

@sinabakh
Copy link
Contributor Author

That's exactly how you described it.

@sinabakh sinabakh marked this pull request as ready for review April 28, 2021 21:50
@alikhajeh1 alikhajeh1 assigned alikhajeh1 and unassigned sinabakh Apr 29, 2021
Copy link
Member

@alikhajeh1 alikhajeh1 left a comment

Choose a reason for hiding this comment

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

@sinabakh thanks for chipping away at this! it's in good shape, just a few minor comments

Comment on lines 17 to 23
# Set variables based on the order for GitHub Actions, or the env value for other CIs
iac_path=${1:-$iac_path}
terraform_plan_flags=${2:-$terraform_plan_flags}
terraform_workspace=${3:-$terraform_workspace}
usage_file=${4:-$usage_file}
config_file=${5:-$config_file}
fail_condition=${7:-$fail_condition}
Copy link
Member

Choose a reason for hiding this comment

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

I think this can be deleted since env vars are used to run the script (not params passed into the script like GH Action).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

if [ ! -z "$fail_condition" ] && [ "$(echo "$fail_condition" | jq '.percentage_threshold')" != "null" ]; then
fail_percentage_threshold=$(echo "$fail_condition" | jq -r '.percentage_threshold')
fi
fail_percentage_threshold=${fail_percentage_threshold:--1}
Copy link
Member

Choose a reason for hiding this comment

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

hmm I wasn't expecting this to default to -1 since for other CI integrations this is the absolute percentage threshold:
"For example, set to 1 to post a comment if the cost estimate changes by more than plus or minus 1%."

Could we do something similar here? I'm actually not sure how useful the drops in cost posting a comment or failing the build are but I lean towards keeping thins consistent until we have more feedback 🤷‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I see, I actually had a different intention. Since setting the default value to 0 will collide with "Fail if there was any change in diff (setting the threshold to 0)", I set its default value as -1. Do you have any suggestions to enhance it?

Copy link
Member

Choose a reason for hiding this comment

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

So we'd want to handle 2 cases:

  1. never fail the build. Default behavior. Maybe we can do that by checking if fail_percentage_threshold is not present?
  2. fail the build if the costs increase or decrease by X%, can be done using the fail_percentage_threshold

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, done.

}

build_output_cmd () {
breakdown_path=$1
Copy link
Member

Choose a reason for hiding this comment

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

@aliscott I saw this line in diff.sh and atlantis_diff.sh too, is it redundant since it's not used in this function or am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed it from this script but left the Atlantis for another pr.

scripts/ci/jenkins_diff.sh Show resolved Hide resolved
@sinabakh
Copy link
Contributor Author

sinabakh commented May 3, 2021

@alikhajeh1 All done.

@alikhajeh1 alikhajeh1 merged commit 26b843a into infracost:master May 3, 2021
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.

None yet

2 participants