Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cicd/gcp configuration #80

Merged
merged 35 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
ca9d5c8
1st github actions config
AlanMendicutti Aug 22, 2023
e7c86b4
Updating gcp authentication creds
AlanMendicutti Aug 22, 2023
3681ccc
Auth gc from github actions
AlanMendicutti Aug 22, 2023
19d5628
Switchin push to true
AlanMendicutti Aug 22, 2023
39b93cc
Adding missing entrypoint
AlanMendicutti Aug 22, 2023
8089fbd
Updating entrypoint file
AlanMendicutti Aug 22, 2023
3ca6cc3
Skipping entrypoint by the moment
AlanMendicutti Aug 22, 2023
e8fd4bb
Removing schema search path
AlanMendicutti Aug 22, 2023
69c4791
Adding review-env setup (database creation)
AlanMendicutti Aug 22, 2023
4d7782e
Replacing vars
AlanMendicutti Aug 23, 2023
b450113
Adding adapter to database.yml
AlanMendicutti Aug 23, 2023
9a57c61
Testing
AlanMendicutti Aug 23, 2023
a9cee2d
Returning back entrypoint config
AlanMendicutti Aug 23, 2023
f936d1e
Reverting entrypoint execution
AlanMendicutti Aug 23, 2023
944636b
Turning into underscore db name
AlanMendicutti Aug 23, 2023
9e30774
Docker layer cache issue
AlanMendicutti Aug 23, 2023
9e5a24d
Adding password strongly
AlanMendicutti Aug 23, 2023
d6a559e
Adding username
AlanMendicutti Aug 23, 2023
5c89787
Turning entrypoint back on
AlanMendicutti Aug 23, 2023
d795aea
Test
AlanMendicutti Aug 23, 2023
1e20b76
Skipping entrypoint
AlanMendicutti Aug 24, 2023
3e351a9
Test
AlanMendicutti Aug 24, 2023
5d90e58
Test
AlanMendicutti Aug 24, 2023
e976cfc
test
AlanMendicutti Aug 24, 2023
feea64b
Testing
AlanMendicutti Aug 24, 2023
47401a4
Trying with tankef config
AlanMendicutti Aug 24, 2023
986309f
Test
AlanMendicutti Aug 24, 2023
ae954b8
Debuggin
AlanMendicutti Aug 24, 2023
ac5fec0
Adding google-cloud-secret_manager gem
AlanMendicutti Aug 24, 2023
91eb34a
Test
AlanMendicutti Aug 24, 2023
af5ecb3
Enabling entrypoint
AlanMendicutti Aug 24, 2023
f453449
Test
AlanMendicutti Aug 24, 2023
75ef835
Adding permissions to entrypoint file
AlanMendicutti Aug 24, 2023
30c7957
Updating gitignore
AlanMendicutti Aug 24, 2023
bd885b9
Test
AlanMendicutti Aug 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/ci-and-cd-for-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,30 @@ on:
branches:
- main

# Generate a GitHub token that can be exchanged with Google Cloud - see
# https://github.com/google-github-actions/auth/tree/v0.6.0#setting-up-workload-identity-federation
permissions:
contents: read
id-token: write
deployments: write

jobs:
test-and-build:
name: Integration
uses: ./.github/workflows/test-and-build.yml
secrets: inherit

release-to-review:
if: ${{ github.actor != 'dependabot[bot]' }}
name: Delivery
uses: ./.github/workflows/deploy-to-cloud-run.yml
needs:
- test-and-build
with:
environment: ${{ github.head_ref }}
deploy-name: pr${{ github.event.pull_request.number }}
cloud-sql-instance: sepomex-365521:us-central1:sepomex
cloud-sql-username-secret: database-username:latest
cloud-sql-password-secret: database-password:latest
cloud-run-service-suffix: ${{ needs.test-and-build.outputs.git-commit-short-sha }}
cloud-run-container-image: us-central1-docker.pkg.dev/sepomex-365521/icalialabs-sepomex/sepomex@${{ needs.test-and-build.outputs.container-image-digest }}
22 changes: 22 additions & 0 deletions .github/workflows/ci-and-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,29 @@ on:
branches:
- main

# Generate a GitHub token that can be exchanged with Google Cloud - see
# https://github.com/google-github-actions/auth/tree/v0.6.0#setting-up-workload-identity-federation
permissions:
contents: read
id-token: write
deployments: write

