github: check new kernel commit

This commit is contained in:
oliverpool 2022-03-23 17:32:29 +01:00
parent 9133a3a809
commit 9412a29536
2 changed files with 48 additions and 25 deletions

View file

@ -1,4 +1,4 @@
name: Auto-update firmware name: Auto-update kernel
on: on:
push: push:
@ -13,8 +13,6 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
with:
submodules: true
- name: Set up Go - name: Set up Go
uses: actions/setup-go@v2 uses: actions/setup-go@v2
@ -23,13 +21,13 @@ jobs:
- name: Check latest kernel version from https://archive.raspberrypi.org/debian/ - name: Check latest kernel version from https://archive.raspberrypi.org/debian/
id: check id: check
run: echo "::set-output name=version::$(go run ./cmd/check-update/main.go)" run: echo "::set-output name=sha::$(go run ./cmd/check-update/main.go)"
- name: Fetch latest kernel - name: Fetch latest kernel
if: steps.check.outputs.version != '' if: steps.check.outputs.sha != ''
run: echo "checkout ${{steps.check.outputs.version}}" run: echo "checkout ${{steps.check.outputs.sha}}"
- name: Commit the new kernel - name: Commit the new kernel
uses: stefanzweifel/git-auto-commit-action@v4 uses: stefanzweifel/git-auto-commit-action@v4
with: with:
commit_message: kernel ${{steps.check.outputs.version}} commit_message: kernel ${{steps.check.outputs.sha}}

View file

@ -2,6 +2,7 @@ package main
import ( import (
"bufio" "bufio"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"log" "log"
@ -51,40 +52,64 @@ func run() error {
log.Println("latest version:", tagName) log.Println("latest version:", tagName)
tags, err := gitTags("linux-sources") latestSha, err := githubCommitSha(tagName)
if err != nil { if err != nil {
return err return err
} }
for _, tag := range tags { log.Println("latest commit:", latestSha)
if tagName == tag {
currentSha, err := submoduleSha("linux-sources")
if err != nil {
return err
}
log.Println("submodule commit:", currentSha)
if latestSha == currentSha {
log.Println("already up to date") log.Println("already up to date")
return nil return nil
} }
} fmt.Println(latestSha)
log.Println("outdated tag", tags)
fmt.Println(tagName)
return nil return nil
} }
func gitTags(folder string) ([]string, error) { func githubCommitSha(tagName string) (string, error) {
current, err := os.Getwd() req, err := http.NewRequest("GET", "https://api.github.com/repos/raspberrypi/linux/git/ref/tags/"+tagName, nil)
if err != nil { if err != nil {
return nil, err return "", err
} }
defer os.Chdir(current) req.Header.Add("Accept", "application/vnd.github.v3+json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
err = os.Chdir(folder) type githubResponse struct {
if err != nil { Message string `json:"message"`
return nil, err Object struct {
Sha string `json:"sha"`
} `json:"object"`
} }
cmd := exec.Command("git", "tag", "--points-at", "HEAD") var gr githubResponse
err = json.NewDecoder(resp.Body).Decode(&gr)
if err != nil {
return "", err
}
if gr.Object.Sha == "" {
return "", errors.New("could not get sha: " + gr.Message)
}
return gr.Object.Sha, nil
}
func submoduleSha(submodule string) (string, error) {
cmd := exec.Command("git", "rev-parse", "HEAD:"+submodule)
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
return nil, err return "", err
} }
return strings.Split(strings.TrimSpace(string(out)), "\n"), nil return strings.TrimSpace(string(out)), nil
} }
func scanOnlineTextFile(url string, stopScanning func(string) bool) error { func scanOnlineTextFile(url string, stopScanning func(string) bool) error {