| 
 | 1 | +# This workflow will build a docker container, publish it to IBM Container Registry, and deploy it to IKS when there is a push to the "main" branch.  | 
 | 2 | +#  | 
 | 3 | +# To configure this workflow:  | 
 | 4 | +#  | 
 | 5 | +# 1. Ensure that your repository contains a Dockerfile  | 
 | 6 | +# 2. Setup secrets in your repository by going to settings: Create ICR_NAMESPACE and IBM_CLOUD_API_KEY  | 
 | 7 | +# 3. Change the values for the IBM_CLOUD_REGION, REGISTRY_HOSTNAME, IMAGE_NAME, IKS_CLUSTER, DEPLOYMENT_NAME, and PORT  | 
 | 8 | + | 
 | 9 | +name: Build and Deploy to IKS  | 
 | 10 | + | 
 | 11 | +on:  | 
 | 12 | +  push:  | 
 | 13 | +    branches: [ "main" ]  | 
 | 14 | + | 
 | 15 | +# Environment variables available to all jobs and steps in this workflow  | 
 | 16 | +env:  | 
 | 17 | +  GITHUB_SHA: ${{ github.sha }}  | 
 | 18 | +  IBM_CLOUD_API_KEY: ${{ secrets.IBM_CLOUD_API_KEY }}  | 
 | 19 | +  IBM_CLOUD_REGION: us-south  | 
 | 20 | +  ICR_NAMESPACE: ${{ us.icr.io/sn-labs-abreu760 }}  | 
 | 21 | +  REGISTRY_HOSTNAME: us.icr.io  | 
 | 22 | +  IMAGE_NAME: iks-test  | 
 | 23 | +  IKS_CLUSTER: example-iks-cluster-name-or-id  | 
 | 24 | +  DEPLOYMENT_NAME: iks-test  | 
 | 25 | +  PORT: 5001  | 
 | 26 | + | 
 | 27 | +jobs:  | 
 | 28 | +  setup-build-publish-deploy:  | 
 | 29 | +    name: Setup, Build, Publish, and Deploy  | 
 | 30 | +    runs-on: ubuntu-latest  | 
 | 31 | +    environment: production  | 
 | 32 | +    steps:  | 
 | 33 | + | 
 | 34 | +    - name: Checkout  | 
 | 35 | +      uses: actions/checkout@v4  | 
 | 36 | + | 
 | 37 | +    # Download and Install IBM Cloud CLI  | 
 | 38 | +    - name: Install IBM Cloud CLI  | 
 | 39 | +      run: |  | 
 | 40 | +        curl -fsSL https://clis.cloud.ibm.com/install/linux | sh  | 
 | 41 | +        ibmcloud --version  | 
 | 42 | +        ibmcloud config --check-version=false  | 
 | 43 | +        ibmcloud plugin install -f kubernetes-service  | 
 | 44 | +        ibmcloud plugin install -f container-registry  | 
 | 45 | +
  | 
 | 46 | +    # Authenticate with IBM Cloud CLI  | 
 | 47 | +    - name: Authenticate with IBM Cloud CLI  | 
 | 48 | +      run: |  | 
 | 49 | +        ibmcloud login --apikey "${IBM_CLOUD_API_KEY}" -r "${IBM_CLOUD_REGION}" -g default  | 
 | 50 | +        ibmcloud cr region-set "${IBM_CLOUD_REGION}"  | 
 | 51 | +        ibmcloud cr login  | 
 | 52 | +
  | 
 | 53 | +    # Build the Docker image  | 
 | 54 | +    - name: Build with Docker  | 
 | 55 | +      run: |  | 
 | 56 | +        docker build -t "$REGISTRY_HOSTNAME"/"$ICR_NAMESPACE"/"$IMAGE_NAME":"$GITHUB_SHA" \  | 
 | 57 | +          --build-arg GITHUB_SHA="$GITHUB_SHA" \  | 
 | 58 | +          --build-arg GITHUB_REF="$GITHUB_REF" .  | 
 | 59 | +
  | 
 | 60 | +    # Push the image to IBM Container Registry  | 
 | 61 | +    - name: Push the image to ICR  | 
 | 62 | +      run: |  | 
 | 63 | +        docker push $REGISTRY_HOSTNAME/$ICR_NAMESPACE/$IMAGE_NAME:$GITHUB_SHA  | 
 | 64 | +
  | 
 | 65 | +    # Deploy the Docker image to the IKS cluster  | 
 | 66 | +    - name: Deploy to IKS  | 
 | 67 | +      run: |  | 
 | 68 | +        ibmcloud ks cluster config --cluster $IKS_CLUSTER  | 
 | 69 | +        kubectl config current-context  | 
 | 70 | +        kubectl create deployment $DEPLOYMENT_NAME --image=$REGISTRY_HOSTNAME/$ICR_NAMESPACE/$IMAGE_NAME:$GITHUB_SHA --dry-run -o yaml > deployment.yaml  | 
 | 71 | +        kubectl apply -f deployment.yaml  | 
 | 72 | +        kubectl rollout status deployment/$DEPLOYMENT_NAME  | 
 | 73 | +        kubectl create service loadbalancer $DEPLOYMENT_NAME --tcp=80:$PORT --dry-run -o yaml > service.yaml  | 
 | 74 | +        kubectl apply -f service.yaml  | 
 | 75 | +        kubectl get services -o wide  | 
0 commit comments