|
| 1 | +# This workflow will build a docker container, publish it to IBM Container Registry, and deploy it to IKS when a release is created |
| 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 | + release: |
| 13 | + types: [created] |
| 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: ${{ secrets.ICR_NAMESPACE }} |
| 21 | + REGISTRY_HOSTNAME: us.icr.io |
| 22 | + IMAGE_NAME: iks-test |
| 23 | + IKS_CLUSTER: br81hufw0csdd57q7gug |
| 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 | + steps: |
| 32 | + |
| 33 | + - name: Checkout |
| 34 | + uses: actions/checkout@v2 |
| 35 | + |
| 36 | + # Download and Install IBM Cloud CLI |
| 37 | + - name: Install IBM Cloud CLI |
| 38 | + run: | |
| 39 | + curl -fsSL https://clis.cloud.ibm.com/install/linux | sh |
| 40 | + ibmcloud --version |
| 41 | + ibmcloud config --check-version=false |
| 42 | + ibmcloud plugin install -f kubernetes-service |
| 43 | + ibmcloud plugin install -f container-registry |
| 44 | +
|
| 45 | + # Authenticate with IBM Cloud CLI |
| 46 | + - name: Authenticate with IBM Cloud CLI |
| 47 | + run: | |
| 48 | + ibmcloud login --apikey "${IBM_CLOUD_API_KEY}" -r "${IBM_CLOUD_REGION}" -g default |
| 49 | + ibmcloud cr region-set "${IBM_CLOUD_REGION}" |
| 50 | + ibmcloud cr login |
| 51 | +
|
| 52 | + # Build the Docker image |
| 53 | + - name: Build with Docker |
| 54 | + run: | |
| 55 | + docker build -t "$REGISTRY_HOSTNAME"/"$ICR_NAMESPACE"/"$IMAGE_NAME":"$GITHUB_SHA" \ |
| 56 | + --build-arg GITHUB_SHA="$GITHUB_SHA" \ |
| 57 | + --build-arg GITHUB_REF="$GITHUB_REF" . |
| 58 | +
|
| 59 | + # Push the image to IBM Container Registry |
| 60 | + - name: Push the image to ICR |
| 61 | + run: | |
| 62 | + docker push $REGISTRY_HOSTNAME/$ICR_NAMESPACE/$IMAGE_NAME:$GITHUB_SHA |
| 63 | +
|
| 64 | + # Deploy the Docker image to the IKS cluster |
| 65 | + - name: Deploy to IKS |
| 66 | + run: | |
| 67 | + ibmcloud ks cluster config --cluster $IKS_CLUSTER |
| 68 | + kubectl config current-context |
| 69 | + kubectl create deployment $DEPLOYMENT_NAME --image=$REGISTRY_HOSTNAME/$ICR_NAMESPACE/$IMAGE_NAME:$GITHUB_SHA --dry-run -o yaml > deployment.yaml |
| 70 | + kubectl apply -f deployment.yaml |
| 71 | + kubectl rollout status deployment/$DEPLOYMENT_NAME |
| 72 | + kubectl create service loadbalancer $DEPLOYMENT_NAME --tcp=80:$PORT --dry-run -o yaml > service.yaml |
| 73 | + kubectl apply -f service.yaml |
| 74 | + kubectl get services -o wide |
0 commit comments