on:push:branches:["master"]workflow_dispatch:# 允许手动触发工作流env:AZURE_WEBAPP_NAME:your-app-name # set this to your application's nameAZURE_WEBAPP_PACKAGE_PATH:'.'# set this to the path to your web app project, defaults to the repository rootNODE_VERSION:'20.x'# set this to the node version to usepermissions:contents:readjobs:build:runs-on:ubuntu-lateststeps:- uses:actions/checkout@v4- name:Set up Node.jsuses:actions/setup-node@v4with:node-version:${{ env.NODE_VERSION }}cache:'npm'- name:npm install, build, and testrun:| npm install
npm run build --if-present
npm run test --if-present- name:Upload artifact for deployment jobuses:actions/upload-artifact@v3with:name:node-apppath:.deploy:permissions:contents:noneruns-on:ubuntu-latestneeds:buildenvironment:name:'Development'url:${{ steps.deploy-to-webapp.outputs.webapp-url }}steps:- name:Download artifact from build jobuses:actions/download-artifact@v3with:name:node-app- name:'Deploy to Azure WebApp'id:deploy-to-webappuses:azure/webapps-deploy@v2with:app-name:${{ env.AZURE_WEBAPP_NAME }}publish-profile:${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}package:${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
name:Goon:[push]jobs:build:runs-on:ubuntu-lateststrategy:matrix:go-version:['1.19','1.20','1.21.x']steps:- uses:actions/checkout@v4- name:Setup Go ${{ matrix.go-version }}uses:actions/setup-go@v5with:go-version:${{ matrix.go-version }}# You can test your matrix by printing the current Go version- name:Display Go versionrun:go version
# This workflow will build and push a new container image to Amazon ECR,# and then will deploy a new task definition to Amazon ECS, when there is a push to the "master" branch.## To use this workflow, you will need to complete the following set-up steps:## 1. Create an ECR repository to store your images.# For example: `aws ecr create-repository --repository-name my-ecr-repo --region us-east-2`.# Replace the value of the `ECR_REPOSITORY` environment variable in the workflow below with your repository's name.# Replace the value of the `AWS_REGION` environment variable in the workflow below with your repository's region.## 2. Create an ECS task definition, an ECS cluster, and an ECS service.# For example, follow the Getting Started guide on the ECS console:# https://us-east-2.console.aws.amazon.com/ecs/home?region=us-east-2#/firstRun# Replace the value of the `ECS_SERVICE` environment variable in the workflow below with the name you set for the Amazon ECS service.# Replace the value of the `ECS_CLUSTER` environment variable in the workflow below with the name you set for the cluster.## 3. Store your ECS task definition as a JSON file in your repository.# The format should follow the output of `aws ecs register-task-definition --generate-cli-skeleton`.# Replace the value of the `ECS_TASK_DEFINITION` environment variable in the workflow below with the path to the JSON file.# Replace the value of the `CONTAINER_NAME` environment variable in the workflow below with the name of the container# in the `containerDefinitions` section of the task definition.## 4. Store an IAM user access key in GitHub Actions secrets named `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.# See the documentation for each action used below for the recommended IAM policies for this IAM user,# and best practices on handling the access key credentials.name:Deploy to Amazon ECSon:push:branches:["master"]env:AWS_REGION:MY_AWS_REGION # set this to your preferred AWS region, e.g. us-west-1ECR_REPOSITORY:MY_ECR_REPOSITORY # set this to your Amazon ECR repository nameECS_SERVICE:MY_ECS_SERVICE # set this to your Amazon ECS service nameECS_CLUSTER:MY_ECS_CLUSTER # set this to your Amazon ECS cluster nameECS_TASK_DEFINITION:MY_ECS_TASK_DEFINITION# set this to the path to your Amazon ECS task definition# file, e.g. .aws/task-definition.jsonCONTAINER_NAME:MY_CONTAINER_NAME # set this to the name of the container in the# containerDefinitions section of your task definitionpermissions:contents:readjobs:deploy:name:Deployruns-on:ubuntu-latestenvironment:productionsteps:- name:Checkoutuses:actions/checkout@v4- name:Configure AWS credentialsuses:aws-actions/configure-aws-credentials@v1with:aws-access-key-id:${{ secrets.AWS_ACCESS_KEY_ID }}aws-secret-access-key:${{ secrets.AWS_SECRET_ACCESS_KEY }}aws-region:${{ env.AWS_REGION }}- name:Login to Amazon ECRid:login-ecruses:aws-actions/amazon-ecr-login@v1- name:Build, tag, and push image to Amazon ECRid:build-imageenv:ECR_REGISTRY:${{ steps.login-ecr.outputs.registry }}IMAGE_TAG:${{ github.sha }}run:| # Build a docker container and
# push it to ECR so that it can
# be deployed to ECS.
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT- name:Fill in the new image ID in the Amazon ECS task definitionid:task-defuses:aws-actions/amazon-ecs-render-task-definition@v1with:task-definition:${{ env.ECS_TASK_DEFINITION }}container-name:${{ env.CONTAINER_NAME }}image:${{ steps.build-image.outputs.image }}- name:Deploy Amazon ECS task definitionuses:aws-actions/amazon-ecs-deploy-task-definition@v1with:task-definition:${{ steps.task-def.outputs.task-definition }}service:${{ env.ECS_SERVICE }}cluster:${{ env.ECS_CLUSTER }}wait-for-service-stability:true
# This workflow will build and push an application to a Azure Kubernetes Service (AKS) cluster when you push your code## This workflow assumes you have already created the target AKS cluster and have created an Azure Container Registry (ACR)# The ACR should be attached to the AKS cluster# For instructions see:# - https://docs.microsoft.com/en-us/azure/aks/kubernetes-walkthrough-portal# - https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal# - https://learn.microsoft.com/en-us/azure/aks/cluster-container-registry-integration?tabs=azure-cli#configure-acr-integration-for-existing-aks-clusters# - https://github.com/Azure/aks-create-action## To configure this workflow:## 1. Set the following secrets in your repository (instructions for getting these# https://docs.microsoft.com/en-us/azure/developer/github/connect-from-azure?tabs=azure-cli%2Clinux)):# - AZURE_CLIENT_ID# - AZURE_TENANT_ID# - AZURE_SUBSCRIPTION_ID## 2. Set the following environment variables (or replace the values below):# - AZURE_CONTAINER_REGISTRY (name of your container registry / ACR)# - CONTAINER_NAME (name of the container image you would like to push up to your ACR)# - RESOURCE_GROUP (where your cluster is deployed)# - CLUSTER_NAME (name of your AKS cluster)# - IMAGE_PULL_SECRET_NAME (name of the ImagePullSecret that will be created to pull your ACR image)## 3. Choose the appropriate render engine for the bake step https://github.com/Azure/k8s-bake. The config below assumes Helm.# Set your helmChart, overrideFiles, overrides, and helm-version to suit your configuration.# - CHART_PATH (path to your helm chart)# - CHART_OVERRIDE_PATH (path to your helm chart with override values)## For more information on GitHub Actions for Azure, refer to https://github.com/Azure/Actions# For more samples to get started with GitHub Action workflows to deploy to Azure, refer to https://github.com/Azure/actions-workflow-samples# For more options with the actions used below please refer to https://github.com/Azure/loginname:Build and deploy an app to AKS with Helmon:push:branches:["master"]workflow_dispatch:env:AZURE_CONTAINER_REGISTRY:"your-azure-container-registry"CONTAINER_NAME:"your-container-name"RESOURCE_GROUP:"your-resource-group"CLUSTER_NAME:"your-cluster-name"CHART_PATH:"your-chart-path"CHART_OVERRIDE_PATH:"your-chart-override-path"jobs:buildImage:permissions:contents:readid-token:writeruns-on:ubuntu-lateststeps:# Checks out the repository this file is in- uses:actions/checkout@v4# Logs in with your Azure credentials- name:Azure loginuses:azure/login@v1.4.6with:client-id:${{ secrets.AZURE_CLIENT_ID }}tenant-id:${{ secrets.AZURE_TENANT_ID }}subscription-id:${{ secrets.AZURE_SUBSCRIPTION_ID }}# Builds and pushes an image up to your Azure Container Registry- name:Build and push image to ACRrun:| az acr build --image ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }} --registry ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} .deploy:permissions:actions:readcontents:readid-token:writeruns-on:ubuntu-latestneeds:[buildImage]steps:# Checks out the repository this file is in- uses:actions/checkout@v4# Logs in with your Azure credentials- name:Azure loginuses:azure/login@v1.4.6with:client-id:${{ secrets.AZURE_CLIENT_ID }}tenant-id:${{ secrets.AZURE_TENANT_ID }}subscription-id:${{ secrets.AZURE_SUBSCRIPTION_ID }}# Use kubelogin to configure your kubeconfig for Azure auth- name:Set up kubelogin for non-interactive loginuses:azure/use-kubelogin@v1with:kubelogin-version:'v0.0.25'# Retrieves your Azure Kubernetes Service cluster's kubeconfig file- name:Get K8s contextuses:azure/aks-set-context@v3with:resource-group:${{ env.RESOURCE_GROUP }}cluster-name:${{ env.CLUSTER_NAME }}admin:'false'use-kubelogin:'true'# Runs Helm to create manifest files- name:Bake deploymentuses:azure/k8s-bake@v2with:renderEngine:"helm"helmChart:${{ env.CHART_PATH }}overrideFiles:${{ env.CHART_OVERRIDE_PATH }}overrides:| replicas:2helm-version:"latest"id:bake# Deploys application based on manifest files from previous step- name:Deploy applicationuses:Azure/k8s-deploy@v4with:action:deploymanifests:${{ steps.bake.outputs.manifestsBundle }}images:| ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }}
这个GitHub Actions工作流文件定义了一个CI/CD流程,用于将Docker镜像构建并推送到Azure容器注册表(ACR),然后将应用程序部署到Azure Kubernetes Service (AKS)集群。
主要步骤包括:检出代码、使用Azure凭证登录、构建并推送Docker镜像到ACR、获取AKS集群的kubeconfig文件、使用Helm渲染部署清单文件、部署应用程序。
# This workflow will build and push a new container image to Alibaba Cloud Container Registry (ACR),# and then will deploy it to Alibaba Cloud Container Service for Kubernetes (ACK), when there is a push to the "master" branch.## To use this workflow, you will need to complete the following set-up steps:## 1. Create an ACR repository to store your container images.# You can use ACR EE instance for more security and better performance.# For instructions see https://www.alibabacloud.com/help/doc-detail/142168.htm## 2. Create an ACK cluster to run your containerized application.# You can use ACK Pro cluster for more security and better performance.# For instructions see https://www.alibabacloud.com/help/doc-detail/95108.htm## 3. Store your AccessKey pair in GitHub Actions secrets named `ACCESS_KEY_ID` and `ACCESS_KEY_SECRET`.# For instructions on setting up secrets see: https://developer.github.com/actions/managing-workflows/storing-secrets/## 4. Change the values for the REGION_ID, REGISTRY, NAMESPACE, IMAGE, ACK_CLUSTER_ID, and ACK_DEPLOYMENT_NAME.#name:Build and Deploy to ACKon:push:branches:["master"]# Environment variables available to all jobs and steps in this workflow.env:REGION_ID:cn-hangzhouREGISTRY:registry.cn-hangzhou.aliyuncs.comNAMESPACE:namespaceIMAGE:repoTAG:${{ github.sha }}ACK_CLUSTER_ID:clusterIDACK_DEPLOYMENT_NAME:nginx-deploymentACR_EE_REGISTRY:myregistry.cn-hangzhou.cr.aliyuncs.comACR_EE_INSTANCE_ID:instanceIDACR_EE_NAMESPACE:namespaceACR_EE_IMAGE:repoACR_EE_TAG:${{ github.sha }}permissions:contents:readjobs:build:runs-on:ubuntu-latestenvironment:productionsteps:- name:Checkoutuses:actions/checkout@v4# 1.1 Login to ACR- name:Login to ACR with the AccessKey pairuses:aliyun/acr-login@v1with:region-id:"${{ env.REGION_ID }}"access-key-id:"${{ secrets.ACCESS_KEY_ID }}"access-key-secret:"${{ secrets.ACCESS_KEY_SECRET }}"# 1.2 Build and push image to ACR- name:Build and push image to ACRrun:| docker build --tag "$REGISTRY/$NAMESPACE/$IMAGE:$TAG" .
docker push "$REGISTRY/$NAMESPACE/$IMAGE:$TAG"# 1.3 Scan image in ACR- name:Scan image in ACRuses:aliyun/acr-scan@v1with:region-id:"${{ env.REGION_ID }}"access-key-id:"${{ secrets.ACCESS_KEY_ID }}"access-key-secret:"${{ secrets.ACCESS_KEY_SECRET }}"repository:"${{ env.NAMESPACE }}/${{ env.IMAGE }}"tag:"${{ env.TAG }}"# 2.1 (Optional) Login to ACR EE- uses:actions/checkout@v4- name:Login to ACR EE with the AccessKey pairuses:aliyun/acr-login@v1with:login-server:"https://${{ env.ACR_EE_REGISTRY }}"region-id:"${{ env.REGION_ID }}"access-key-id:"${{ secrets.ACCESS_KEY_ID }}"access-key-secret:"${{ secrets.ACCESS_KEY_SECRET }}"instance-id:"${{ env.ACR_EE_INSTANCE_ID }}"# 2.2 (Optional) Build and push image ACR EE- name:Build and push image to ACR EErun:| docker build -t "$ACR_EE_REGISTRY/$ACR_EE_NAMESPACE/$ACR_EE_IMAGE:$TAG" .
docker push "$ACR_EE_REGISTRY/$ACR_EE_NAMESPACE/$ACR_EE_IMAGE:$TAG"# 2.3 (Optional) Scan image in ACR EE- name:Scan image in ACR EEuses:aliyun/acr-scan@v1with:region-id:"${{ env.REGION_ID }}"access-key-id:"${{ secrets.ACCESS_KEY_ID }}"access-key-secret:"${{ secrets.ACCESS_KEY_SECRET }}"instance-id:"${{ env.ACR_EE_INSTANCE_ID }}"repository:"${{ env.ACR_EE_NAMESPACE}}/${{ env.ACR_EE_IMAGE }}"tag:"${{ env.ACR_EE_TAG }}"# 3.1 Set ACK context- name:Set K8s contextuses:aliyun/ack-set-context@v1with:access-key-id:"${{ secrets.ACCESS_KEY_ID }}"access-key-secret:"${{ secrets.ACCESS_KEY_SECRET }}"cluster-id:"${{ env.ACK_CLUSTER_ID }}"# 3.2 Deploy the image to the ACK cluster- name:Set up Kustomizerun:|- curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash /dev/stdin 3.8.6- name:Deployrun:|- ./kustomize edit set image REGISTRY/NAMESPACE/IMAGE:TAG=$REGISTRY/$NAMESPACE/$IMAGE:$TAG
./kustomize build . | kubectl apply -f -
kubectl rollout status deployment/$ACK_DEPLOYMENT_NAME
kubectl get services -o wide
# This workflow will build a docker container, publish it to Google Container# Registry, and deploy it to GKE when there is a push to the "master"# branch.## To configure this workflow:## 1. Enable the following Google Cloud APIs:## - Artifact Registry (artifactregistry.googleapis.com)# - Google Kubernetes Engine (container.googleapis.com)# - IAM Credentials API (iamcredentials.googleapis.com)## You can learn more about enabling APIs at# https://support.google.com/googleapi/answer/6158841.## 2. Ensure that your repository contains the necessary configuration for your# Google Kubernetes Engine cluster, including deployment.yml,# kustomization.yml, service.yml, etc.## 3. Create and configure a Workload Identity Provider for GitHub:# https://github.com/google-github-actions/auth#preferred-direct-workload-identity-federation.## Depending on how you authenticate, you will need to grant an IAM principal# permissions on Google Cloud:## - Artifact Registry Administrator (roles/artifactregistry.admin)# - Kubernetes Engine Developer (roles/container.developer)## You can learn more about setting IAM permissions at# https://cloud.google.com/iam/docs/manage-access-other-resources## 5. Change the values in the "env" block to match your values.name:'Build and Deploy to GKE'on:push:branches:- '"master"'env:PROJECT_ID: 'my-project' # TODO:update to your Google Cloud project IDGAR_LOCATION: 'us-central1' # TODO:update to your regionGKE_CLUSTER: 'cluster-1' # TODO:update to your cluster nameGKE_ZONE: 'us-central1-c' # TODO:update to your cluster zoneDEPLOYMENT_NAME: 'gke-test' # TODO:update to your deployment nameREPOSITORY: 'samples' # TODO:update to your Artifact Registry docker repository nameIMAGE:'static-site'WORKLOAD_IDENTITY_PROVIDER: 'projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider' # TODO:update to your workload identity providerjobs:setup-build-publish-deploy:name:'Setup, Build, Publish, and Deploy'runs-on:'ubuntu-latest'environment:'production'permissions:contents:'read'id-token:'write'steps:- name:'Checkout'uses:'actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332'# actions/checkout@v4# Configure Workload Identity Federation and generate an access token.## See https://github.com/google-github-actions/auth for more options,# including authenticating via a JSON credentials file.- id:'auth'name:'Authenticate to Google Cloud'uses:'google-github-actions/auth@f112390a2df9932162083945e46d439060d66ec2'# google-github-actions/auth@v2with:workload_identity_provider:'${{ env.WORKLOAD_IDENTITY_PROVIDER }}'# Authenticate Docker to Google Cloud Artifact Registry- name:'Docker Auth'uses:'docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567'# docker/login-action@v3with:username:'oauth2accesstoken'password:'${{ steps.auth.outputs.auth_token }}'registry:'${{ env.GAR_LOCATION }}-docker.pkg.dev'# Get the GKE credentials so we can deploy to the cluster- name:'Set up GKE credentials'uses:'google-github-actions/get-gke-credentials@6051de21ad50fbb1767bc93c11357a49082ad116'# google-github-actions/get-gke-credentials@v2with:cluster_name:'${{ env.GKE_CLUSTER }}'location:'${{ env.GKE_ZONE }}'# Build the Docker image- name:'Build and push Docker container'run:|- DOCKER_TAG="${GAR_LOCATION}-docker.pkg.dev/${PROJECT_ID}/${REPOSITORY}/${IMAGE}:${GITHUB_SHA}"
docker build \
--tag "${DOCKER_TAG}" \
--build-arg GITHUB_SHA="${GITHUB_SHA}" \
--build-arg GITHUB_REF="${GITHUB_REF}" \
.
docker push "${DOCKER_TAG}"# Set up kustomize- name:'Set up Kustomize'run:|- curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv5.4.3/kustomize_v5.4.3_linux_amd64.tar.gz
chmod u+x ./kustomize# Deploy the Docker image to the GKE cluster- name:'Deploy to GKE'run:|- # replacing the image name in the k8s template
./kustomize edit set image LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE:TAG=$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA
./kustomize build . | kubectl apply -f -
kubectl rollout status deployment/$DEPLOYMENT_NAME
kubectl get services -o wide