jobs:
test-and-build:
name: Integration
uses: ./.github/workflows/test-and-build.yml

deploy-to-staging:
name: Staging
uses: ./.github/workflows/deploy-to-cloud-run.yml
needs:
- test-and-build
with:
environment: staging
deploy-name: staging
cloud-sql-instance: sepomex-365521:us-central1:sepomex
cloud-sql-username-secret: database-username:latest
cloud-sql-password-secret: database-password:latest
cloud-run-service-suffix: ${{ needs.test-and-build.outputs.git-commit-short-sha }}
cloud-run-container-image: us-central1-docker.pkg.dev/sepomex-365521/sepomex/sepomex@${{ needs.test-and-build.outputs.container-image-digest }}
secrets: inherit
101 changes: 101 additions & 0 deletions .github/workflows/deploy-to-cloud-run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
on:
workflow_call:
inputs:
environment:
required: true
type: string
deploy-name:
required: true
type: string
cloud-sql-instance:
required: true
type: string
cloud-sql-username-secret:
required: false
type: string
default: projects/582875546495/secrets/database-username:latest
cloud-sql-password-secret:
required: false
type: string
default: projects/582875546495/secrets/database-password:latest
cloud-run-container-image:
required: true
type: string
cloud-run-service-suffix:
required: false
type: string
cloud-run-minimum-instances:
required: false
type: number
default: 0

jobs:
service-deploy:
name: Service Deploy
runs-on: ubuntu-latest
env:
DATABASE_NAME: sepomex_${{ inputs.deploy-name }}
steps:
# actions/checkout MUST come before auth
- name: Checkout the code
uses: actions/[email protected]

- name: Authenticate to Google Cloud
uses: google-github-actions/[email protected]
with:
service_account: [email protected]
workload_identity_provider: projects/582875546495/locations/global/workloadIdentityPools/github-pool/providers/github-provider

- name: Set up Google Cloud SDK
uses: google-github-actions/[email protected]

- name: URLEncode Cloud SQL Instance string
id: url-encode-cloud-sql-instance
run: |-
ruby -e 'require "erb"; puts "encoded-value=#{ERB::Util.url_encode("${{ inputs.cloud-sql-instance }}")}"' >> $GITHUB_OUTPUT

- name: Register Deploy Start on Github
uses: bobheadxi/[email protected]
id: deploy-start
with:
step: start
ref: ${{ github.head_ref }}
env: ${{ inputs.environment }}
token: ${{ github.token }}

- name: Set deploy timestamp
id: set-deploy-timestamp
run: echo "timestamp=$(date +%s)" >> $GITHUB_OUTPUT

- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/[email protected]
with:
region: us-central1
service: sepomex-${{ inputs.deploy-name }}
suffix: ${{ inputs.cloud-run-service-suffix }}-${{ steps.set-deploy-timestamp.outputs.timestamp }}
image: ${{ inputs.cloud-run-container-image }}
secrets: |
DATABASE_USERNAME=${{ inputs.cloud-sql-username-secret }}
DATABASE_PASSWORD=${{ inputs.cloud-sql-password-secret }}
env_vars: |
DATABASE_URL=postgres://%2Fcloudsql%2F${{ steps.url-encode-cloud-sql-instance.outputs.encoded-value }}/${{ env.DATABASE_NAME }}
GOOGLE_CLOUD_PROJECT=sepomex-365521
flags: |-
--allow-unauthenticated
--add-cloudsql-instances ${{ inputs.cloud-sql-instance }}
--min-instances=${{ inputs.cloud-run-minimum-instances }}
--service-account [email protected]

- name: Finalize the deployment state on Github
uses: bobheadxi/[email protected]
if: always()
with:
step: finish
override: true
auto_inactive: true
status: ${{ job.status }}
token: ${{ github.token }}
env_url: ${{ steps.deploy.outputs.url }}
env: ${{ steps.deploy-start.outputs.env }}
deployment_id: ${{ steps.deploy-start.outputs.deployment_id }}
48 changes: 48 additions & 0 deletions .github/workflows/review-env-setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Review Environment Setup

on:
workflow_dispatch:
pull_request:
branches:
- main
types:
- opened
- reopened

jobs:
create_deployment:
if: ${{ github.actor != 'dependabot[bot]' }}
name: Create deployment
runs-on: ubuntu-latest
env:
DB_NAME: "sepomex-pr${{ github.event.pull_request.number }}"
GOOGLE_CLOUD_PROJECT: sepomex-365521

