diff --git a/.github/workflows/.pr-close.yml b/.github/workflows/.pr-close.yml new file mode 100644 index 0000000..a3413af --- /dev/null +++ b/.github/workflows/.pr-close.yml @@ -0,0 +1,116 @@ +name: PR Closed + +on: + workflow_call: + inputs: + ### Typical / recommended + cleanup: + description: 'Type of cleanup; e.g. helm or label' + required: false + type: string + oc_server: + default: https://api.silver.devops.gov.bc.ca:6443 + description: 'OpenShift server' + required: false + type: string + packages: + description: 'Comma-separated list of packages to promote' + required: false + type: string + + ### Usually a bad idea / not recommended + merge_branch: + description: 'Merge target branch' + required: false + type: string + default: ${{ github.event.repository.default_branch }} + organization: + description: 'GitHub organization; e.g. bcgov' + default: ${{ github.repository_owner }} + required: false + type: string + repository: + description: 'GitHub repository, e.g. quickstart-openshift' + default: ${{ github.event.repository.name }} + required: false + type: string + tag_promote: + description: 'Tag to promote to; e.g. latest, test, demo or prod' + default: latest + required: false + type: string + target: + description: 'Target; e.g. PR number, test or prod' + default: ${{ github.event.number }} + required: false + type: string + + secrets: + oc_namespace: + description: 'OpenShift namespace' + required: false + oc_token: + description: 'OpenShift token' + required: false + +jobs: + vars: + if: inputs.packages != '' + runs-on: ubuntu-latest + outputs: + packages: ${{ steps.packages.outputs.packages }} + steps: + - id: packages + run: | + echo "packages=$(jq -cn '$ARGS.positional' --args ${{ inputs.packages }})" + echo "packages=$(jq -cn '$ARGS.positional' --args ${{ inputs.packages }})" >> $GITHUB_OUTPUT + + retags: + name: Retags + needs: vars + if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == '${{ inputs.merge_branch }}' + runs-on: ubuntu-22.04 + permissions: + packages: write + strategy: + matrix: + package: ${{ fromJson(needs.vars.outputs.packages) }} + timeout-minutes: 1 + steps: + - uses: shrink/actions-docker-registry-tag@v4 + with: + registry: ghcr.io + repository: ${{ inputs.organization }}/${{ inputs.repository }}/${{ matrix.package }} + target: ${{ inputs.target }} + tags: ${{ inputs.tag_promote }} + + # Clean up OpenShift when PR closed, no conditions + cleanup-openshift: + name: Cleanup OpenShift + runs-on: ubuntu-22.04 + timeout-minutes: 10 + steps: + - name: Helm + if: inputs.cleanup == 'helm' + env: + release: ${{ inputs.repository }}-${{ inputs.target }} + run: | + oc login --token=${{ secrets.oc_token }} --server=${{ inputs.oc_server }} + oc project ${{ inputs.oc_namespace }} # Safeguard! + + # If found, then remove + helm status ${{ env.release }} && helm uninstall --no-hooks ${{ env.release }} || \ + echo "Not found: ${{ env.release }}" + + # Remove Bitnami Crunchy PVCs + oc delete pvc data-${{ env.release }}-bitnami-pg-0 || \ + echo "Not found: pvc data-${{ env.release }}-bitnami-pg-0" + + - name: OpenShift Label + if: inputs.cleanup == 'label' + run: | + oc login --token=${{ secrets.oc_token }} --server=${{ inputs.oc_server }} + oc project ${{ inputs.oc_namespace }} # Safeguard! + + # Remove old build runs, build pods and deployment pods + oc delete all,cm,pvc,secret -l app=${{ inputs.repository }}-${{ inputs.target }} diff --git a/.github/workflows/pr-open.yml b/.github/workflows/pr-open.yml new file mode 100644 index 0000000..cbaff6a --- /dev/null +++ b/.github/workflows/pr-open.yml @@ -0,0 +1,15 @@ +name: PR + +on: + pull_request: + +concurrency: + # Cancel in progress for PR open and close + group: ${{ github.workflow }}-${{ github.event.number }} + cancel-in-progress: true + +jobs: + test: + name: Test + # uses: bcgov-nr/action-support/.github/workflows/pr-close.yml@main + uses: ./.github/workflows/.pr-close.yml diff --git a/LICENSE b/LICENSE index 19b138d..9c58c4c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 Repositories of BC's Natural Resource Ministries +Copyright (c) 2024 Province of British Columbia Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 1beba54..7b5ed4a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# action-pr-close -Common tasks for pull request close, like image promotion and openshift cleanup. +# quickstart-openshift-helpers +Workflows and any other common code used by bcgov/quickstart-openshift (template).