This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
add GITHUB_TOKEN #301
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 "staging" branch. | |
# | |
# To configure this workflow: | |
# | |
# 1. Ensure that your repository contains the necessary configuration for your Google Kubernetes Engine cluster, including deployment.yml, kustomization.yml, service.yml, etc. | |
# | |
# 2. Create and configure a Workload Identity Provider for GitHub (https://github.com/google-github-actions/auth#setting-up-workload-identity-federation) | |
# | |
# 3. Change the values for the GAR_LOCATION, GKE_ZONE, GKE_CLUSTER, IMAGE, REPOSITORY and DEPLOYMENT_NAME environment variables (below). | |
# | |
# For more support on how to run the workflow, please visit https://github.com/google-github-actions/setup-gcloud/tree/master/example-workflows/gke-kustomize | |
name: Build and Deploy Staging to GKE | |
on: | |
push: | |
branches: [ "staging" ] | |
workflow_dispatch: | |
env: | |
PROJECT_ID: ${{ secrets.GKE_PROJECT }} | |
GAR_LOCATION: us-east1 | |
GKE_CLUSTER: freenet-org-east1 | |
GKE_ZONE: us-east1-b | |
DEPLOYMENT_NAME: freenet-org-staging | |
REPOSITORY: freenet-repo | |
IMAGE: freenet-org-staging | |
NAMESPACE: freenetorg | |
jobs: | |
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@v3 | |
# Configure Workload Identity Federation and generate an access token. | |
- id: 'auth' | |
name: 'Authenticate to Google Cloud' | |
uses: 'google-github-actions/auth@v0' | |
with: | |
token_format: 'access_token' | |
workload_identity_provider: 'projects/516912345376/locations/global/workloadIdentityPools/freenetorg-pool/providers/github' | |
service_account: 'gke-service-acct@freenet-353920.iam.gserviceaccount.com' | |
- name: Docker configuration | |
run: |- | |
echo ${{steps.auth.outputs.access_token}} | docker login -u oauth2accesstoken --password-stdin https://$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@v0 | |
with: | |
cluster_name: ${{ env.GKE_CLUSTER }} | |
location: ${{ env.GAR_LOCATION }} | |
# Build the Docker image | |
- name: Build | |
run: |- | |
docker build \ | |
--tag "$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA" \ | |
--build-arg GITHUB_SHA="$GITHUB_SHA" \ | |
--build-arg GITHUB_REF="$GITHUB_REF" \ | |
. | |
# Push the Docker image to Google Artifact Registry | |
- name: Publish | |
run: |- | |
docker push "$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA" | |
# Set up kustomize | |
- name: Set up Kustomize | |
run: |- | |
cd deployment/base/ | |
curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64 | |
chmod u+x ./kustomize | |
# Deploy the Docker image to the GKE cluster | |
- name: Deploy | |
run: |- | |
# replacing the image name in the k8s template | |
cd deployment/staging/ | |
../base/kustomize edit set image $GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA | |
../base/kustomize build . | kubectl apply -f - | |
kubectl rollout status deployment/$DEPLOYMENT_NAME -n $NAMESPACE | |
kubectl get services -o wide |