Skip to content

Store params and parsed params into deployment payload#325

Merged
GrantBirki merged 9 commits intogithub:mainfrom
fabn:feature/payload-arguments
Nov 28, 2024
Merged

Store params and parsed params into deployment payload#325
GrantBirki merged 9 commits intogithub:mainfrom
fabn:feature/payload-arguments

Conversation

@fabn
Copy link
Copy Markdown
Contributor

@fabn fabn commented Nov 28, 2024

Follow up of #322, the use case is the following: when creating a deployment one could use passed params to customise the deployment process (e.g. to pass terraform variables, or set some custom ENV variables in a kubernetes deployment).

Some environments needs to be update whenever their source branch is updated without need to trigger another deployment from scratch.

With this PR it's possible to inspect the previous deployment object and get creation params and update the environment reusing the same creation flags, proof of concept (not tested but it should work):

name: Update Environment

on:
  pull_request:
    types: [synchronize]

jobs:
  # This job get a deployment for the current branch, it assumes that at most a
  # deployment exists for any given branch, since it's a proof of concept
  # Also it assumes a previous create command like `.deploy | --vars.foo=bar --vars.bar=baz`
  payload:
    name: Retrieve payload
    runs-on: ubuntu-latest
    steps:
      - name: Get payload
        id: payload
        run: |
          gh api repos/${{ github.repository }}/deployments?ref=${{ github.event.pull_request.head.ref }}) \
           | jq -c 'first | .payload.parsed_params.vars' > payload.json
          echo vars=$(cat payload.json) >> "$GITHUB_OUTPUT"
    outputs:
      variables: ${{ steps.payload.outputs.vars }} # will contain {"foo":"bar","bar":"baz"}

  deploy:
    name: Terraform deploy
    needs: payload
    runs-on: ubuntu-latest
    steps:
      - name: Store params
        # Assuming that the payload is a JSON object with a key `params_parsed`
        run: |
          echo '${{ fromJSON(needs.payload.outputs.variables) }}' > params.tfvars.json
      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v3
      - uses: actions/checkout@v4
      - name: Terraform init
        run: terraform init
      - name: Terraform apply
        run: terraform apply -auto-approve -var-file=params.tfvars.json

@fabn fabn requested a review from GrantBirki as a code owner November 28, 2024 15:06
@GrantBirki GrantBirki added the enhancement New feature or request label Nov 28, 2024
Copy link
Copy Markdown
Contributor

@GrantBirki GrantBirki left a comment

Choose a reason for hiding this comment

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

@fabn thank you again for another great contribution! I have reviewed these changes and pushed a few commits to improve unit tests, add documentation, and make it so that we only ever have to parse the params once. I'll get these changes merged in for you today so that you can test them out. I'll plan on making a new release containing these changes next week. 🚀

@GrantBirki GrantBirki merged commit 0bd0654 into github:main Nov 28, 2024
@fabn
Copy link
Copy Markdown
Contributor Author

fabn commented Nov 28, 2024

Thank you for your prompt reaction 💪🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants