Skip to content

Commit

Permalink
ci: move deploy jobs to separate release workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
stdavis committed Oct 15, 2024
1 parent 620b421 commit 0e15a76
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 219 deletions.
104 changes: 104 additions & 0 deletions .github/actions/deploy/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
name: Deploy
description: Deploy to GCP
inputs:
project_id:
description: "The GCP project ID"
required: true
identity_provider:
description: "The identity provider for the workload identity"
required: true
service_account_email:
description: "The service account email"
required: true
pause_schedule_job:
description: "Pause the scheduler job"
required: false
default: "no"
github_token:
description: "The GitHub token"
required: true

runs:
using: composite
steps:
- name: 🗝️ Authenticate to Google Cloud
id: auth
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ inputs.identity_provider }}
service_account: ${{ inputs.service_account_email }}
token_format: "access_token"

- name: 🐳 Set up Docker Buildx
id: builder
uses: docker/setup-buildx-action@v3

- name: 🗝️ Authenticate Docker to Google Cloud
uses: docker/login-action@v3
with:
registry: us-central1-docker.pkg.dev
username: oauth2accesstoken
password: ${{ steps.auth.outputs.access_token }}

- name: 🏷️ Extract tags from GitHub
id: meta
uses: docker/metadata-action@v5
with:
github-token: ${{ inputs.github_token }}
images: us-central1-docker.pkg.dev/${{ inputs.project_id }}/images/job
tags: |
type=ref,suffix=-{{sha}},event=branch
type=ref,prefix=pr-,suffix=-{{sha}},event=pr
type=semver,pattern={{version}}
latest
- name: 📦 Build and push image
uses: docker/build-push-action@v6
with:
builder: ${{ steps.builder.outputs.name }}
tags: ${{ steps.meta.outputs.tags }}
context: .
file: ./Dockerfile
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false

- name: ☁️ Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2

- name: 🚀 Deploy Cloud Run Job
uses: google-github-actions/deploy-cloudrun@v2
with:
project_id: ${{ inputs.project_id }}
region: us-central1
image: us-central1-docker.pkg.dev/${{ inputs.project_id }}/images/job:latest
job: default
secrets: /secrets/app/secrets.json=skid-secrets:latest
secrets_update_strategy: overwrite
timeout: 3h
flags: |
--memory=3Gi
--task-timeout=3h
--max-retries=0
--service-account=cloud-run-sa@${{ inputs.project_id }}.iam.gserviceaccount.com
- name: 🕰️ Create Cloud Scheduler
shell: bash
run: |
for i in $(gcloud scheduler jobs list --location=us-central1 --uri); do
gcloud scheduler jobs delete $i --quiet
done
gcloud scheduler jobs create http nfhl-schedule \
--description="Trigger the nfhl-skid bot once a week on saturday evening" \
--schedule="0 3 * * 1" \
--time-zone=America/Denver \
--location=us-central1 \
--uri="https://us-central1-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/${{ inputs.project_id }}/jobs/default:run" \
--oauth-service-account-email=scheduler-sa@${{ inputs.project_id }}.iam.gserviceaccount.com
- name: 🙅 Pause Scheduler Job
shell: bash
if: inputs.pause_schedule_job != 'no'
run: |
gcloud scheduler jobs pause "nfhl-schedule" --location=us-central1 --quiet
39 changes: 39 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Pull Request Events

on: pull_request

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
cancel-in-progress: true

jobs:
test:
name: Test
runs-on: ubuntu-latest

steps:
- name: ⬇️ Set up code
uses: actions/checkout@v4
with:
show-progress: false

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: pip
cache-dependency-path: setup.py

- name: Install libkrb5 for Kerberos on Linux
run: |
sudo apt-get update
sudo apt-get install -y libkrb5-dev
- name: Install module
run: pip install .[tests]

- name: 🧶 Lint
run: ruff check --output-format=github .

- name: 🧪 Run pytest
run: pytest
216 changes: 23 additions & 193 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,36 @@ on:
branches:
- main
- dev
pull_request:
branches:
- main
- dev

concurrency:
group: "${{ github.head_ref || github.ref }}"
cancel-in-progress: true

jobs:
test:
name: Setup and Test
release:
name: Create release
if: github.ref_name == 'main'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
show-progress: false

- name: Set up Python
uses: actions/setup-python@v5
- name: 🚀 Create Release
uses: agrc/release-composite-action@v1
with:
python-version: "3.11"
cache: pip
cache-dependency-path: setup.py

- name: Install libkrb5 for Kerberos on Linux
run: |
sudo apt-get update
sudo apt-get install -y libkrb5-dev
- name: Install module
run: pip install .[tests]

- name: Test with pytest
run: pytest
repo-token: ${{ secrets.GITHUB_TOKEN }}
github-app-id: ${{ secrets.UGRC_RELEASE_BOT_APP_ID }}
github-app-key: ${{ secrets.UGRC_RELEASE_BOT_APP_KEY }}
github-app-name: ${{ secrets.UGRC_RELEASE_BOT_NAME }}
github-app-email: ${{ secrets.UGRC_RELEASE_BOT_EMAIL }}
release-type: python
extra-files: src/nfhl/version.py

deploy-dev:
name: Deploy to Cloud Run (dev)
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/dev'
if: github.ref_name == 'dev'
environment:
name: dev
permissions:
Expand All @@ -60,169 +47,12 @@ jobs:
with:
show-progress: false

- name: 🗝️ Authenticate to Google Cloud
id: auth
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.IDENTITY_PROVIDER }}
service_account: ${{ secrets.SERVICE_ACCOUNT_EMAIL }}
token_format: "access_token"