# Generate a GitHub token that can be exchanged with Google Cloud - see
# https://github.com/google-github-actions/auth/tree/v0.6.0#setting-up-workload-identity-federation
permissions:
contents: read
id-token: write

steps:
# actions/checkout MUST come before auth
- name: Checkout the code
uses: actions/[email protected]

- name: Authenticate to Google Cloud
uses: google-github-actions/[email protected]
with:
service_account: [email protected]
workload_identity_provider: projects/582875546495/locations/global/workloadIdentityPools/github-pool/providers/github-provider

- name: Set up Google Cloud SDK
uses: google-github-actions/[email protected]

- name: Ensure a review database exists
run: |-
gcloud sql databases describe ${{ env.DB_NAME }} \
--instance=sepomex \
--format="value(name)" \
&& echo "Database ${{ env.DB_NAME }} already exists" \
|| gcloud sql databases create ${{ env.DB_NAME }} \
--instance=sepomex
24 changes: 19 additions & 5 deletions .github/workflows/test-and-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ jobs:

- name: Set up Docker Buildx
id: buildx
uses: docker/[email protected]
uses: docker/[email protected]
with:
version: v0.9.1

- name: Build Test Image
id: build-test-image
Expand Down Expand Up @@ -81,20 +83,32 @@ jobs:
path: |
tmp/capybara/screenshots

- name: Authenticate to Google Cloud
uses: google-github-actions/[email protected]
with:
service_account: [email protected]
workload_identity_provider: projects/582875546495/locations/global/workloadIdentityPools/github-pool/providers/github-provider

- name: Set up Google Cloud SDK
uses: google-github-actions/[email protected]

- name: Authorize push to Google Cloud Artifact Registry
run: gcloud auth configure-docker us-central1-docker.pkg.dev

- name: Build & Push Release Image
id: build-and-push-release-image
uses: docker/[email protected]
with:
push: false
push: true
target: release
platforms: linux/amd64
builder: ${{ steps.buildx.outputs.name }}
build-args: |
DEVELOPER_UID=${{ steps.variables.outputs.runner-uid }}
DEVELOPER_USERNAME=${{ steps.variables.outputs.runner-user }}
tags: |
us-central1-docker.pkg.dev/icalia-labs-sepomex/sepomex/sepomex-web:${{ steps.variables.outputs.git-commit-short-sha }}
us-central1-docker.pkg.dev/icalia-labs-sepomex/sepomex/sepomex-web:${{ steps.variables.outputs.git-dasherized-branch }}
us-central1-docker.pkg.dev/icalia-labs-sepomex/sepomex/sepomex-web:latest
us-central1-docker.pkg.dev/sepomex-365521/icalialabs-sepomex/sepomex:${{ steps.variables.outputs.git-commit-short-sha }}
us-central1-docker.pkg.dev/sepomex-365521/icalialabs-sepomex/sepomex:${{ steps.variables.outputs.git-dasherized-branch }}
us-central1-docker.pkg.dev/sepomex-365521/icalialabs-sepomex/sepomex:latest
cache-from: type=gha
cache-to: type=gha,mode=max
13 changes: 10 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

/db/development.sqlite3
/log/development.log
/tmp
tmp/cache/bootsnap-load-path-cache
log/test.log

docker-compose.override.yml
tmp/cache

# Ignore history files
**/.*_hist*

# Ignore database dumps
db/dumps/*
!db/dumps/.keep

# Ignore all logfiles and tempfiles.
/tmp/*
!/tmp/.keep

# Ignore pidfiles, but keep the directory.
/tmp/pids/*
!/tmp/pids/
!/tmp/pids/.keep
!/tmp/pids/server.pid
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ruby '2.7.5'
gem 'rails', '~> 6.0', '>= 6.0.3.2'

# Use pg as the database for Active Record
gem 'pg'
gem 'pg', '~> 1.1'

# Use Puma as the app server
gem 'puma', '~> 4.3'
Expand All @@ -31,6 +31,9 @@ gem 'pagy', '~> 3.8', '>= 3.8.2'
gem 'rack-cors', '~> 1.1', '>= 1.1.1'
# Testing

# Read secrets from Google Cloud Secret Manager
gem 'google-cloud-secret_manager', '~> 1.1', '>= 1.1.3'

group :development, :test do
gem 'listen', '>= 3.0.5', '< 3.2'

Expand Down
Loading
Loading