diff --git a/.github/workflows/preview-create.yml b/.github/workflows/preview-create.yml new file mode 100644 index 0000000..e70f6c8 --- /dev/null +++ b/.github/workflows/preview-create.yml @@ -0,0 +1,81 @@ +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: Image to use for this workflow. + region: + type: string + default: ap-southeast-2 + description: Region which this preview infrastructure resides. + eks_cluster_name: + type: string + 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.create.outputs.preview-name }} + url: + description: The URL of the preview environment + value: ${{ jobs.create.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: + name: Create 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=${{ 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 + 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 }} + - 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..233200e --- /dev/null +++ b/.github/workflows/preview-delete.yml @@ -0,0 +1,59 @@ +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: Image to use for this workflow. + region: + type: string + default: ap-southeast-2 + description: Region which this preview infrastructure resides. + eks_cluster_name: + type: string + 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 + outputs: + version: + description: The preview environment name + value: ${{ jobs.delete.outputs.preview-name }} + +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: + delete: + name: Delete Preview Environment + runs-on: ubuntu-latest + container: + image: ${{ inputs.image }} + outputs: + preview-name: ${{ steps.info.outputs.preview_name }} + steps: + - name: ⬇️ Checkout Code + uses: actions/checkout@v3 + - name: ℹ️ Get Preview Environment Information + id: info + run: | + 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 + run: | + skpr-preview delete ${{ steps.info.outputs.preview_name }} 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 +```