- name: 🐳 Set up Docker Buildx
id: builder
uses: docker/setup-buildx-action@v3

- name: 🗝️ Authenticate Docker to Google Cloud
uses: docker/login-action@v3
with:
registry: us-central1-docker.pkg.dev
username: oauth2accesstoken
password: ${{ steps.auth.outputs.access_token }}

- name: 🏷️ Extract tags from GitHub
id: meta
uses: docker/metadata-action@v5
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
images: us-central1-docker.pkg.dev/${{ secrets.PROJECT_ID }}/images/job
tags: |
type=ref,suffix=-{{sha}},event=branch
type=ref,prefix=pr-,suffix=-{{sha}},event=pr
type=semver,pattern={{version}}
latest
- name: 📦 Build and push image
uses: docker/build-push-action@v6
with:
builder: ${{ steps.builder.outputs.name }}
tags: ${{ steps.meta.outputs.tags }}
context: .
file: ./Dockerfile
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false

- name: ☁️ Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2

- name: 🚀 Deploy Cloud Run Job
uses: google-github-actions/deploy-cloudrun@v2
with:
project_id: ${{ secrets.PROJECT_ID }}
region: us-central1
image: us-central1-docker.pkg.dev/${{ secrets.PROJECT_ID }}/images/job:latest
job: default
secrets: /secrets/app/secrets.json=skid-secrets:latest
secrets_update_strategy: overwrite
timeout: 3h
flags: |
--memory=3Gi
--task-timeout=3h
--max-retries=0
--service-account=cloud-run-sa@${{ secrets.PROJECT_ID }}.iam.gserviceaccount.com
- name: 🕰️ Create Cloud Scheduler
run: |
for i in $(gcloud scheduler jobs list --location=us-central1 --uri); do
gcloud scheduler jobs delete $i --quiet
done
gcloud scheduler jobs create http nfhl-schedule \
--description="Trigger the nfhl-skid bot once a week on saturday evening" \
--schedule="0 3 * * 1" \
--time-zone=America/Denver \
--location=us-central1 \
--uri="https://us-central1-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/${{ secrets.PROJECT_ID }}/jobs/default:run" \
--oauth-service-account-email=scheduler-sa@${{ secrets.PROJECT_ID }}.iam.gserviceaccount.com
deploy-prod:
name: Deploy to Cloud Run (prod)
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
environment:
name: prod
permissions:
id-token: write
contents: read

steps:
- name: ⬇️ Set up code
uses: actions/checkout@v4
with:
show-progress: false

- name: 🗝️ Authenticate to Google Cloud
id: auth
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ secrets.IDENTITY_PROVIDER }}
service_account: ${{ secrets.SERVICE_ACCOUNT_EMAIL }}
token_format: "access_token"

- name: 🐳 Set up Docker Buildx
id: builder
uses: docker/setup-buildx-action@v3

- name: 🗝️ Authenticate Docker to Google Cloud
uses: docker/login-action@v3
with:
registry: us-central1-docker.pkg.dev
username: oauth2accesstoken
password: ${{ steps.auth.outputs.access_token }}

- name: 🏷️ Extract tags from GitHub
id: meta
uses: docker/metadata-action@v5
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
images: us-central1-docker.pkg.dev/${{ secrets.PROJECT_ID }}/images/job
tags: |
type=ref,suffix=-{{sha}},event=branch
type=ref,prefix=pr-,suffix=-{{sha}},event=pr
type=semver,pattern={{version}}
latest
- name: 📦 Build and push image
uses: docker/build-push-action@v6
with:
builder: ${{ steps.builder.outputs.name }}
tags: ${{ steps.meta.outputs.tags }}
context: .
file: ./Dockerfile
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: false

- name: ☁️ Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2

- name: 🚀 Deploy Cloud Run Job
uses: google-github-actions/deploy-cloudrun@v2
- name: 🚀 Deploy
uses: ./.github/actions/deploy
timeout-minutes: 15
with:
project_id: ${{ secrets.PROJECT_ID }}
region: us-central1
image: us-central1-docker.pkg.dev/${{ secrets.PROJECT_ID }}/images/job:latest
job: default
secrets: /secrets/app/secrets.json=skid-secrets:latest
secrets_update_strategy: overwrite
timeout: 3h
flags: |
--memory=3Gi
--task-timeout=3h
--max-retries=0
--service-account=cloud-run-sa@${{ secrets.PROJECT_ID }}.iam.gserviceaccount.com
- name: 🕰️ Create Cloud Scheduler
run: |
for i in $(gcloud scheduler jobs list --location=us-central1 --uri); do
gcloud scheduler jobs delete $i --quiet
done
gcloud scheduler jobs create http nfhl-schedule \
--description="Trigger the nfhl-skid bot once a week on saturday evening" \
--schedule="0 3 * * 1" \
--time-zone=America/Denver \
--location=us-central1 \
--uri="https://us-central1-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/${{ secrets.PROJECT_ID }}/jobs/default:run" \
--oauth-service-account-email=scheduler-sa@${{ secrets.PROJECT_ID }}.iam.gserviceaccount.com
identity_provider: ${{ secrets.IDENTITY_PROVIDER }}
service_account_email: ${{ secrets.SERVICE_ACCOUNT_EMAIL }}
pause_schedule_job: "yes"
github_token: ${{ secrets.GITHUB_TOKEN }}
Loading

0 comments on commit 0e15a76

Please sign in to comment.