diff --git a/.github/workflows/ci-and-cd-for-review.yml b/.github/workflows/ci-and-cd-for-review.yml deleted file mode 100644 index 2e1c8a8..0000000 --- a/.github/workflows/ci-and-cd-for-review.yml +++ /dev/null @@ -1,12 +0,0 @@ -name: Review - -on: - # Trigger the workflow on activity over any pull request to main: - pull_request: - branches: - - main - -jobs: - test-and-build: - name: Integration - uses: ./.github/workflows/test-and-build.yml diff --git a/.github/workflows/ci-and-cd.yml b/.github/workflows/ci-and-cd.yml index e73ff75..cdb952a 100644 --- a/.github/workflows/ci-and-cd.yml +++ b/.github/workflows/ci-and-cd.yml @@ -5,8 +5,57 @@ on: push: branches: - main + pull_request: + branches: + - main + +permissions: + contents: read + id-token: write + deployments: write + +env: + PROJECT_ID: sepomex-365521 + IMAGE_NAME: sepomex + GAR_LOCATION: us-central1 + REPOSITORY: icalialabs-sepomex + SERVICE: sepomex + REGION: us-central1 jobs: - test-and-build: - name: Integration - uses: ./.github/workflows/test-and-build.yml + build-and-push-to-gcp: + name: Build and Push to GCP + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Authenticate to Google Cloud + id: auth + uses: google-github-actions/auth@v0 + with: + token_format: access_token + workload_identity_provider: projects/582875546495/locations/global/workloadIdentityPools/github-pool/providers/github-provider + service_account: github-actions@sepomex-365521.iam.gserviceaccount.com + + - name: Set up Google Cloud SDK + uses: google-github-actions/setup-gcloud@v0.6.0 + + - name: Docker Auth + id: docker-auth + uses: docker/login-action@v1 + with: + username: oauth2accesstoken + password: ${{ steps.auth.outputs.access_token }} + registry: ${{ env.GAR_LOCATION }}-docker.pkg.dev + + - name: Build and Push Container + run: |- + docker build -t ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }} ./ + docker push ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }} + + - name: Deploy to Cloud Run + id: deploy + run: |- + gcloud run deploy ${{ env.SERVICE }} --image ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }} -q --region ${{ env.REGION }} --port 3000 --allow-unauthenticated diff --git a/.github/workflows/test-and-build.yml b/.github/workflows/test-and-build.yml deleted file mode 100644 index dee8735..0000000 --- a/.github/workflows/test-and-build.yml +++ /dev/null @@ -1,100 +0,0 @@ -# (Copied from IcaliaLabs/portal) - -on: - workflow_call: - outputs: - git-commit-short-sha: - description: "The Git commit short SHA" - value: ${{ jobs.test-and-build.outputs.git-commit-short-sha }} - container-image-digest: - description: "The Docker image digest which uniquely identifies the built image" - value: ${{ jobs.test-and-build.outputs.container-image-digest }} - -# On this project, I went with a single "Test & Build" job, since each job -# gets billed rounded to the next minute, and having multiple separated jobs -# is not worth it at the beginning of the project. - -# Keep in mind that, if the test suite becomes too big, and test threading is -# required, or the workflow speed becomes critial, we might need to revert back -# to separated jobs. - -jobs: - test-and-build: - name: Test and Build - runs-on: ubuntu-latest - - outputs: - git-commit-short-sha: ${{ steps.variables.outputs.git-commit-short-sha }} - container-image-digest: ${{ steps.build-and-push-release-image.outputs.digest }} - - steps: - - name: Checkout the code - uses: actions/checkout@v3.0.2 - - - # We'll generate a unique id that we'll use to identify the build run - # on our systems - name: Generate UUID for build - id: uuidgen - run: echo "::set-output name=uuid::$(uuidgen)" - - - name: Set additional variables - id: variables - uses: icalia-actions/common-variables@v0.0.3 - - - name: Set build timestamp - id: set-build-timestamp - run: echo "::set-output name=timestamp::$(date +%s)" - - - name: Set up Docker Buildx - id: buildx - uses: docker/setup-buildx-action@v2.0.0 - - - name: Build Test Image - id: build-test-image - uses: docker/build-push-action@v3.1.1 - with: - load: true - target: testing - 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: | - icalialabs/sepomex-web:testing-${{ steps.variables.outputs.git-dasherized-branch }} - cache-from: type=gha - cache-to: type=gha,mode=max - - - name: Run tests - env: - UID: ${{ steps.variables.outputs.runner-uid }} - USER: ${{ steps.variables.outputs.runner-user }} - TESTING_IMAGE_TAG: ":testing-${{ steps.variables.outputs.git-dasherized-branch }}" - run: docker compose run --rm tests - - - name: Archive system spec screenshots - uses: actions/upload-artifact@v3.1.0 - if: always() - with: - name: system-spec-screenshots - retention-days: 7 - path: | - tmp/capybara/screenshots - - - name: Build & Push Release Image - id: build-and-push-release-image - uses: docker/build-push-action@v3.1.1 - with: - push: false - 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 - cache-from: type=gha - cache-to: type=gha,mode=max diff --git a/Dockerfile b/Dockerfile index 52ea0c1..8ac6876 100644 --- a/Dockerfile +++ b/Dockerfile @@ -239,6 +239,9 @@ RUN SECRET_KEY_BASE=10167c7f7654ed02b3557b05b88ece rails secret > /dev/null # Set the installed app directory as the working directory: WORKDIR /workspaces/sepomex +# Grant permission +RUN ["chmod", "+x", "/workspaces/sepomex/bin/entrypoint"] + # Set the entrypoint script: ENTRYPOINT [ "/workspaces/sepomex/bin/entrypoint" ] diff --git a/bin/entrypoint b/bin/entrypoint new file mode 100644 index 0000000..496b82f --- /dev/null +++ b/bin/entrypoint @@ -0,0 +1,24 @@ +#!/usr/bin/env ruby + +# frozen_string_literal: true + +def set_given_or_default_command + ARGV.concat %w[puma] if ARGV.empty? +end + +def execute_given_or_default_command + exec(*ARGV) +end + +def run_database_migrations + raise('Migrations failed') unless system('rails db:migrate') +end + +def run_database_seeds + raise('Seeds failed') unless system('rails db:seed') +end + +set_given_or_default_command +run_database_migrations +run_database_seeds +execute_given_or_default_command