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

Add looping capability + deploy configurationn #4

Merged
merged 1 commit into from
Mar 28, 2024
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
68 changes: 68 additions & 0 deletions .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Build & Deploy

on:
push:
branches:
- main
workflow_dispatch:
schedule:
- cron: '10 17 * * 4'

permissions:
id-token: write
contents: read
packages: write

jobs:
package:
uses: Chia-Network/actions/.github/workflows/docker-build.yaml@main

deploy:
name: Deploy ${{ matrix.mode.name }}
needs:
- package
runs-on: [k8s-public]
container:
image: registry.gitlab.com/cmmarslender/kubectl-helm:v3
strategy:
fail-fast: false
matrix:
mode:
- name: label-prs
steps:
- uses: actions/checkout@v4

- name: Vault Login
uses: Chia-Network/actions/vault/login@main
with:
vault_url: ${{ secrets.VAULT_URL }}
role_name: github-github-bot

- name: Get secrets from vault
uses: hashicorp/vault-action@v2
with:
url: ${{ secrets.VAULT_URL }}
token: ${{ env.VAULT_TOKEN }}
secrets: |
secret/data/fmt/k8s/fmt-k8s-internal api_server_url | K8S_API_SERVER_URL;
secret/data/github_users/chiaautomation/github-bot token | BOT_GITHUB_TOKEN;
secret/data/github/teams internal | INTERNAL_TEAM_NAME;

- name: Login to k8s cluster
uses: Chia-Network/actions/vault/k8s-login@main
with:
vault_url: ${{ secrets.VAULT_URL }}
vault_token: ${{ env.VAULT_TOKEN }}
backend_name: fmt-k8s-internal
role_name: github-actions
cluster_url: ${{ env.K8S_API_SERVER_URL }}

- uses: Chia-Network/actions/helm/deploy@main
env:
DOCKER_TAG: "sha-${{ github.sha }}"
with:
namespace: "github-bot"
app_name: "github-bot-${{ matrix.mode.name }}"
helm_chart_repo: "https://chia-network.github.io/helm-charts"
helm_chart: "generic"
helm_values: "./k8s/${{ matrix.mode.name }}.yml"
14 changes: 14 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Build Docker Images

on:
pull_request:
workflow_call:

concurrency:
# SHA is added to the end if on `main` to let all main workflows run
group: ${{ github.ref }}-${{ github.workflow }}-${{ github.event_name }}-${{ (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/') || startsWith(github.ref, 'refs/heads/long_lived/')) && github.sha || '' }}
cancel-in-progress: true

jobs:
package:
uses: Chia-Network/actions/.github/workflows/docker-build.yaml@main
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:1 as builder

COPY . /app
WORKDIR /app
RUN make build

FROM gcr.io/distroless/static-debian12

COPY --from=builder /app/bin/github-bot /github-bot

ENTRYPOINT ["/github-bot", "--config", "/config.yml"]
20 changes: 17 additions & 3 deletions cmd/labelPRs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"log"
"time"

"github.com/google/go-github/v60/github"
"github.com/spf13/cobra"
Expand All @@ -21,9 +22,22 @@ var labelPRsCmd = &cobra.Command{
log.Fatalf("error loading config: %s\n", err.Error())
}
client := github.NewClient(nil).WithAuthToken(cfg.GithubToken)
err = label.PullRequests(client, cfg.InternalTeam, cfg.LabelConfig)
if err != nil {
log.Fatalln(err.Error())

loop := viper.GetBool("loop")
loopDuration := viper.GetDuration("loop-time")
for {
log.Println("Labeling Pull Requests")
err = label.PullRequests(client, cfg.InternalTeam, cfg.LabelConfig)
if err != nil {
log.Fatalln(err.Error())
}

if !loop {
break
}

log.Printf("Waiting %s for next iteration\n", loopDuration.String())
time.Sleep(loopDuration)
}
},
}
Expand Down
11 changes: 10 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -26,12 +27,20 @@ func Execute() {
}

func init() {
var cfgFile string
var (
cfgFile string
loop bool
loopTime time.Duration
)

cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "config.yml", "config file to load")
rootCmd.PersistentFlags().BoolVar(&loop, "loop", false, "Use this var to periodically check on a loop")
rootCmd.PersistentFlags().DurationVar(&loopTime, "loop-time", 1*time.Hour, "The amount of time to wait between each iteration of the loop")

cobra.CheckErr(viper.BindPFlag("config", rootCmd.PersistentFlags().Lookup("config")))
cobra.CheckErr(viper.BindPFlag("loop", rootCmd.PersistentFlags().Lookup("loop")))
cobra.CheckErr(viper.BindPFlag("loop-time", rootCmd.PersistentFlags().Lookup("loop-time")))
}

// initConfig reads in config file and ENV variables if set.
Expand Down
24 changes: 24 additions & 0 deletions k8s/label-prs.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
replicaCount: 1
image:
repository: ghcr.io/chia-network/github-bot
tag: {{ DOCKER_TAG }}

deployment:
args:
- label-prs
- --loop

# Creates a secret with the following values, and mounts as a file into the main deployment container
secretFile:
mountPath: "/config.yml"
stringValues:
github_token: "{{ BOT_GITHUB_TOKEN }}"
internal_team: "{{ INTERNAL_TEAM_NAME }}"
label_internal: ""
label_external: "community-pr"
label_check_repos:
- name: "Chia-Network/chia-blockchain"
minimum_number: 17788
label_skip_users:
- "dependabot[bot]"