Skip to content

Commit

Permalink
Merge branch 'staging' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
kelyacf authored Aug 21, 2024
2 parents 5dbbe05 + 8d451ad commit 6d487d4
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 46 deletions.
70 changes: 70 additions & 0 deletions .github/workflows/google-run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

name: Build and Deploy to Cloud Run

on:
push:
branches: [ "staging" ]

env:
PROJECT_ID: ${{ secrets.GKE_PROJECT }}
GAR_LOCATION: us-central1
SERVICE_NAME: cloud-run-service
REPOSITORY: docker-repository
IMAGE: geppetto

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

# Configurar gcloud CLI
- name: Setup gcloud CLI
uses: google-github-actions/setup-gcloud@v1
with:
version: 'latest'
service_account_key: '${{ secrets.GCLOUD_KEY_JSON }}'

# Autenticar en Google Cloud
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@v2'
with:
credentials_json: '${{ secrets.GCLOUD_KEY_JSON }}'
token_format: 'access_token'

- name: Docker configuration
env:
ACCESS_TOKEN: ${{ steps.auth.outputs.access_token }}
run: echo $ACCESS_TOKEN | docker login -u oauth2accesstoken --password-stdin https://us-central1-docker.pkg.dev

- name: Log access token
run: echo "Access Token:${{ steps.auth.outputs.access_token }}"

- name: Build
run: |
docker build \
--tag "us-central1-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE }}:${{ github.sha }}" \
--build-arg GITHUB_SHA="${{ github.sha }}" \
--build-arg GITHUB_REF="${{ github.ref }}" \
.
- name: Publish
run: |
docker push "us-central1-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE }}:${{ github.sha }}"
- name: Deploy to Cloud Run
run: |
gcloud run deploy ${{ env.SERVICE_NAME }} \
--image=us-central1-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE }}:${{ github.sha }} \
--region=${{ env.GAR_LOCATION }} \
--platform=managed \
--allow-unauthenticated
47 changes: 47 additions & 0 deletions .github/workflows/google-run2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Build and Deploy Locally

on:
push:
branches: [ "staging" ]

env:
IMAGE: geppetto
SERVICE_NAME: cloud-run-service

jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
environment: production

permissions:
contents: 'read'

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Log in to Docker Hub
run: echo "${{ secrets.DOCKER_HUB_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_HUB_USERNAME }}" --password-stdin

- name: Build Docker Image
run: |
docker build \
--tag "${{ env.IMAGE }}:${{ github.sha }}" \
--build-arg GITHUB_SHA="${{ github.sha }}" \
--build-arg GITHUB_REF="${{ github.ref }}" \
.
- name: Push Docker Image to Docker Hub
run: |
docker tag "${{ env.IMAGE }}:${{ github.sha }}" "${{ secrets.DOCKER_HUB_USERNAME }}/${{ env.IMAGE }}:${{ github.sha }}"
docker push "${{ secrets.DOCKER_HUB_USERNAME }}/${{ env.IMAGE }}:${{ github.sha }}"
- name: Deploy Docker Container Locally
run: |
docker run -d --name ${{ env.SERVICE_NAME }} -p 8080:8080 "${{ secrets.DOCKER_HUB_USERNAME }}/${{ env.IMAGE }}:${{ github.sha }}"
docker run -d --name ${{ env.SERVICE_NAME }} -p 8080:8080 "${{ secrets.DOCKER_HUB_USERNAME }}/${{ env.IMAGE }}:${{ github.sha }}"
97 changes: 55 additions & 42 deletions .github/workflows/google.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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 "main" branch.
#
# To configure this workflow:
# 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.
#
Expand All @@ -17,12 +17,12 @@ on:
branches: [ "staging" ]

