Create Redis Cluster - DEV #11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Create Redis Cluster - DEV | |
env: | |
IMAGE_REGISTRY: ghcr.io/${{ github.repository_owner }} | |
IMAGE_REGISTRY_USER: ${{ github.actor }} | |
IMAGE_REGISTRY_PASSWORD: ${{ github.token }} | |
OPENSHIFT_SERVER: ${{ secrets.OPENSHIFT_SERVER }} | |
OPENSHIFT_TOKEN: ${{ secrets.OPENSHIFT_TOKEN }} | |
OPENSHIFT_NAMESPACE: ${{ secrets.OFM_NAMESPACE_NO_ENV }}-dev | |
DOCKER_ARTIFACTORY_REPO: artifacts.developer.gov.bc.ca/docker-remote | |
REDIS_VERSION: ${{ vars.REDIS_VERSION }} | |
REPLICAS: 6 | |
on: | |
workflow_dispatch: | |
jobs: | |
build-and-deploy-redis: | |
runs-on: ubuntu-24.04 | |
environment: dev | |
steps: | |
- name: Check for Required Secrets | |
uses: actions/github-script@v4 | |
with: | |
script: | | |
const secrets = { | |
OPENSHIFT_SERVER: `${{ secrets.OPENSHIFT_SERVER }}`, | |
OPENSHIFT_TOKEN: `${{ secrets.OPENSHIFT_TOKEN }}`, | |
}; | |
const GHCR = "ghcr.io"; | |
if (`${{ env.IMAGE_REGISTRY }}`.startsWith(GHCR)) { | |
core.info(`Image registry is ${GHCR} - no registry password required`); | |
} | |
else { | |
core.info("A registry password is required"); | |
secrets["IMAGE_REGISTRY_PASSWORD"] = `${{ secrets.IMAGE_REGISTRY_PASSWORD }}`; | |
} | |
const missingSecrets = Object.entries(secrets).filter(([ name, value ]) => { | |
if (value.length === 0) { | |
core.error(`Secret "${name}" is not set`); | |
return true; | |
} | |
core.info(`Secret "${name}" is set`); | |
return false; | |
}); | |
if (missingSecrets.length > 0) { | |
core.setFailed('At least one required secret is not set in the repository. \n' + | |
"You can add it using:\n" + | |
"GitHub UI: https://docs.github.com/en/actions/reference/encrypted-secrets#creating-encrypted-secrets-for-a-repository \n" + | |
"GitHub CLI: https://cli.github.com/manual/gh_secret_set \n" + | |
"Also, refer to https://github.com/redhat-actions/oc-login#getting-started-with-the-action-or-see-example"); | |
} | |
else { | |
core.info(`All the required secrets are set`); | |
} | |
- name: Check out repository with branch [${{ env.BRANCH }}] | |
uses: actions/checkout@v2 | |
with: | |
ref: ${{ env.BRANCH }} | |
- name: Login to Docker Hub | |
uses: docker/login-action@v1 | |
with: | |
registry: ${{ env.DOCKER_ARTIFACTORY_REPO }} | |
username: ${{ secrets.DOCKER_HUB_USERNAME }} | |
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | |
- name: Build Redis from Dockerfile | |
id: build-image-redis | |
uses: redhat-actions/buildah-build@v2 | |
with: | |
image: ofm-redis | |
tags: ${{ env.REDIS_VERSION }} | |
dockerfiles: | | |
./tools/redis/Dockerfile | |
build-args: REDIS_VERSION=${{ env.REDIS_VERSION }} | |
- name: Push Redis to Registry | |
id: push-image-redis | |
uses: redhat-actions/push-to-registry@v2 | |
with: | |
image: ${{ steps.build-image-redis.outputs.image }} | |
tags: ${{ steps.build-image-redis.outputs.tags }} | |
registry: ${{ env.IMAGE_REGISTRY }} | |
username: ${{ env.IMAGE_REGISTRY_USER }} | |
password: ${{ env.IMAGE_REGISTRY_PASSWORD }} | |
- name: Install OC | |
uses: redhat-actions/openshift-tools-installer@v1 | |
with: | |
oc: 4 | |
- name: Init | |
run : | | |
echo "Initializing..." | |
set -eux | |
# Login to OpenShift and select project | |
oc login \ | |
--token=${{ env.OPENSHIFT_TOKEN }} \ | |
--server=${{ env.OPENSHIFT_SERVER }} | |
oc project ${{ env.OPENSHIFT_NAMESPACE }} | |
- name: Cleanup | |
run : | | |
echo "Cleaning up previous installation (if any)..." | |
oc delete all,rc,svc,route,pvc,secret,configmap,sa -l app=redis | |
- name: Deploy Redis Pods | |
run: | | |
oc process -f tools/openshift/redis-ha.yaml \ | |
-p REPLICAS=${{ env.REPLICAS }} \ | |
-p IMAGE=${{ steps.push-image-redis.outputs.registry-path }} \ | |
| oc apply -f - | |
- name: Wait for Readiness | |
run: | | |
echo "Waiting for pods to be ready..." | |
oc rollout status statefulset/redis | |
- name: Create Cluster | |
run: | | |
echo "Creating Cluster..." | |
oc exec -it redis-0 \ | |
-- redis-cli --cluster create --cluster-replicas 1 \ | |
$(oc get pods -l app=redis -o \ | |
jsonpath='{range.items[*]}{.status.podIP}:6379 {end}') \ | |
--cluster-yes | |
echo "Success!" | |
- name: Rollout Backend Pods | |
run: | | |
oc rollout restart \ | |
deployment/ofm-backend-dev deployment/ofm-backend-test | |
oc rollout status deployment/ofm-backend-dev | |
oc rollout status deployment/ofm-backend-test |