|
| 1 | +name: Test, Build and Deploy to Cloud Run |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ "main" ] |
| 6 | + pull_request: |
| 7 | + branches: [ "main" ] |
| 8 | + |
| 9 | +jobs: |
| 10 | + test: |
| 11 | + name: Run Tests |
| 12 | + runs-on: ubuntu-latest |
| 13 | + steps: |
| 14 | + - name: Checkout code |
| 15 | + uses: actions/checkout@v4 |
| 16 | + |
| 17 | + - name: Set up Python |
| 18 | + uses: actions/setup-python@v5 |
| 19 | + with: |
| 20 | + python-version: '3.11' # Use a versão do Python que você usa |
| 21 | + |
| 22 | + - name: Install dependencies |
| 23 | + run: | |
| 24 | + python -m pip install --upgrade pip |
| 25 | + pip install -r requirements.txt |
| 26 | + pip install -r requirements-dev.txt |
| 27 | +
|
| 28 | + - name: Run pytest |
| 29 | + run: pytest |
| 30 | + |
| 31 | + build-and-deploy: |
| 32 | + name: Build and Deploy to Cloud RUn |
| 33 | + needs: test # Este job só executa se o job 'test' passar |
| 34 | + runs-on: ubuntu-latest |
| 35 | + steps: |
| 36 | + - name: Checkout code |
| 37 | + uses: actions/checkout@v4 |
| 38 | + |
| 39 | + - name: Google Auth |
| 40 | + uses: google-github-actions/auth@v2 |
| 41 | + with: |
| 42 | + credentials_json: ${{ secrets.GCP_SERVICE_KEY }} |
| 43 | + |
| 44 | + - name: Set up Google Cloud SDK |
| 45 | + uses: google-github-actions/setup-gcloud@v2 |
| 46 | + with: |
| 47 | + project_id: ${{ secrets.GCP_PROJECT_ID }} |
| 48 | + # Não é necessário export_default_credentials: false aqui, |
| 49 | + # pois a ação auth já cuidou da autenticação. |
| 50 | + |
| 51 | + - name: Configure Docker for Artifact Registry |
| 52 | + run: | |
| 53 | + gcloud auth configure-docker ${{ secrets.GCP_REGION }}-docker.pkg.dev --quiet |
| 54 | +
|
| 55 | + - name: Build Docker image |
| 56 | + run: | |
| 57 | + docker build . --file Dockerfile --tag ${{ secrets.GCP_REGION }}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.ARTIFACT_REPOSITORY }}/alura-imersao-dev:latest |
| 58 | +
|
| 59 | + - name: Push Docker image |
| 60 | + run: | |
| 61 | + docker push ${{ secrets.GCP_REGION }}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.ARTIFACT_REPOSITORY }}/alura-imersao-dev:latest |
| 62 | +
|
| 63 | + - name: Deploy to Cloud Run |
| 64 | + run: | |
| 65 | + gcloud run deploy alura-imersao-dev \ |
| 66 | + --image ${{ secrets.GCP_REGION }}-docker.pkg.dev/${{ secrets.GCP_PROJECT_ID }}/${{ secrets.ARTIFACT_REPOSITORY }}/alura-imersao-dev:latest \ |
| 67 | + --region ${{ secrets.GCP_REGION }} \ |
| 68 | + --platform managed \ |
| 69 | + --allow-unauthenticated \ |
| 70 | + --port 8000 |
| 71 | +
|
| 72 | + - name: Show gcloud authentication status |
| 73 | + run: | |
| 74 | + gcloud auth list |
| 75 | + gcloud config list |
0 commit comments