Skip to content

Commit

Permalink
Fix main for deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
AntGa committed Aug 27, 2024
1 parent 105075c commit b28d819
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
68 changes: 37 additions & 31 deletions .github/workflows/toGoogle.yml
Original file line number Diff line number Diff line change
@@ -1,56 +1,62 @@
name: 'Build and Deploy to Cloud Run'
name: Build and Deploy to Cloud Run

on:
push:
branches:
- main

env:
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
REGION: ${{ secrets.GCP_REGION }}
SERVICE: ${{ secrets.GCP_SERVICE_NAME }}
WORKLOAD_IDENTITY_PROVIDER: ${{ secrets.GCP_WORKLOAD_IDENTITY_PROVIDER }}

jobs:
deploy:
runs-on: 'ubuntu-latest'

# Add 'id-token' with the intended permissions for workload identity federation
permissions:
contents: 'read'
id-token: 'write'
runs-on: ubuntu-latest
environment: GCR
env:
PROJECT_ID: ${{secrets.PROJECT_ID}}
GAR_LOCATION: ${{secrets.GAR_LOCATION}} # TODO: update Artifact Registry location
REPOSITORY: ${{secrets.REPOSITORY}} # TODO: update Artifact Registry repository name
SERVICE: image-converter # TODO: update Cloud Run service name
REGION: ${{secrets.GAR_LOCATION}} # TODO: update Cloud Run service region

steps:
- name: 'Checkout'
uses: 'actions/checkout@v4'
- name: Checkout
uses: actions/checkout@v2

# Configure Workload Identity Federation and generate an access token.
- id: 'auth'
name: 'Authenticate to Google Cloud'
- name: Google Auth
id: auth
uses: 'google-github-actions/auth@v2'
with:
workload_identity_provider: ${{ env.WORKLOAD_IDENTITY_PROVIDER }}
credentials_json: '${{ secrets.GCP_CREDENTIALS }}'
token_format: 'access_token'

# Docker authentication and build
- name: 'Docker Auth'
uses: 'docker/login-action@v3'
with:
username: 'oauth2accesstoken'
password: '${{ steps.auth.outputs.auth_token }}'
registry: '${{ env.REGION }}-docker.pkg.dev'
# BEGIN - Docker auth and build (NOTE: If you already have a container image, these Docker steps can be omitted)

- name: 'Build and Push Container'
# Authenticate Docker to Google Cloud Artifact Registry
- name: Docker Auth
run: |-
DOCKER_TAG="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}"
docker build --tag "${DOCKER_TAG}" .
docker push "${DOCKER_TAG}"
gcloud auth configure-docker "${{ env.GAR_LOCATION }}-docker.pkg.dev"
- name: 'Deploy to Cloud Run'
uses: 'google-github-actions/deploy-cloudrun@v2'
- 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 }}"
# END - Docker auth and build

- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/deploy-cloudrun@v2
with:
service: ${{ env.SERVICE }}
region: ${{ env.REGION }}
image: '${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}'
# NOTE: If using a pre-built image, update the image name here
image: ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/${{ env.SERVICE }}:${{ github.sha }}


- name: 'Show output'
run: |-
echo ${{ steps.deploy.outputs.url }}
# If required, use the Cloud Run url output in later steps
- name: Show Output
run: echo ${{ steps.deploy.outputs.url }}
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ WORKDIR /app

# Copy the requirements file and install dependencies
COPY requirements.txt requirements.txt

EXPOSE 8080

RUN pip install -r requirements.txt

# Copy the rest of the application code
COPY . .

# Specify the command to run the app
CMD ["gunicorn", "--bind", "0.0.0.0:8080", "app:app"]
CMD ["python", "app.py"]
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ def upload_file():


if __name__ == "__main__":
app.run(debug=True)
app.run(port=int(os.environ.get("PORT", 8080)), host="0.0.0.0", debug=True)

0 comments on commit b28d819

Please sign in to comment.