Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: standardize actions and a few other things #31

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 skinname-schedule \
--description="Trigger the skinname-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 "skinname-schedule" --location=us-central1 --quiet
6 changes: 3 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ updates:
interval: monthly
groups:
safe-dependencies:
update-types: ['minor', 'patch']
update-types: ["minor", "patch"]
major-dependencies:
update-types: ['major']
update-types: ["major"]
commit-message:
prefix: deps
prefix-development: deps(dev)
Expand All @@ -18,4 +18,4 @@ updates:
interval: monthly
groups:
ci-dependencies:
dependency-type: 'production'
dependency-type: "production"
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: Unit tests
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-file: .python-version
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
172 changes: 25 additions & 147 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,36 @@ on:
branches:
- main
- dev
pull_request:
branches:
- main
- dev

env:
CLOUD_FUNCTION_MEMORY: 512M
CLOUD_FUCNTION_RUN_TIMEOUT: 240s
SCHEDULE_NAME: monday-morning
SCHEDULE_CRON: 0 9 * * 1
SCHEDULE_DESCRIPTION: "Trigger the projectname-skid bot once a week on monday morning"

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/skidname/version.py

deploy-dev:
name: Deploy to GCF
needs: test
name: Deploy to Cloud Run (dev)
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/dev'
if: github.ref_name == 'dev'
environment:
name: dev
permissions:
Expand All @@ -64,117 +44,15 @@ jobs:
steps:
- name: ⬇️ Set up code
uses: actions/checkout@v4

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

- name: 🚀 Deploy to Cloud Function
id: deploy
uses: google-github-actions/deploy-cloud-functions@v3
timeout-minutes: 15
with:
name: projectname-skid
runtime: python311
entry_point: subscribe
source_dir: src/projectname
service_account: cloud-function-sa@${{ secrets.PROJECT_ID }}.iam.gserviceaccount.com
event_trigger_type: google.cloud.pubsub.topic.v1.messagePublished
event_trigger_pubsub_topic: projects/${{ secrets.PROJECT_ID }}/topics/${{ env.SCHEDULE_NAME }}-topic
memory: ${{ env.CLOUD_FUNCTION_MEMORY }}
service_timeout: ${{ env.CLOUD_FUNCTION_RUN_TIMEOUT }}
environment_variables: STORAGE_BUCKET=${{secrets.STORAGE_BUCKET}}
secrets: |
/secrets/app/secrets.json=${{secrets.PROJECT_ID}}/skid-secrets
max_instance_count: 1
event_trigger_retry: false

- name: 📥 Create PubSub topic
run: |
if [ ! "$(gcloud pubsub topics list | grep $SCHEDULE_NAME-topic)" ]; then
gcloud pubsub topics create $SCHEDULE_NAME-topic --quiet
fi

- 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 pubsub $SCHEDULE_NAME \
--description="$SCHEDULE_DESCRIPTION" \
--schedule="$SCHEDULE_CRON" \
--time-zone=America/Denver \
--location=us-central1 \
--topic=$SCHEDULE_NAME-topic \
--message-body='foo' \
--quiet

deploy-prod:
name: Deploy to GCF
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

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

- name: 🚀 Deploy to Cloud Function
id: deploy
uses: google-github-actions/deploy-cloud-functions@v3
- name: 🚀 Deploy
uses: ./.github/actions/deploy
timeout-minutes: 15
with:
name: projectname-skid
runtime: python311
entry_point: main
source_dir: src/projectname
service_account: cloud-function-sa@${{ secrets.PROJECT_ID }}.iam.gserviceaccount.com
event_trigger_type: google.cloud.pubsub.topic.v1.messagePublished
event_trigger_pubsub_topic: projects/${{ secrets.PROJECT_ID }}/topics/${{ env.SCHEDULE_NAME }}-topic
memory: ${{ env.CLOUD_FUNCTION_MEMORY }}
service_timeout: ${{ env.CLOUD_FUNCTION_RUN_TIMEOUT }}
environment_variables: STORAGE_BUCKET=${{secrets.STORAGE_BUCKET}}
secrets: |
/secrets/ftp/known_hosts=${{secrets.PROJECT_ID}}/known_hosts
/secrets/app/secrets.json=${{secrets.PROJECT_ID}}/skid-secrets
max_instance_count: 1
event_trigger_retry: false

- name: 📥 Create PubSub topic
run: |
if [ ! "$(gcloud pubsub topics list | grep $SCHEDULE_NAME-topic)" ]; then
gcloud pubsub topics create $SCHEDULE_NAME-topic --quiet
fi

- 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 pubsub $SCHEDULE_NAME \
--description="$SCHEDULE_DESCRIPTION" \
--schedule="$SCHEDULE_CRON" \
--time-zone=America/Denver \
--location=us-central1 \
--topic=$SCHEDULE_NAME-topic \
--message-body='{"run": "now"}' \
--quiet
project_id: ${{ secrets.PROJECT_ID }}
identity_provider: ${{ secrets.IDENTITY_PROVIDER }}
service_account_email: ${{ secrets.SERVICE_ACCOUNT_EMAIL }}
pause_schedule_job: "yes"
github_token: ${{ secrets.GITHUB_TOKEN }}
Loading