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

init action #1

Merged
merged 67 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
7380892
init action
Noahnc Oct 24, 2023
cad3947
fix path
Noahnc Oct 24, 2023
3c5914b
fix(shell): Add shell to action
Noahnc Oct 24, 2023
ed30e81
path
Noahnc Oct 24, 2023
9a0b974
fix
Noahnc Oct 24, 2023
3a434d4
fix
Noahnc Oct 24, 2023
90c1dc5
fix index.js
Noahnc Oct 24, 2023
79a010e
doc(README)
Noahnc Oct 24, 2023
0cbf9a3
refac
Noahnc Oct 24, 2023
0a5309e
fix action
Noahnc Oct 24, 2023
1f9e83b
add log
Noahnc Oct 24, 2023
7e8b0d6
fix action
Noahnc Oct 24, 2023
51c3e49
update action
Noahnc Oct 24, 2023
4ec02e1
fix action
Noahnc Oct 24, 2023
65299a5
test
Noahnc Oct 24, 2023
2bf611c
fix comment
Noahnc Oct 24, 2023
b349df2
fix
Noahnc Oct 24, 2023
e5bac0d
fix quotes
Noahnc Oct 24, 2023
c333e7d
fix
Noahnc Oct 24, 2023
2b7c347
update and refactoring
Noahnc Oct 24, 2023
1954272
update and refactoring
Noahnc Oct 24, 2023
d27b1a1
update and refactoring
Noahnc Oct 24, 2023
f69ef9d
update and refactoring
Noahnc Oct 24, 2023
7711680
update and refactoring
Noahnc Oct 24, 2023
df60d9d
update and refactoring
Noahnc Oct 24, 2023
b787bd2
update and refactoring
Noahnc Oct 24, 2023
501f7e0
update and refactoring
Noahnc Oct 24, 2023
2411fd4
update and refactoring
Noahnc Oct 24, 2023
dec3c04
update and refactoring
Noahnc Oct 24, 2023
48468f4
update and refactoring
Noahnc Oct 24, 2023
1e6e1e7
update and refactoring
Noahnc Oct 25, 2023
1d1244c
update and refactoring
Noahnc Oct 25, 2023
20d8d7b
update and refactoring
Noahnc Oct 25, 2023
0c5079f
update and refactoring
Noahnc Oct 25, 2023
2651b5b
update and refactoring
Noahnc Oct 25, 2023
f4df69d
update and refactoring
Noahnc Oct 25, 2023
9165e85
update and refactoring
Noahnc Oct 25, 2023
76bee12
update and refactoring
Noahnc Oct 25, 2023
28193e0
update and refactoring
Noahnc Oct 25, 2023
96ec220
implement tile version handling
Noahnc Oct 25, 2023
0558792
test action
Noahnc Oct 26, 2023
2ec6bd8
test action
Noahnc Oct 26, 2023
a0705a0
test action
Noahnc Oct 26, 2023
bac59ca
test action
Noahnc Oct 26, 2023
2c04c5d
test action
Noahnc Oct 26, 2023
dd9397d
test action
Noahnc Oct 26, 2023
f33576e
test action
Noahnc Oct 26, 2023
4c2854b
test action
Noahnc Oct 26, 2023
7138ced
test action
Noahnc Oct 26, 2023
ee9c038
test action
Noahnc Oct 26, 2023
80665bd
test action
Noahnc Oct 26, 2023
d322b15
test action
Noahnc Oct 26, 2023
339d515
test action
Noahnc Oct 26, 2023
e4e0017
test action
Noahnc Oct 26, 2023
59a337b
test action
Noahnc Oct 26, 2023
99282b0
test action
Noahnc Oct 26, 2023
2346b67
test action
Noahnc Oct 26, 2023
ba28a44
test action
Noahnc Oct 26, 2023
75f76e2
test action
Noahnc Oct 26, 2023
b62a419
test action
Noahnc Oct 26, 2023
eb790a1
test action
Noahnc Oct 26, 2023
1549d11
test action
Noahnc Oct 26, 2023
52967f8
test action
Noahnc Oct 26, 2023
c829184
test action
Noahnc Oct 26, 2023
044e83e
test action
Noahnc Oct 26, 2023
f1d6109
test action
Noahnc Oct 26, 2023
3223f8d
test action
Noahnc Oct 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/action_integration_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "GitHub Action integration test"

