Skip to content

Commit

Permalink
support latest version with the latest tag
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher Larivière <[email protected]>
  • Loading branch information
larivierec committed Dec 7, 2023
1 parent eb6166c commit e8c1626
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 22 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/builder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ jobs:
- name: Matrices
id: prepare-matrices
shell: bash
env:
GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}
GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }}
run: |
if [[ -z "${{ inputs.appsToBuild }}" ]]; then
matrices=$(go run cmd/containers.go "all" "true" "false" "stable")
matrices=$(go run cmd/main.go "all" "${{inputs.pushImages}}" "${{inputs.force}}" "stable")
else
matrices=$(go run cmd/containers.go "${{ inputs.appsToBuild }}" "true" "false" "stable")
matrices=$(go run cmd/main.go "${{ inputs.appsToBuild }}" "${{inputs.pushImages}}" "${{inputs.force}}" "stable")
fi
echo "matrices=${matrices}" >> $GITHUB_OUTPUT
echo "${matrices}"
Expand Down Expand Up @@ -138,6 +141,7 @@ jobs:
images: ghcr.io/${{github.repository_owner}}/${{ matrix.image.name }}
tags: |
type=raw,value=v${{matrix.image.version}}
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and Push
uses: docker/build-push-action@4a13e500e55cf31b7a5d59a38ab2040ab0f42f56 # v5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
name: Build & publish images
name: Manual - Build & publish images
on:
push:
tags:
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/release-on-merge.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
name: Release on Merge
name: Merge - Build & publish images

concurrency:
group: container-release
Expand All @@ -25,3 +25,4 @@
with:
appsToBuild: "${{ needs.get-changed-images.outputs.addedOrModifiedImages }}"
pushImages: true
force: false
3 changes: 2 additions & 1 deletion .github/workflows/release-schedule.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
name: Scheduled Release
name: Scheduled - Build & publish images

concurrency:
group: container-release
Expand Down Expand Up @@ -32,3 +32,4 @@
with:
appsToBuild: ${{ inputs.appsToBuild }}
pushImages: true
force: false
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
**/terraform.tfstate**
**/*.tfstate
**/*.bitwarden
.actsecret
.goenv
## macos
.DS_Store
4 changes: 3 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
"name": "Launch Package",
"type": "go",
"request": "launch",
"program": "cmd/containers.go",
"mode": "debug",
"program": "cmd/main.go",
"cwd": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.goenv",
"args": [
"all",
"true",
Expand Down
5 changes: 5 additions & 0 deletions api/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package api

type Interface interface {
GetPublishedVersion(imageName string) (string, error)
}
40 changes: 25 additions & 15 deletions cmd/containers.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
github "larivierec/containers/m/v2/internal"
"log"
"os"
"os/exec"
Expand All @@ -13,6 +14,8 @@ import (
"gopkg.in/yaml.v3"
)

var githubApi = &github.Github{}

type Platform struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
Expand Down Expand Up @@ -112,14 +115,16 @@ func getPlatformMetadata(subdir string, meta Metadata, forRelease, force bool, c
toBuild["name"] = fmt.Sprintf("%s-%s", meta.App, channel.Name)
}

// published := getPublishedVersion(toBuild["name"])
// if !force && published != "" && published == version {
// continue
// }

// toBuild["published_version"] = published
// Skip if latest version already published
if !force {
published, err := githubApi.GetPublishedVersion(toBuild["name"].(string))
if (err == nil && published != "") && strings.Contains(published, version) {
continue
}
toBuild["published_version"] = published
}
toBuild["version"] = version
toBuild["tags"] = []string{"rolling", version}
toBuild["tags"] = []string{"latest", version}
toBuild["label_type"] = "org.opencontainers.image"

for _, platform := range channel.Platforms {
Expand All @@ -146,9 +151,10 @@ func getPlatformMetadata(subdir string, meta Metadata, forRelease, force bool, c
}

func main() {
apiInit()
args := os.Args[1:]
if len(args) < 3 {
fmt.Println("Usage: go run cmd/containers.go <apps> <forRelease> <force> [<channels>]")
fmt.Println("Usage: go run cmd/main.go <apps> <forRelease> <force> [<channels>]")
os.Exit(1)
}

Expand Down Expand Up @@ -211,11 +217,15 @@ func processSpecificApps(selectedApps []string, forRelease, force bool, channels
}
}

// TODO evaluate if we actually need this
// func getPublishedVersion(imageName string) string {
// return ""
// }
func apiInit() {
githubApi.RepoOwner = os.Getenv("GITHUB_REPOSITORY_OWNER")
githubApi.Token = os.Getenv("GITHUB_TOKEN")

// func getMetadataForFile(subdir, file string) *Metadata {
// return nil
// }
if githubApi.RepoOwner == "" {
githubApi.RepoOwner = os.Getenv("REPO_OWNER")
}

if githubApi.Token == "" {
githubApi.Token = os.Getenv("TOKEN")
}
}
67 changes: 67 additions & 0 deletions internal/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package github

import (
"encoding/json"
"fmt"
"net/http"
)

type Github struct {
Token string
RepoOwner string
}

func (g *Github) GetPublishedVersion(imageName string) (string, error) {
url := fmt.Sprintf("https://api.github.com/users/%s/packages/container/%s/versions", g.RepoOwner, imageName)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return "", err
}

req.Header.Set("Accept", "application/vnd.github.v3+json")
req.Header.Set("Authorization", "token "+g.Token)

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return "", err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
fmt.Println("Received non-OK status code:", resp.StatusCode)
return "", err
}

var data []map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
fmt.Println("Error decoding response body:", err)
return "", err
}

for _, image := range data {
if metadata, ok := image["metadata"].(map[string]interface{}); ok {
if container, ok := metadata["container"].(map[string]interface{}); ok {
if tags, ok := container["tags"].([]interface{}); ok {
for i, tag := range tags {
if tagStr, ok := tag.(string); ok && tagStr == "latest" {
tags = append(tags[:i], tags[i+1:]...)
longest := ""
for _, t := range tags {
if tStr, ok := t.(string); ok && len(tStr) > len(longest) {
longest = tStr
}
}
return longest, nil
}
}
}
}
}
}

return "", fmt.Errorf("no data to go through")
}

0 comments on commit e8c1626

Please sign in to comment.