From 10cf1213d191aaf1901ca722ef21be52f6d50296 Mon Sep 17 00:00:00 2001 From: Jimmy Briggs Date: Mon, 24 Jun 2024 13:57:58 -0400 Subject: [PATCH] Update and rename docker-build.yml to docker-gcp.yml --- .github/workflows/docker-build.yml | 62 ----------------------- .github/workflows/docker-gcp.yml | 80 ++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 62 deletions(-) delete mode 100644 .github/workflows/docker-build.yml create mode 100644 .github/workflows/docker-gcp.yml diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml deleted file mode 100644 index 8df98e9..0000000 --- a/.github/workflows/docker-build.yml +++ /dev/null @@ -1,62 +0,0 @@ -name: Docker Build - -on: - push: - branches: [main] - tags: [ '*.*.*' ] - -env: - GCP_PROJECT: ${{ secrets.GCP_PROJECT }} - GCP_IMAGE: ${{ secrets.GCP_IMAGE }} - -jobs: - docker: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v4 - - name: Prepare - id: prep - run: | - DOCKER_IMAGE=gcr.io/${GCP_PROJECT}/${GCP_IMAGE} - VERSION=$(echo $GITHUB_REF | sed 's/refs\/tags\///') - DOCKER_TAG=${DOCKER_IMAGE}:${VERSION} - if [[ $VERSION =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then - MINOR=${VERSION%.*} - MAJOR=${MINOR%.*} - TAGS="$TAGS,${DOCKER_IMAGE}:${MINOR},${DOCKER_IMAGE}:${MAJOR},${DOCKER_IMAGE}:latest" - elif [ "${{ github.event_name }}" = "push" ]; then - TAGS="$TAGS,${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}" - fi - echo ::set-output name=version::${VERSION} - echo ::set-output name=tags::${TAGS} - echo ::set-output name=created::$(date -u +'%Y-%m-%dT%H:%M:%SZ') - - name: Set up QEMU - uses: docker/setup-qemu-action@v1 - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 - - name: Login to GCR - if: github.event_name != 'pull_request' - uses: docker/login-action@v1 - with: - registry: gcr.io - username: _json_key - password: ${{ secrets.GCR_JSON_KEY }} - - name: Build and push - id: docker_build - uses: docker/build-push-action@v2 - with: - context: . - file: ./Dockerfile - platforms: linux/amd64 - push: ${{ github.event_name != 'pull_request' }} - tags: ${{ steps.prep.outputs.tags }} - labels: | - org.opencontainers.image.title=${{ github.event.repository.name }} - org.opencontainers.image.description=${{ github.event.repository.description }} - org.opencontainers.image.url=${{ github.event.repository.html_url }} - org.opencontainers.image.source=${{ github.event.repository.clone_url }} - org.opencontainers.image.version=${{ steps.prep.outputs.version }} - org.opencontainers.image.created=${{ steps.prep.outputs.created }} - org.opencontainers.image.revision=${{ github.sha }} - org.opencontainers.image.licenses=${{ github.event.repository.license.spdx_id }} diff --git a/.github/workflows/docker-gcp.yml b/.github/workflows/docker-gcp.yml new file mode 100644 index 0000000..1b88d5d --- /dev/null +++ b/.github/workflows/docker-gcp.yml @@ -0,0 +1,80 @@ +name: Build and Deploy to Cloud Run + +on: + push: + branches: + - main + workflow_dispatch: + +env: + GCP_PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} + GCP_REGION: ${{ secrets.GCP_REGION }} + GCP_SERVICE_ACCOUNT_CREDENTIALS: ${{ secrets.GCP_SERVICE_ACCOUNT_CREDENTIALS }} + REPOSITORY_NAME: ${{ github.repository }} + APP_NAME: rshinycloudrun + +jobs: + deploy: + permissions: + contents: read + packages: write + deployments: write + id-token: write + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Google Auth + id: auth + uses: google-github-actions/auth@v2 + with: + token_format: 'access_token' + credentials_json: ${{ env.GCP_SERVICE_ACCOUNT_CREDENTIALS }} + + - name: Set up Cloud SDK + id: setup-cloud-sdk + uses: google-github-actions/setup-gcloud@v2 + with: + project_id: ${{ env.GCP_PROJECT_ID }} + + - name: Verify gcloud CLI + run: gcloud info + + - name: Docker Auth + id: docker-auth + run: |- + gcloud auth configure-docker ${{ env.GCP_REGION }}-docker.pkg.dev + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Get Image Tag + id: get-image-tag + run: |- + echo ::set-env name=IMAGE_TAG::${{ env.GCP_REGION }}-docker.pkg.dev/${{ env.GCP_PROJECT_ID }}/${{ env.REPOSITORY_NAME }}/${{ env.APP_NAME }}:${{ github.sha }} + + - name: Build and Push Container + id: build + run: |- + docker build -t ${{ env.IMAGE_TAG }} . + docker push ${{ env.IMAGE_TAG }} + + - name: Deploy to Cloud Run + id: deploy + uses: google-github-actions/deploy-cloudrun@v1 + with: + service: ${{ env.APP_NAME }} + image: ${{ env.IMAGE_TAG }} + region: ${{ env.GCP_REGION }} + platform: managed + allow-unauthenticated: true + env_vars: | + R_CONFIG_ACTIVE=production + + - name: Output Service URL + run: echo ${{ steps.deploy.outputs.url }}