env:
PROJECT_ID: ${{ secrets.GKE_PROJECT }} # GKE_PROJECT creado en secrets del proyecto - Devops-Tools
GAR_LOCATION: us-central1 # Region del Artifactor
GKE_CLUSTER: autopilot-cluster-1 # Cree un nuevo cluster de Kubernetes
GKE_ZONE: us-central1 # Zona del cluster
DEPLOYMENT_NAME: gke-test # TODO: update to deployment name
REPOSITORY: docker-repository # Cree un nuevo repositorio donde se almacenara la imagen de Docker
PROJECT_ID: ${{ secrets.GKE_PROJECT }}
GAR_LOCATION: us-central1
GKE_CLUSTER: autopilot-cluster-1
GKE_ZONE: us-central1
DEPLOYMENT_NAME: gke-test
REPOSITORY: docker-repository
IMAGE: geppetto

jobs:
Expand All @@ -39,55 +39,68 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

# Configure Workload Identity Federation and generate an access token.
# Configurar gcloud CLI
- name: Setup gcloud CLI
uses: google-github-actions/setup-gcloud@v1
with:
version: 'latest'
service_account_key: '${{ secrets.GCLOUD_KEY_JSON }}'

# Verificar que el archivo de credenciales existe y es válido
#- name: Write credentials file
# run: echo "${{ secrets.GCLOUD_KEY_JSON }}" > $HOME/gcloud-key.json

#- name: Verify credentials file content
# run: |
# echo "Contents of gcloud-key.json:"
# cat $HOME/gcloud-key.json
# shell: bash


# Autenticar gcloud
#- name: Authenticate to gcloud
# run: gcloud auth activate-service-account --key-file=$HOME/gcloud-key.json

# Autenticar en Google Cloud
- id: 'auth'
name: 'Authenticate to Google Cloud'
uses: 'google-github-actions/auth@v0'
uses: 'google-github-actions/auth@v2'
with:
credentials_json: '${{ secrets.GCLOUD_KEY_JSON }}'
token_format: 'access_token'
workload_identity_provider: 'projects/99059079106/locations/global/workloadIdentityPools/github/subject/SUBJECT_ATTRIBUTE_VALUE'
service_account: 'deploy-geppetto-from-github@geppetto-408614.iam.gserviceaccount.com'


- name: Docker configuration
env:
ACCESS_TOKEN: ${{ steps.auth.outputs.access_token }}
run: echo $ACCESS_TOKEN | docker login -u oauth2accesstoken --password-stdin https://us-central1-docker.pkg.dev

# Alternative option - authentication via credentials json
# - id: 'auth'
# uses: 'google-github-actions/auth@v0'
# with:
# credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
- name: Log access token
run: echo "Access Token:${{ steps.auth.outputs.access_token }}"

- 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:
uses: google-github-actions/get-gke-credentials@v2
with:
cluster_name: ${{ env.GKE_CLUSTER }}
location: ${{ env.GKE_ZONE }}

# 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
run: |
docker build \
--tag "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE }}:${{ github.sha }}" \
--build-arg GITHUB_SHA="${{ github.sha }}" \
--build-arg GITHUB_REF="${{ github.ref }}" \
.
- name: Publish
run: |-
docker push "$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA"
run: |
docker push "${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE }}:${{ github.sha }}"
# Set up kustomize
- name: Set up Kustomize
run: |-
curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64
chmod u+x ./kustomize
run: |
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
./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 -
run: |
./kustomize edit set image ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.IMAGE }}:${{ github.sha }}
./kustomize build . | kubectl apply -f -
4 changes: 0 additions & 4 deletions geppetto/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
load_dotenv(os.path.join("config", ".env"))


SLACK_BOT_TOKEN = os.getenv("SLACK_BOT_TOKEN_TEST")
SLACK_APP_TOKEN = os.getenv("SLACK_APP_TOKEN_TEST")
SIGNING_SECRET = os.getenv("SIGNING_SECRET_TEST")

DEFAULT_RESPONSES = load_json("default_responses.json")

# Initialize logging
Expand Down

0 comments on commit 6d487d4

Please sign in to comment.