Github Actions Pipeline for openshift deployment

Use Case

Write a github action job which would build an image and then publish to ghcr. Upon Successful publishing, trigger a openshift deployment


name: OpenShift Deployment

on:
push:
branches:
– main

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
  uses: actions/checkout@v2

- name: Build Docker image
  run: docker build -t my-image:latest .

- name: Log in to Docker registry
  run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin

- name: Push Docker image to GHCR
  run: docker push ghcr.io/${{ github.repository_owner }}/my-image:latest

- name: Set up OpenShift CLI
  uses: redhat-actions/oc-login@v1
  with:
    openshift_server_url: ${{ secrets.OPENSHIFT_SERVER_URL }}
    openshift_token: ${{ secrets.OPENSHIFT_TOKEN }}

- name: Trigger OpenShift deployment
  run: |
    oc project my-project
    oc rollout latest deployment/my-deployment

To use this workflow, you need to set up the following secrets in your GitHub repository:

  • OPENSHIFT_SERVER_URL: The URL of your OpenShift server.
  • OPENSHIFT_TOKEN: An authentication token to access your OpenShift cluster.

Make sure to replace my-image, my-project, and my-deployment with your own image, project, and deployment names.

This workflow listens for pushes to the main branch. When a push event occurs, it checks out the repository, builds a Docker image, logs in to GHCR using the provided GITHUB_TOKEN, pushes the image to GHCR, sets up the OpenShift CLI using the provided secrets, and finally triggers an OpenShift deployment by rolling out the latest changes to the specified deployment.

Ensure that your GitHub Actions runner has the necessary access permissions to push the Docker image to GHCR and trigger the OpenShift deployment.

Rajesh Kumar
Follow me
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x