|
| 1 | +# This workflow will build and push a new container image to Alibaba Cloud Container Registry (ACR), |
| 2 | +# and then will deploy it to Alibaba Cloud Container Service for Kubernetes (ACK), when there is a push to the master branch. |
| 3 | +# |
| 4 | +# To use this workflow, you will need to complete the following set-up steps: |
| 5 | +# |
| 6 | +# 1. Create an ACR repository to store your container images. |
| 7 | +# You can use ACR EE instance for more security and better performance. |
| 8 | +# For instructions see https://www.alibabacloud.com/help/doc-detail/142168.htm |
| 9 | +# |
| 10 | +# 2. Create an ACK cluster to run your containerized application. |
| 11 | +# You can use ACK Pro cluster for more security and better performance. |
| 12 | +# For instructions see https://www.alibabacloud.com/help/doc-detail/95108.htm |
| 13 | +# |
| 14 | +# 3. Store your AccessKey pair in GitHub Actions secrets named `ACCESS_KEY_ID` and `ACCESS_KEY_SECRET`. |
| 15 | +# For instructions on setting up secrets see: https://developer.github.com/actions/managing-workflows/storing-secrets/ |
| 16 | +# |
| 17 | +# 4. Change the values for the REGION_ID, REGISTRY, NAMESPACE, IMAGE, ACK_CLUSTER_ID, and ACK_DEPLOYMENT_NAME. |
| 18 | +# |
| 19 | + |
| 20 | +name: Build and Deploy to ACK |
| 21 | + |
| 22 | +on: |
| 23 | + push: |
| 24 | + branches: |
| 25 | + - master |
| 26 | + |
| 27 | +# Environment variables available to all jobs and steps in this workflow. |
| 28 | +env: |
| 29 | + REGION_ID: cn-hangzhou |
| 30 | + REGISTRY: registry.cn-hangzhou.aliyuncs.com |
| 31 | + NAMESPACE: namespace |
| 32 | + IMAGE: repo |
| 33 | + TAG: ${{ github.sha }} |
| 34 | + ACK_CLUSTER_ID: clusterID |
| 35 | + ACK_DEPLOYMENT_NAME: nginx-deployment |
| 36 | + |
| 37 | + ACR_EE_REGISTRY: myregistry.cn-hangzhou.cr.aliyuncs.com |
| 38 | + ACR_EE_INSTANCE_ID: instanceID |
| 39 | + ACR_EE_NAMESPACE: namespace |
| 40 | + ACR_EE_IMAGE: repo |
| 41 | + ACR_EE_TAG: ${{ github.sha }} |
| 42 | + |
| 43 | +permissions: |
| 44 | + contents: read |
| 45 | + |
| 46 | +jobs: |
| 47 | + build: |
| 48 | + runs-on: ubuntu-latest |
| 49 | + environment: production |
| 50 | + |
| 51 | + steps: |
| 52 | + - name: Checkout |
| 53 | + uses: actions/checkout@v3 |
| 54 | + |
| 55 | + # 1.1 Login to ACR |
| 56 | + - name: Login to ACR with the AccessKey pair |
| 57 | + uses: aliyun/acr-login@v1 |
| 58 | + with: |
| 59 | + region-id: "${{ env.REGION_ID }}" |
| 60 | + access-key-id: "${{ secrets.ACCESS_KEY_ID }}" |
| 61 | + access-key-secret: "${{ secrets.ACCESS_KEY_SECRET }}" |
| 62 | + |
| 63 | + # 1.2 Buid and push image to ACR |
| 64 | + - name: Build and push image to ACR |
| 65 | + run: | |
| 66 | + docker build --tag "$REGISTRY/$NAMESPACE/$IMAGE:$TAG" . |
| 67 | + docker push "$REGISTRY/$NAMESPACE/$IMAGE:$TAG" |
| 68 | + |
| 69 | + # 1.3 Scan image in ACR |
| 70 | + - name: Scan image in ACR |
| 71 | + uses: aliyun/acr-scan@v1 |
| 72 | + with: |
| 73 | + region-id: "${{ env.REGION_ID }}" |
| 74 | + access-key-id: "${{ secrets.ACCESS_KEY_ID }}" |
| 75 | + access-key-secret: "${{ secrets.ACCESS_KEY_SECRET }}" |
| 76 | + repository: "${{ env.NAMESPACE }}/${{ env.IMAGE }}" |
| 77 | + tag: "${{ env.TAG }}" |
| 78 | + |
| 79 | + # 2.1 (Optional) Login to ACR EE |
| 80 | + - uses: actions/checkout@v3 |
| 81 | + - name: Login to ACR EE with the AccessKey pair |
| 82 | + uses: aliyun/acr-login@v1 |
| 83 | + with: |
| 84 | + login-server: "https://${{ env.ACR_EE_REGISTRY }}" |
| 85 | + region-id: "${{ env.REGION_ID }}" |
| 86 | + access-key-id: "${{ secrets.ACCESS_KEY_ID }}" |
| 87 | + access-key-secret: "${{ secrets.ACCESS_KEY_SECRET }}" |
| 88 | + instance-id: "${{ env.ACR_EE_INSTANCE_ID }}" |
| 89 | + |
| 90 | + # 2.2 (Optional) Build and push image ACR EE |
| 91 | + - name: Build and push image to ACR EE |
| 92 | + run: | |
| 93 | + docker build -t "$ACR_EE_REGISTRY/$ACR_EE_NAMESPACE/$ACR_EE_IMAGE:$TAG" . |
| 94 | + docker push "$ACR_EE_REGISTRY/$ACR_EE_NAMESPACE/$ACR_EE_IMAGE:$TAG" |
| 95 | + # 2.3 (Optional) Scan image in ACR EE |
| 96 | + - name: Scan image in ACR EE |
| 97 | + uses: aliyun/acr-scan@v1 |
| 98 | + with: |
| 99 | + region-id: "${{ env.REGION_ID }}" |
| 100 | + access-key-id: "${{ secrets.ACCESS_KEY_ID }}" |
| 101 | + access-key-secret: "${{ secrets.ACCESS_KEY_SECRET }}" |
| 102 | + instance-id: "${{ env.ACR_EE_INSTANCE_ID }}" |
| 103 | + repository: "${{ env.ACR_EE_NAMESPACE}}/${{ env.ACR_EE_IMAGE }}" |
| 104 | + tag: "${{ env.ACR_EE_TAG }}" |
| 105 | + |
| 106 | + # 3.1 Set ACK context |
| 107 | + - name: Set K8s context |
| 108 | + uses: aliyun/ack-set-context@v1 |
| 109 | + with: |
| 110 | + access-key-id: "${{ secrets.ACCESS_KEY_ID }}" |
| 111 | + access-key-secret: "${{ secrets.ACCESS_KEY_SECRET }}" |
| 112 | + cluster-id: "${{ env.ACK_CLUSTER_ID }}" |
| 113 | + |
| 114 | + # 3.2 Deploy the image to the ACK cluster |
| 115 | + - name: Set up Kustomize |
| 116 | + run: |- |
| 117 | + curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash /dev/stdin 3.8.6 |
| 118 | + - name: Deploy |
| 119 | + run: |- |
| 120 | + ./kustomize edit set image REGISTRY/NAMESPACE/IMAGE:TAG=$REGISTRY/$NAMESPACE/$IMAGE:$TAG |
| 121 | + ./kustomize build . | kubectl apply -f - |
| 122 | + kubectl rollout status deployment/$ACK_DEPLOYMENT_NAME |
| 123 | + kubectl get services -o wide |
0 commit comments