-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from cristian-rincon/feature/2-ci-cd
feat: Step 2 - CI/CD
- Loading branch information
Showing
4 changed files
with
193 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
name: Continuous Deployment | ||
run-name: CD | ||
on: | ||
workflow_run: | ||
workflows: ["Continuous Integration"] | ||
types: | ||
- completed | ||
branches: | ||
- main | ||
- develop | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} | ||
GCP_REGION: ${{ secrets.GCP_REGION }} | ||
GCP_ENVIRONMENT: ${{ github.ref == 'refs/heads/main' && 'production' || 'development' }} | ||
GCP_CREDENTIALS_JSON: ${{ secrets.GCP_CREDENTIALS_JSON }} | ||
GOOGLE_APPLICATION_CREDENTIALS: ${{ github.workspace }}/credentials.json | ||
jobs: | ||
apply: | ||
name: Apply Terraform | ||
runs-on: ubuntu-latest | ||
environment: ${{ github.ref == 'refs/heads/main' && 'production' || 'development' }} | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Setup Terraform | ||
uses: hashicorp/setup-terraform@v1 | ||
with: | ||
terraform_version: 1.8.1 | ||
- name: Set up gcloud Cloud SDK environment | ||
uses: google-github-actions/setup-gcloud@v2.1.1 | ||
- name: Authenticate to GCP | ||
run: | | ||
echo "${GCP_CREDENTIALS_JSON}" > ${{ github.workspace }}/credentials.json | ||
gcloud auth activate-service-account --key-file=${{ github.workspace }}/credentials.json | ||
gcloud config set project ${{ env.GCP_PROJECT_ID }} # Reemplaza con tu ID de proyecto | ||
- name: Terraform Init | ||
run: terraform init | ||
working-directory: ./infra | ||
- name: Terraform Plan | ||
id: plan | ||
run: terraform plan -out=tfplan | ||
env: | ||
TF_VAR_environment: ${{ github.ref == 'refs/heads/main' && 'production' || 'development' }} | ||
TF_VAR_project_id: ${{ github.ref == 'refs/heads/main' && env.GCP_PROJECT_ID }} | ||
TF_VAR_region: ${{ env.GCP_REGION }} | ||
GOOGLE_APPLICATION_CREDENTIALS: ${{ env.GCP_CREDENTIALS_JSON }} | ||
- name: Terraform Apply | ||
run: terraform apply -auto-approve tfplan | ||
env: | ||
TF_VAR_environment: ${{ github.ref == 'refs/heads/main' && 'production' || 'development' }} | ||
TF_VAR_project_id: ${{ github.ref == 'refs/heads/main' && env.GCP_PROJECT_ID }} | ||
TF_VAR_region: ${{ env.GCP_REGION }} | ||
- name: Set GitHub Environment | ||
id: set_environment | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
const environment = '${{ github.ref == 'refs/heads/main' && 'production' || 'development' }}'; | ||
const context = require('@actions/github').context; | ||
const response = await github.request(`POST /repos/${context.repo.owner}/${context.repo.repo}/deployments`, { | ||
ref: context.ref, | ||
environment: environment, | ||
description: `Deploying to ${environment} environment`, | ||
}); | ||
return response.data.id; | ||
- name: GitHub Deployment Status | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
const deployment_id = '${{ steps.set_environment.outputs.result }}'; | ||
await github.request(`POST /repos/${context.repo.owner}/${context.repo.repo}/deployments/${deployment_id}/statuses`, { | ||
state: 'success', | ||
description: 'Deployment completed successfully', | ||
}); | ||
- name: Terraform Output | ||
run: terraform output |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
name: Continuous Integration | ||
run-name: CI | ||
on: | ||
push: | ||
branches: | ||
- main | ||
- develop | ||
pull_request: | ||
branches: | ||
- main | ||
- develop | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} | ||
GCP_REGION: ${{ secrets.GCP_REGION }} | ||
GCP_ENVIRONMENT: ${{ github.ref == 'refs/heads/main' && 'production' || 'development' }} | ||
GCP_CREDENTIALS_JSON: ${{ secrets.GCP_CREDENTIALS_JSON }} | ||
GOOGLE_APPLICATION_CREDENTIALS: ${{ github.workspace }}/credentials.json | ||
jobs: | ||
check-terraform-code: | ||
name: Check Terraform Code | ||
runs-on: ubuntu-latest | ||
environment: ${{ github.ref == 'refs/heads/main' && 'production' || 'development' }} | ||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
- name: Setup Terraform | ||
uses: hashicorp/setup-terraform@v1 | ||
with: | ||
terraform_version: 1.8.1 | ||
- name: Set up gcloud Cloud SDK environment | ||
uses: google-github-actions/setup-gcloud@v2.1.1 | ||
- name: Authenticate to GCP | ||
run: | | ||
echo "${GCP_CREDENTIALS_JSON}" > ${{ github.workspace }}/credentials.json | ||
gcloud auth activate-service-account --key-file=${{ github.workspace }}/credentials.json | ||
gcloud config set project ${{ env.GCP_PROJECT_ID }} # Reemplaza con tu ID de proyecto | ||
- name: Terraform Init | ||
run: terraform init | ||
working-directory: ./infra | ||
- name: Terraform Format | ||
run: terraform fmt -check | ||
working-directory: ./infra | ||
- name: Terraform Validate | ||
run: terraform validate | ||
working-directory: ./infra | ||
- name: Terraform Plan | ||
run: terraform plan | ||
working-directory: ./infra | ||
env: | ||
TF_VAR_environment: ${{ github.ref == 'refs/heads/main' && 'production' || 'development' }} | ||
TF_VAR_project_id: ${{ github.ref == 'refs/heads/main' && env.GCP_PROJECT_ID }} | ||
TF_VAR_region: ${{ env.GCP_REGION }} | ||
# check-python-code: | ||
# name: Check Python Code | ||
# runs-on: ubuntu-latest | ||
# steps: | ||
# - name: Checkout code | ||
# uses: actions/checkout@v2 | ||
# - name: Setup Python | ||
# uses: actions/setup-python@v2 | ||
# with: | ||
# python-version: 3.9 | ||
# - name: Install dependencies | ||
# run: pip install -r requirements.txt | ||
# - name: Run tests | ||
# run: pytest |
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 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