bcgov/citz-imb-staff-purchasing-reimbursement

View on GitHub
.github/workflows/api-build-deploy.yaml

Summary

Maintainability
Test Coverage
name: Build and Deploy API

on:
  push:
    branches: [ main ]
    paths:
      - "api/**"
  workflow_dispatch:

jobs:
  build-tag-push:
    name: Build, Tag, Push to Artifactory
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Log In to Docker
        run: |
          docker login ${{ secrets.ARTIFACTORY_URL }} -u ${{ secrets.ARTIFACTORY_USERNAME }} -p ${{ secrets.ARTIFACTORY_PASSWORD }}

      - name: Copy env.template
        run: |
          cp .env.template .env
      
      - name: Build API Image
        run: |
          docker-compose build spr-api

      - name: List Docker Images
        run: |
          docker images
        
      - name: Tag Image
        run: |
          docker tag citz-imb-staff-purchasing-reimbursement_spr-api ${{ secrets.ARTIFACTORY_URL }}/${{ secrets.IMAGE_REPOSITORY }}/spr-api:latest
      
      - name: Push Image
        run: |
          docker push ${{ secrets.ARTIFACTORY_URL }}/${{ secrets.IMAGE_REPOSITORY }}/spr-api:latest
  
  deploy:
    name: Deploy to OpenShift
    runs-on: ubuntu-latest
    needs: [ build-tag-push ]
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v3

      - name: Login to Openshift
        uses: redhat-actions/oc-login@v1
        env:
          OPENSHIFT_USER: github-actions
          OPENSHIFT_NAMESPACE: ${{ secrets.OPENSHIFT_NAMESPACE }}
        with: 
          openshift_server_url: ${{ secrets.OPENSHIFT_SERVER_URL }}
          openshift_token: ${{ secrets.OPENSHIFT_TOKEN }}
          namespace: ${{ secrets.OPENSHIFT_NAMESPACE }}

      - name: Apply DeploymentConfig
        env:
          OPENSHIFT_NAMESPACE: ${{ secrets.OPENSHIFT_NAMESPACE }}
          APPLICATION_NAME: staff-purchase-reimbursement
          CONTAINER_NAME: spr-api
          DEPLOYMENT_CONFIG: api-dc-template.yaml
          IMAGE_REPOSITORY: ${{ secrets.IMAGE_REPOSITORY }}
          ARTIFACTORY_URL: ${{ secrets.ARTIFACTORY_URL }}
        run: |
          ./.github/helpers/deploy.sh

      - name: Rollout Pod
        continue-on-error: true
        run: |
          oc rollout latest dc/spr-api

  healthcheck:
    name: Determine Rollout Success
    runs-on: ubuntu-latest
    needs: [ deploy ]
    steps:
      - name: Login to Openshift
        uses: redhat-actions/oc-login@v1
        env:
          OPENSHIFT_USER: github-actions
          OPENSHIFT_NAMESPACE: ${{ secrets.OPENSHIFT_NAMESPACE }}
        with: 
          openshift_server_url: ${{ secrets.OPENSHIFT_SERVER_URL }}
          openshift_token: ${{ secrets.OPENSHIFT_TOKEN }}
          namespace: ${{ secrets.OPENSHIFT_NAMESPACE }}
      
      - name: Check Status of Rollout
        run: |
          oc rollout status -n ${{ secrets.OPENSHIFT_NAMESPACE }} dc/spr-api --watch
  
  cleanup:
    name: Clean Up Old Objects
    runs-on: ubuntu-latest
    needs: [ healthcheck ]
    steps:
      - name: Login to Openshift
        uses: redhat-actions/oc-login@v1
        env:
          OPENSHIFT_USER: github-actions
          OPENSHIFT_NAMESPACE: ${{ secrets.OPENSHIFT_NAMESPACE }}
        with: 
          openshift_server_url: ${{ secrets.OPENSHIFT_SERVER_URL }}
          openshift_token: ${{ secrets.OPENSHIFT_TOKEN }}
          namespace: ${{ secrets.OPENSHIFT_NAMESPACE }}

      - name: Remove old pod objects
        run: |
          oc delete pod --namespace=${{ secrets.OPENSHIFT_NAMESPACE }} --field-selector status.phase=Succeeded

      - name: Remove old replication controllers
        run: |
          oc delete rc --namespace=${{ secrets.OPENSHIFT_NAMESPACE }} --field-selector status.replicas=0