permissions:
contents: write
pull-requests: write

on:
pull_request:
branches:
- main
workflow_dispatch:

jobs:
integration-test:
name: "Run GitHub Action integration test"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Run in report only mode
uses: ./
with:
push_changes: false
report_only: true

- name: Run in update mode
uses: ./
with:
push_changes: false
report_only: false






78 changes: 78 additions & 0 deletions .github/workflows/cli-integration-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: CLI Integration test

on:
pull_request:
types:
- opened
- synchronize
- reopened
- closed
branches:
- main

jobs:
test:
# only run if not closed or closed with merge
if: ${{ github.event.pull_request.merged == true || github.event.pull_request.state != 'closed' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
report_json_file: InfraPatch_Statistics.json

strategy:
matrix:
os:
- macos-latest
- ubuntu-latest
# - windows-latest Windows does currently not work because of pygohcl
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install Python
uses: actions/setup-python@v4
with:
python-version: "3.11"

- name: Install InfraPatch CLI
run: |
python -m pip install .
shell: bash

- name: Run InfraPatch report
shell: bash
run: infrapatch --debug report --dump-json-statistics

- name: Check report result
shell: pwsh
run: |
$report = Get-Content $env:report_json_file -Raw | ConvertFrom-Json
if ( -not $report.total_resources -gt 0 ) {
throw "Failed to get resources"
}
if ( $report.resources_patched -ne 0 ) {
throw "No resources should be patched"
}
if ( $report.errors -gt 0 ) {
throw "Errors have been detected"
}

- name: Run InfraPatch update
shell: bash
run: infrapatch --debug update --dump-json-statistics --confirm

- name: Check update result
shell: pwsh
run: |
$report = Get-Content $env:report_json_file -Raw | ConvertFrom-Json
if ( -not $report.total_resources -gt 0 ) {
throw "Failed to get resources"
}
if ( -not ( $report.resources_patched -gt 3 ) ) {
throw "No resources should be patched"
}
if ( $report.errors -gt 0 ) {
throw "Errors have been detected"
}


2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ __pycache__/
# C extensions
*.so

**/.terraform/*

# Distribution / packaging
.Python
build/
Expand Down
15 changes: 15 additions & 0 deletions .idea/git_toolbox_prj.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The follwoing chapter describes the CLI usage.

### Installation

Before installing the CLI, make sure you have Python 3.11 or higher installed.
The InfraPatch CLI can be installed via pip:

```bash
Expand All @@ -22,6 +23,7 @@ After the installation, InfraPatch can be run with the following command:
```bash
infrapatch --help
```
![infrapatch_help.png](asset%2Finfrapatch_help.png)

### Usage

Expand All @@ -31,12 +33,14 @@ The `report` command will scan your Terraform code and report the current and ne
```bash
infrapatch report
```
![infrapatch_report.gif](asset%2Finfrapatch_report.gif)

The `update` command will scan your Terraform code and ask you for confirmation to update the listed modules and providers to the newest version.

```bash
infrapatch update
```
![infrapatch_update.gif](asset%2Finfrapatch_update.gif)

### Authentication

Expand Down Expand Up @@ -66,7 +70,54 @@ infrapatch --credentials-file-path "path/to/credentials/file" update

### GitHub Action

This repository also contains a GitHub Action that can be used to automatically update your Terraform code.
The following example shows how to use the GitHub Action:
This repository also contains a GitHub Action.
The Action can for example be run on a schedule to automatically update your terraform code and open a PR with the changes.

The following example workflow runs once a day:

```yaml
name: "InfraPatch"

permissions:
contents: write
pull-requests: write

on:
schedule:
- cron: '0 23 * * *'
workflow_dispatch:

jobs:
infrapatch:
name: "Check Terraform Code for Updates"
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Run in update mode
uses: Noahnc/infrapatch@main
with:
report_only: false
```

#### Report only Mode

By default, the Action will create a Branch with all the changes and opens a PR to Branch for which the Action was triggered.
When setting the input `report_only` to `true`, the Action will only report available updates in the Action output.

#### Authentication

If you use private registries in your Terraform project, you can specify credentials for the Action with the Input `registry_secrets`:

```yaml
- name: Run in update mode
uses: Noahnc/infrapatch@main
with:
report_only: false
registry_secrets: |
spacelift.io=${{ secrets.SPACELIFT_API_TOKEN }}
<second_registry>=<registry_token>
```

Each secret must be specified in a new line with the following format: `<registry_name>=<registry_token>`
106 changes: 104 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,115 @@
name: InfraPatch Github Action
description: A github action to update provider and module dependencies in terraform files
author: "Noah Canadea"
inputs:
target_branch_name:
description: "Name of the branch where changes will be pushed to. Defaults to feature/infrapatch-bot"
required: true
default: "feature/infrapatch-bot"
default_registry_domain:
description: "Default registry domain to use for modules and providers without explicit registry domain set. Defaults to registry.terraform.io"
required: false
default: "registry.terraform.io"
git_user:
description: "Git user to use for commits. Defaults to InfraPatch Bot"
required: false
default: "InfraPatch Bot"
git_email:
description: "Git email to use for commits. Defaults to [email protected]"
required: false
default: "[email protected]"
github_token:
description: "GitHub access token. Defaults to github.token."
default: ${{ github.token }}
report_only:
description: "Only report new versions. Do not update files. Defaults to false"
default: "false"
required: true
registry_secrets:
description: "Registry secrets to use for private registries"
required: false
default: ""
working_directory:
description: "Working directory to run the command in. Defaults to the root of the repository"
required: false
default: ${{ github.workspace }}
runs:
using: composite
steps:
- name: Extract branch name
id: head_branch
shell: bash
run: |
head_branch="origin/${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
echo "Detected head branch: $head_branch"
echo "branch=$head_branch" >> $GITHUB_OUTPUT

- name: Install Python
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install infrapatch
run: pip install .

- name: Install requirements
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
shell: bash

- name: Create target branch
id: create_branch
if: ${{ inputs.report_only == 'false' }}
uses: peterjgrainger/[email protected]
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
with:
branch: "refs/heads/${{ inputs.target_branch_name }}"

- name: Configure git
if: ${{ inputs.report_only }} == 'false' }}
working-directory: ${{ inputs.working_directory }}
shell: bash
run: |
git config --global user.name "${{ inputs.git_user }}"
git config --global user.email "${{ inputs.git_email }}"

- name: Switch to target branch
if: ${{ inputs.report_only == 'false' }}
working-directory: ${{ inputs.working_directory }}
shell: bash
run: |
git fetch origin
git checkout -b "${{ inputs.target_branch_name }}" "origin/${{ inputs.target_branch_name }}"

- name: Rebase target branch
if: ${{ steps.create_branch.outputs.created == 'false' }}
working-directory: ${{ inputs.working_directory }}
shell: bash
run: |
echo "Rebasing ${{ inputs.target_branch_name }} on ${{ steps.head_branch.outputs.branch }}"
git rebase -Xtheirs ${{ steps.head_branch.outputs.branch }}

- name: Run InfraPatch Action
shell: bash
run: |
module="infrapatch.action"
arguments=()
if [ "${{ runner.debug }}" == "1" ]; then
arguments+=("--debug")
fi
if [ "${{ inputs.report_only }}" == "true" ]; then
arguments+=("--report-only")
fi
if [ "${{ inputs.registry_secrets }}" != "" ]; then
arguments+=("--registry-secrets-string" "\"${{ inputs.registry_secrets }}\"")
fi
arguments+=("--github-token" "${{ inputs.github_token }}")
arguments+=("--working-directory" "${{ inputs.working_directory }}")
arguments+=("--default-registry-domain" "${{ inputs.default_registry_domain }}")
python -m "$module" "${arguments[@]}"

- name: Push changes
if: ${{ inputs.report_only == 'false' }}
working-directory: ${{ inputs.working_directory }}
shell: bash
run: |
git push
Binary file added asset/infrapatch_help.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added asset/infrapatch_report.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added asset/infrapatch_update.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions infrapatch/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

File renamed without changes.
Loading
Loading