From 6eba2d142e6e741933b63ba96723b6aff00d3477 Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Thu, 31 Oct 2024 14:53:30 +0000 Subject: [PATCH 1/2] added automated release updating --- .github/scripts/update_pr.py | 35 +++++------- .github/scripts/update_release.py | 55 +++++++++++++++++++ .../demo_3_apply_production_temp.yml | 31 +++++++++-- .github/workflows/update_release.yml | 47 ++++++++++++++++ 4 files changed, 143 insertions(+), 25 deletions(-) create mode 100644 .github/scripts/update_release.py create mode 100644 .github/workflows/update_release.yml diff --git a/.github/scripts/update_pr.py b/.github/scripts/update_pr.py index 7279d21..20c9978 100644 --- a/.github/scripts/update_pr.py +++ b/.github/scripts/update_pr.py @@ -2,19 +2,24 @@ import os import json -from github import Github +import github from github.GithubException import GithubException +import github.PullRequest +REPO_PATH = "deploymenttheory/terraform-demo-jamfpro-v2" + +# Env var pickup and validation TOKEN = os.environ.get("GITHUB_TOKEN") DROPFILE_PATH = os.environ.get("ARTIFACT_PATH") TYPE = os.environ.get("RUN_TYPE") - ENV_VARS = [TOKEN, DROPFILE_PATH, TYPE] if any(i == "" for i in ENV_VARS): raise KeyError(f"one or more env vars are empty: {ENV_VARS}") -GH = Github(TOKEN) + +# Global GH connection +GH = github.Github(TOKEN) def open_drop_file() -> dict: """opens the drop file""" @@ -24,22 +29,13 @@ def open_drop_file() -> dict: def get_pr(): """ - Get existing PR or create a new one between branches. - - Args: - repo_name (str): Repository name in format "owner/repo" - head_branch (str): Branch containing changes - base_branch (str): Target branch for PR (default: main) - github_token (str): GitHub access token - - Returns: - github.PullRequest.PullRequest: Pull request object + Gets PR """ file = open_drop_file() target_pr_id = file["pr_number"] print(f"LOG: {target_pr_id}") try: - repo = GH.get_repo("deploymenttheory/terraform-demo-jamfpro-v2") + repo = GH.get_repo(REPO_PATH) pr = repo.get_pull(int(target_pr_id)) if pr: @@ -57,7 +53,7 @@ def get_pr(): -def update_pr_with_text(pr): +def update_pr_with_text(pr: github.PullRequest): """ Add a comment to the PR with specified text. @@ -66,13 +62,12 @@ def update_pr_with_text(pr): message (str): Comment text to add """ comments = [] - with open(DROPFILE_PATH + "/outputs.json", "r", encoding="UTF-8") as f: - json_data = json.load(f) + json_data = open_drop_file() - if TYPE == "plan": - comments.append(json.dumps(json_data["plan_output"], indent=2)) + if TYPE == "plan": + comments.append(json.dumps(json_data["plan_output"], indent=2)) - comments.append(json.dumps(json_data, indent=2)) + comments.append(json.dumps(json_data, indent=2)) try: for c in comments: diff --git a/.github/scripts/update_release.py b/.github/scripts/update_release.py new file mode 100644 index 0000000..9c7fd97 --- /dev/null +++ b/.github/scripts/update_release.py @@ -0,0 +1,55 @@ +"""script to update the text on a release""" + +import os +import json +import github +from github.GithubException import GithubException + +REPO_PATH = "deploymenttheory/terraform-demo-jamfpro-v2" + +TOKEN = os.environ.get("GITHUB_TOKEN") +DROPFILE_PATH = os.environ.get("ARTIFACT_PATH") +GIT_TAG = os.environ.get("RELEASE_TAG") + +ENV_VARS = [TOKEN, DROPFILE_PATH] + +if any (i == "" for i in ENV_VARS): + raise KeyError(f"one or more env vars are empty: {ENV_VARS}") + + +GH = github.Github(TOKEN) + +def open_drop_file() -> dict: + """opens the drop file""" + with open(DROPFILE_PATH + "/outputs.json", "r", encoding="UTF-8") as f: + return json.load(f) + + +def get_update_release(): + """gets a tag""" + try: + repo = GH.get_repo(REPO_PATH) + release = repo.get_release(GIT_TAG) + + except GithubException as e: + print(f"GitHub API error: {e}") + raise + except Exception as e: + print(f"Unexpected error: {e}") + raise + + file = open_drop_file() + message = f"{release.body}\nApply Result:\n{json.dumps(file, indent=2)}" + release.update_release( + message=message + ) + + +def main(): + get_update_release() + + +if __name__ == "__main__": + main() + + \ No newline at end of file diff --git a/.github/workflows/demo_3_apply_production_temp.yml b/.github/workflows/demo_3_apply_production_temp.yml index 26623e9..6089bed 100644 --- a/.github/workflows/demo_3_apply_production_temp.yml +++ b/.github/workflows/demo_3_apply_production_temp.yml @@ -47,11 +47,32 @@ jobs: uses: hashicorp/tfc-workflows-github/actions/apply-run@v1.3.1 with: token: ${{ secrets.TF_API_KEY }} - run: ${{ steps.terraform-plan.outputs.run_id }} - + run: ${{ steps.terraform-plan.outputs.run_id }} - - - + - name: Save outputs to file + run: | + cat << EOF > ${{ vars.OUTPUTS_FILE_FN }} + { + "status": "${{ steps.terraform-apply.outputs.status }}" + } + EOF + + + - name: Upload outputs json file as artifact + uses: actions/upload-artifact@v4 + with: + name: ${{ vars.APPLY_OUTPUT_ARTIFACT_NAME }} + path: ${{ vars.OUTPUTS_FILE_FN }} + retention-days: 0 + + + update-release: + name: Deposit plan output on trigger release + needs: terraform-upload-plan-apply + uses: ./.github/workflows/update_release.yml + with: + outputs-payload: ${{ vars.APPLY_OUTPUT_ARTIFACT_NAME }} + git-tag: ${{ github.ref_name }} + diff --git a/.github/workflows/update_release.yml b/.github/workflows/update_release.yml new file mode 100644 index 0000000..1e1f30d --- /dev/null +++ b/.github/workflows/update_release.yml @@ -0,0 +1,47 @@ +name: Add PR Comment + +on: + workflow_call: + inputs: + outputs-payload: + required: true + type: string + + git-tag: + required: true + type: string + +jobs: + + update-release: + runs-on: ubuntu-latest + permissions: + packages: read + pull-requests: write + contents: write + + container: + image: ghcr.io/${{ github.repository }}/python:latest + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + steps: + - name: Checkout repo + uses: actions/checkout@v4 + + - name: Download artifact + id: download-artifact + uses: actions/download-artifact@v4 + with: + name: ${{ inputs.outputs-payload }} + + - name: Run pr update python utility + run: python .github/scripts/update_pr.py + env: + ARTIFACT_PATH: ${{ steps.download-artifact.outputs.download-path }} + GIT_TAG: ${{ inputs.git-tag }} + + + + + \ No newline at end of file From 051a1d09c21406946e262d5e5a98c98f7734270c Mon Sep 17 00:00:00 2001 From: Joseph Little Date: Thu, 31 Oct 2024 14:54:01 +0000 Subject: [PATCH 2/2] new tag --- workload/terraform/jamfpro/categories.tf | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/workload/terraform/jamfpro/categories.tf b/workload/terraform/jamfpro/categories.tf index 3edca87..90ca408 100644 --- a/workload/terraform/jamfpro/categories.tf +++ b/workload/terraform/jamfpro/categories.tf @@ -17,4 +17,8 @@ resource "jamfpro_category" "category_4" { resource "jamfpro_category" "category_5" { name = "Marketing - JL Demo V2 - 22" +} + +resource "jamfpro_category" "category_6" { + name = "Marketing - JL Demo V2 - 23" } \ No newline at end of file