From 1a4b18e002c847192ed6cd10960f7114ed025ec0 Mon Sep 17 00:00:00 2001 From: Nick Schuch Date: Tue, 31 Oct 2023 09:52:33 +1000 Subject: [PATCH 1/7] Adds workflows for preview environment creation and deletion --- .github/workflows/preview-create.yml | 79 ++++++++++++++++++++++++++++ .github/workflows/preview-delete.yml | 54 +++++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 .github/workflows/preview-create.yml create mode 100644 .github/workflows/preview-delete.yml diff --git a/.github/workflows/preview-create.yml b/.github/workflows/preview-create.yml new file mode 100644 index 0000000..86d3296 --- /dev/null +++ b/.github/workflows/preview-create.yml @@ -0,0 +1,79 @@ +name: Create Preview Environment + +on: + workflow_call: + inputs: + image: + type: string + default: skpr/preview:2.x + description: Region which this preview infrastructure resides. + region: + type: string + required: true + description: Region which this preview infrastructure resides. + eks_cluster_name: + default: true + type: boolean + description: Name of the EKS cluster which this preview environment resides. + k8s_namespace: + type: string + description: Name of the Kubernetes namespace which this preview environment resides. + domain: + type: string + description: Domain which these preview environments reside on. + post_deploy_command: + type: string + description: Command which will be executed after the environment is created. + secrets: + skpr_preview_registry_url: + required: true + skpr_preview_username: + required: true + skpr_preview_password: + required: true + outputs: + version: + description: The preview environment name + value: ${{ jobs.info.outputs.preview_name }} + url: + description: The URL of the preview environment + value: ${{ jobs.info.outputs.preview_domain }} + +env: + AWS_REGION: ${{ inputs.region }} + SKPR_PREVIEW_CLUSTER_NAME: ${{ inputs.eks_cluster_name }} + SKPR_PREVIEW_NAMESPACE: ${{ inputs.k8s_namespace }} + SKPR_PREVIEW_REGISTRY_URL: ${{ secrets.SKPR_PREVIEW_REGISTRY_URL }} + SKPR_PREVIEW_REGISTRY_USERNAME: ${{ secrets.SKPR_PREVIEW_USERNAME }} + SKPR_PREVIEW_REGISTRY_PASSWORD: ${{ secrets.SKPR_PREVIEW_PASSWORD }} + AWS_ACCESS_KEY_ID: ${{ secrets.SKPR_PREVIEW_USERNAME }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.SKPR_PREVIEW_PASSWORD }} + +jobs: + create-preview-environment: + name: Delete Preview Environment + if: github.event.pull_request.draft == false + runs-on: ubuntu-latest + container: + image: ${{ inputs.image }} + outputs: + preview-name: ${{ steps.info.outputs.preview_name }} + preview-domain: ${{ steps.info.outputs.preview_domain }} + steps: + - name: ⬇️ Checkout Code + uses: actions/checkout@v3 + - name: ℹ️ Get Preview Environment Information + id: info + run: | + PREVIEW_NAME=${{ github.event.pull_request.head.ref }} + PREVIEW_NAME=$(echo $PREVIEW_NAME | sed -r 's/[^a-zA-Z0-9-]//g' | tr '[:upper:]' '[:lower:]') + PREVIEW_DOMAIN=${PREVIEW_NAME}.${{ inputs.domain }} + echo "::set-output name=preview_name::$PREVIEW_NAME" + echo "::set-output name=preview_domain::$PREVIEW_DOMAIN" + - name: 🚀 Create Preview Environment + run: | + skpr-preview create ${{ steps.info.outputs.preview_name }} ${{ steps.info.outputs.preview_domain }} + - name: 🧹 Execute Post Creation Commands + timeout-minutes: 30 + run: | + skpr-preview exec ${{ steps.info.outputs.preview_name }} -- ${{ inputs.post_deploy_command }} diff --git a/.github/workflows/preview-delete.yml b/.github/workflows/preview-delete.yml new file mode 100644 index 0000000..534d87e --- /dev/null +++ b/.github/workflows/preview-delete.yml @@ -0,0 +1,54 @@ +name: Delete Preview Environment + +on: + workflow_call: + inputs: + image: + type: string + default: skpr/preview:2.x + description: Region which this preview infrastructure resides. + region: + type: string + required: true + description: Region which this preview infrastructure resides. + eks_cluster_name: + default: true + type: boolean + description: Name of the EKS cluster which this preview environment resides. + k8s_namespace: + type: string + description: Name of the Kubernetes namespace which this preview environment resides. + secrets: + skpr_preview_username: + required: true + skpr_preview_password: + required: true + +env: + AWS_REGION: ${{ inputs.region }} + SKPR_PREVIEW_CLUSTER_NAME: ${{ inputs.eks_cluster_name }} + SKPR_PREVIEW_NAMESPACE: ${{ inputs.k8s_namespace }} + AWS_ACCESS_KEY_ID: ${{ secrets.SKPR_PREVIEW_USERNAME }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.SKPR_PREVIEW_PASSWORD }} + +jobs: + create-preview: + name: Create Preview Environment + runs-on: ubuntu-latest + container: + image: ${{ inputs.image }} + outputs: + preview-name: ${{ steps.info.outputs.preview_name }} + preview-domain: ${{ steps.info.outputs.preview_domain }} + steps: + - name: ⬇️ Checkout Code + uses: actions/checkout@v3 + - name: ℹ️ Get Preview Environment Information + id: info + run: | + PREVIEW_NAME=${{ github.event.pull_request.head.ref }} + PREVIEW_NAME=$(echo $PREVIEW_NAME | sed -r 's/[^a-zA-Z0-9-]//g' | tr '[:upper:]' '[:lower:]') + echo "::set-output name=preview_name::$PREVIEW_NAME" + - name: 🧹 Delete Preview Environment + run: | + skpr-preview delete ${{ steps.info.outputs.preview_name }} From 0dce98c095b7ea389fddf420344dcdf07715c412 Mon Sep 17 00:00:00 2001 From: Nick Schuch Date: Tue, 31 Oct 2023 09:56:37 +1000 Subject: [PATCH 2/7] Fixing linting --- .github/workflows/preview-create.yml | 4 ++-- .github/workflows/preview-delete.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/preview-create.yml b/.github/workflows/preview-create.yml index 86d3296..48a76c4 100644 --- a/.github/workflows/preview-create.yml +++ b/.github/workflows/preview-create.yml @@ -68,8 +68,8 @@ jobs: PREVIEW_NAME=${{ github.event.pull_request.head.ref }} PREVIEW_NAME=$(echo $PREVIEW_NAME | sed -r 's/[^a-zA-Z0-9-]//g' | tr '[:upper:]' '[:lower:]') PREVIEW_DOMAIN=${PREVIEW_NAME}.${{ inputs.domain }} - echo "::set-output name=preview_name::$PREVIEW_NAME" - echo "::set-output name=preview_domain::$PREVIEW_DOMAIN" + echo "preview_name=$preview_name" >> $GITHUB_OUTPUT + echo "preview_domain=$PREVIEW_DOMAIN" >> $GITHUB_OUTPUT - name: 🚀 Create Preview Environment run: | skpr-preview create ${{ steps.info.outputs.preview_name }} ${{ steps.info.outputs.preview_domain }} diff --git a/.github/workflows/preview-delete.yml b/.github/workflows/preview-delete.yml index 534d87e..629a923 100644 --- a/.github/workflows/preview-delete.yml +++ b/.github/workflows/preview-delete.yml @@ -48,7 +48,7 @@ jobs: run: | PREVIEW_NAME=${{ github.event.pull_request.head.ref }} PREVIEW_NAME=$(echo $PREVIEW_NAME | sed -r 's/[^a-zA-Z0-9-]//g' | tr '[:upper:]' '[:lower:]') - echo "::set-output name=preview_name::$PREVIEW_NAME" + echo "preview_name=$preview_name" >> $GITHUB_OUTPUT - name: 🧹 Delete Preview Environment run: | skpr-preview delete ${{ steps.info.outputs.preview_name }} From 10a6d0b15c9c2121402ac1ddc6a57719795310eb Mon Sep 17 00:00:00 2001 From: Nick Schuch Date: Tue, 31 Oct 2023 10:06:07 +1000 Subject: [PATCH 3/7] Adds default region --- .github/workflows/preview-create.yml | 2 +- .github/workflows/preview-delete.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/preview-create.yml b/.github/workflows/preview-create.yml index 48a76c4..4b1a2c0 100644 --- a/.github/workflows/preview-create.yml +++ b/.github/workflows/preview-create.yml @@ -9,7 +9,7 @@ on: description: Region which this preview infrastructure resides. region: type: string - required: true + default: ap-southeast-2 description: Region which this preview infrastructure resides. eks_cluster_name: default: true diff --git a/.github/workflows/preview-delete.yml b/.github/workflows/preview-delete.yml index 629a923..6867bcd 100644 --- a/.github/workflows/preview-delete.yml +++ b/.github/workflows/preview-delete.yml @@ -9,7 +9,7 @@ on: description: Region which this preview infrastructure resides. region: type: string - required: true + default: ap-southeast-2 description: Region which this preview infrastructure resides. eks_cluster_name: default: true From e45ed8c56681407b66cc857cc754d0b50d070326 Mon Sep 17 00:00:00 2001 From: Nick Schuch Date: Tue, 31 Oct 2023 10:08:24 +1000 Subject: [PATCH 4/7] Fix inputs --- .github/workflows/preview-create.yml | 3 +-- .github/workflows/preview-delete.yml | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/preview-create.yml b/.github/workflows/preview-create.yml index 4b1a2c0..821bcda 100644 --- a/.github/workflows/preview-create.yml +++ b/.github/workflows/preview-create.yml @@ -12,8 +12,7 @@ on: default: ap-southeast-2 description: Region which this preview infrastructure resides. eks_cluster_name: - default: true - type: boolean + type: string description: Name of the EKS cluster which this preview environment resides. k8s_namespace: type: string diff --git a/.github/workflows/preview-delete.yml b/.github/workflows/preview-delete.yml index 6867bcd..4bc110e 100644 --- a/.github/workflows/preview-delete.yml +++ b/.github/workflows/preview-delete.yml @@ -12,8 +12,7 @@ on: default: ap-southeast-2 description: Region which this preview infrastructure resides. eks_cluster_name: - default: true - type: boolean + type: string description: Name of the EKS cluster which this preview environment resides. k8s_namespace: type: string From 97def9c13ae676645dc2329e7ef0dddd6c625af4 Mon Sep 17 00:00:00 2001 From: Nick Schuch Date: Tue, 31 Oct 2023 10:12:23 +1000 Subject: [PATCH 5/7] Fix preview name --- .github/workflows/preview-create.yml | 2 +- .github/workflows/preview-delete.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/preview-create.yml b/.github/workflows/preview-create.yml index 821bcda..931a75d 100644 --- a/.github/workflows/preview-create.yml +++ b/.github/workflows/preview-create.yml @@ -67,7 +67,7 @@ jobs: PREVIEW_NAME=${{ github.event.pull_request.head.ref }} PREVIEW_NAME=$(echo $PREVIEW_NAME | sed -r 's/[^a-zA-Z0-9-]//g' | tr '[:upper:]' '[:lower:]') PREVIEW_DOMAIN=${PREVIEW_NAME}.${{ inputs.domain }} - echo "preview_name=$preview_name" >> $GITHUB_OUTPUT + echo "preview_name=$PREVIEW_NAME" >> $GITHUB_OUTPUT echo "preview_domain=$PREVIEW_DOMAIN" >> $GITHUB_OUTPUT - name: 🚀 Create Preview Environment run: | diff --git a/.github/workflows/preview-delete.yml b/.github/workflows/preview-delete.yml index 4bc110e..a7741d4 100644 --- a/.github/workflows/preview-delete.yml +++ b/.github/workflows/preview-delete.yml @@ -47,7 +47,7 @@ jobs: run: | PREVIEW_NAME=${{ github.event.pull_request.head.ref }} PREVIEW_NAME=$(echo $PREVIEW_NAME | sed -r 's/[^a-zA-Z0-9-]//g' | tr '[:upper:]' '[:lower:]') - echo "preview_name=$preview_name" >> $GITHUB_OUTPUT + echo "preview_name=$PREVIEW_NAME" >> $GITHUB_OUTPUT - name: 🧹 Delete Preview Environment run: | skpr-preview delete ${{ steps.info.outputs.preview_name }} From ff39448f30d750ad6d705f13627284a051b2bd45 Mon Sep 17 00:00:00 2001 From: Nick Schuch Date: Tue, 31 Oct 2023 10:48:56 +1000 Subject: [PATCH 6/7] Remove github.event.pull_request.head.ref --- .github/workflows/preview-create.yml | 15 +++++++++------ .github/workflows/preview-delete.yml | 16 +++++++++++----- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.github/workflows/preview-create.yml b/.github/workflows/preview-create.yml index 931a75d..e70f6c8 100644 --- a/.github/workflows/preview-create.yml +++ b/.github/workflows/preview-create.yml @@ -3,10 +3,13 @@ name: Create Preview Environment on: workflow_call: inputs: + ref: + type: string + description: The fully-formed ref of the branch or tag that triggered the workflow run. image: type: string default: skpr/preview:2.x - description: Region which this preview infrastructure resides. + description: Image to use for this workflow. region: type: string default: ap-southeast-2 @@ -33,10 +36,10 @@ on: outputs: version: description: The preview environment name - value: ${{ jobs.info.outputs.preview_name }} + value: ${{ jobs.create.outputs.preview-name }} url: description: The URL of the preview environment - value: ${{ jobs.info.outputs.preview_domain }} + value: ${{ jobs.create.outputs.preview-domain }} env: AWS_REGION: ${{ inputs.region }} @@ -49,8 +52,8 @@ env: AWS_SECRET_ACCESS_KEY: ${{ secrets.SKPR_PREVIEW_PASSWORD }} jobs: - create-preview-environment: - name: Delete Preview Environment + create: + name: Create Preview Environment if: github.event.pull_request.draft == false runs-on: ubuntu-latest container: @@ -64,7 +67,7 @@ jobs: - name: ℹ️ Get Preview Environment Information id: info run: | - PREVIEW_NAME=${{ github.event.pull_request.head.ref }} + PREVIEW_NAME=${{ inputs.ref }} PREVIEW_NAME=$(echo $PREVIEW_NAME | sed -r 's/[^a-zA-Z0-9-]//g' | tr '[:upper:]' '[:lower:]') PREVIEW_DOMAIN=${PREVIEW_NAME}.${{ inputs.domain }} echo "preview_name=$PREVIEW_NAME" >> $GITHUB_OUTPUT diff --git a/.github/workflows/preview-delete.yml b/.github/workflows/preview-delete.yml index a7741d4..233200e 100644 --- a/.github/workflows/preview-delete.yml +++ b/.github/workflows/preview-delete.yml @@ -3,10 +3,13 @@ name: Delete Preview Environment on: workflow_call: inputs: + ref: + type: string + description: The fully-formed ref of the branch or tag that triggered the workflow run. image: type: string default: skpr/preview:2.x - description: Region which this preview infrastructure resides. + description: Image to use for this workflow. region: type: string default: ap-southeast-2 @@ -22,6 +25,10 @@ on: required: true skpr_preview_password: required: true + outputs: + version: + description: The preview environment name + value: ${{ jobs.delete.outputs.preview-name }} env: AWS_REGION: ${{ inputs.region }} @@ -31,21 +38,20 @@ env: AWS_SECRET_ACCESS_KEY: ${{ secrets.SKPR_PREVIEW_PASSWORD }} jobs: - create-preview: - name: Create Preview Environment + delete: + name: Delete Preview Environment runs-on: ubuntu-latest container: image: ${{ inputs.image }} outputs: preview-name: ${{ steps.info.outputs.preview_name }} - preview-domain: ${{ steps.info.outputs.preview_domain }} steps: - name: ⬇️ Checkout Code uses: actions/checkout@v3 - name: ℹ️ Get Preview Environment Information id: info run: | - PREVIEW_NAME=${{ github.event.pull_request.head.ref }} + PREVIEW_NAME=${{ inputs.ref }} PREVIEW_NAME=$(echo $PREVIEW_NAME | sed -r 's/[^a-zA-Z0-9-]//g' | tr '[:upper:]' '[:lower:]') echo "preview_name=$PREVIEW_NAME" >> $GITHUB_OUTPUT - name: 🧹 Delete Preview Environment From a720898c15e3c6b8e2640dc4338e1c863cfafcef Mon Sep 17 00:00:00 2001 From: Nick Schuch Date: Tue, 31 Oct 2023 11:27:23 +1000 Subject: [PATCH 7/7] Adds documentation --- README.md | 21 +++++++++++++- docs/preview.md | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 docs/preview.md diff --git a/README.md b/README.md index 6906e14..0ff46ec 100644 --- a/README.md +++ b/README.md @@ -1 +1,20 @@ -# gh-workflows +# GitHub Actions Workflows + +This repository contains GitHub Action workflows to make it easy to automate your development pipeline. + +## Usage + +Example use below, see documentation for more [details](#documentation) on each workflow. + +```yaml +jobs: + my_job: + uses: skpr/gh-workflow/.github/workflows/workflow_file.yml@main + with: + input_a: abc + input_b: def +``` + +## Documentation + +* [Preview Environment](/docs/preview.md) - Create preview environments as part of a pull request workflow. diff --git a/docs/preview.md b/docs/preview.md new file mode 100644 index 0000000..b304bae --- /dev/null +++ b/docs/preview.md @@ -0,0 +1,76 @@ +# Preview Environment + +The following document outlines our Github Workflows which faciliate a preview environment deployment pipeline. + +## Required Secrets + +The following are secrets which are provided by a Skpr platform team member. + +* SKPR_PREVIEW_REGISTRY_URL +* SKPR_PREVIEW_REGISTRY_USERNAME +* SKPR_PREVIEW_REGISTRY_PASSWORD + +## Examples + +### Create Preview Environment + +Creates a preview environment as part of a pull request workflow. + +This workflow will: + +* Package the application using the same approach as `skpr package`. +* Deploy the application onto Preview environment infrastructure. +* Execute post deploy commands after the preview environment is deployed. + +```yaml +name: Create Preview Environment + +on: + pull_request: + types: [ synchronize, opened, reopened, ready_for_review ] + +concurrency: + group: preview-${{ github.head_ref }} + cancel-in-progress: true + +jobs: + deploy: + uses: skpr/gh-workflows/.github/workflows/preview-create.yml@main + secrets: + skpr_preview_registry_url: ${{ secrets.SKPR_PREVIEW_REGISTRY_URL }} + skpr_preview_username: ${{ secrets.SKPR_PREVIEW_USERNAME }} + skpr_preview_password: ${{ secrets.SKPR_PREVIEW_PASSWORD }} + with: + ref: ${{ github.event.pull_request.head.ref }} + eks_cluster_name: NEEDS TO BE CONFIGURED + k8s_namespace: NEEDS TO BE CONFIGURED + domain: NEEDS TO BE CONFIGURED + post_deploy_command: drush deploy +``` + +### Delete Preview Environment + +Deletes a preview environment when a pull request is closed or moved to a draft state. + +```yaml +name: Delete Preview Environment + +on: + pull_request: + types: [ closed, converted_to_draft ] + +concurrency: + group: preview-${{ github.head_ref }} + cancel-in-progress: true + +jobs: + deploy: + uses: skpr/gh-workflows/.github/workflows/preview-delete.yml@main + secrets: + skpr_preview_username: ${{ secrets.SKPR_PREVIEW_USERNAME }} + skpr_preview_password: ${{ secrets.SKPR_PREVIEW_PASSWORD }} + with: + ref: ${{ github.event.pull_request.head.ref }} + eks_cluster_name: NEEDS TO BE CONFIGURED + k8s_namespace: NEEDS TO BE CONFIGURED +```