Skip to content

Commit

Permalink
Merge pull request #4 from perses/nexucis/upload-archive
Browse files Browse the repository at this point in the history
upload archive to github release
  • Loading branch information
jgbernalp authored Jul 15, 2024
2 parents 0144aa0 + 0f8338f commit ba8ced1
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 29 deletions.
28 changes: 27 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ on:
- main
pull_request:
merge_group:
release:
types:
- published

jobs:
build:
Expand Down Expand Up @@ -40,4 +43,27 @@ jobs:
with:
enable_npm: true
- run: npm ci
- run: npm run lint
- run: npm run lint

release:
name: "release"
needs: "build"
runs-on: ubuntu-latest
permissions:
contents: write
if: ${{ github.event.release.tag_name }}
env:
GITHUB_TOKEN: ${{ github.TOKEN }}
steps:
- name: checkout
uses: actions/checkout@v4
- uses: perses/github-actions@v0.5.2
- uses: ./.github/perses-ci/actions/setup_environment
with:
enable_npm: false
enable_go: true
- name: Download archive
uses: actions/download-artifact@v4
with:
name: archives
- run: go run ./scripts/upload-archive/upload-archive.go -tag=${{ github.event.release.tag_name }}
50 changes: 22 additions & 28 deletions scripts/build-archive/build-archive.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,49 @@
// Copyright 2024 The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path"

"github.com/perses/perses-plugins/scripts/npm"
"github.com/sirupsen/logrus"
)

type npmPackage struct {
Workspaces []string `json:"workspaces"`
}

type manifest struct {
ID string `json:"id"`
Name string `json:"name"`
}

func createArchive(archivePath string) error {
distPath := path.Join(archivePath, "dist")
manifestFilePath := path.Join(distPath, "mf-manifest.json")
data, err := os.ReadFile(manifestFilePath)
func createArchive(pluginName string) error {
distPath := path.Join(pluginName, "dist")
manifest, err := npm.ReadManifest(pluginName)
if err != nil {
return err
}
manifestData := manifest{}
if unmarshalErr := json.Unmarshal(data, &manifestData); unmarshalErr != nil {
return err
logrus.Fatal(err)
}
newArchiveFolder := path.Join(archivePath, manifestData.ID)
newArchiveFolder := path.Join(pluginName, manifest.ID)
if execErr := exec.Command("cp", "-r", distPath, newArchiveFolder).Run(); execErr != nil {
return execErr
}
if execErr := exec.Command("tar", "-czvf", path.Join(archivePath, fmt.Sprintf("%s.tar.gz", manifestData.ID)), newArchiveFolder).Run(); execErr != nil {
if execErr := exec.Command("tar", "-czvf", path.Join(pluginName, fmt.Sprintf("%s.tar.gz", manifest.ID)), newArchiveFolder).Run(); execErr != nil {
return execErr
}
return exec.Command("rm", "-rf", newArchiveFolder).Run()
}

func main() {
data, err := os.ReadFile("package.json")
workspaces, err := npm.GetWorkspaces()
if err != nil {
logrus.Fatal(err)
}
pkg := npmPackage{}
if unmarshalErr := json.Unmarshal(data, &pkg); unmarshalErr != nil {
logrus.Fatal(unmarshalErr)
}
for _, workspace := range pkg.Workspaces {
for _, workspace := range workspaces {
logrus.Infof("building archive for the plugin %s", workspace)
if createArchiveErr := createArchive(workspace); createArchiveErr != nil {
logrus.Fatal(err)
Expand Down
51 changes: 51 additions & 0 deletions scripts/npm/npm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2024 The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package npm

import (
"encoding/json"
"os"
"path"
)

type Package struct {
Workspaces []string `json:"workspaces"`
}

func GetWorkspaces() ([]string, error) {
data, err := os.ReadFile("package.json")
if err != nil {
return nil, err
}
pkg := Package{}
if unmarshalErr := json.Unmarshal(data, &pkg); unmarshalErr != nil {
return nil, unmarshalErr
}
return pkg.Workspaces, nil
}

type Manifest struct {
ID string `json:"id"`
Name string `json:"name"`
}

func ReadManifest(pluginPath string) (*Manifest, error) {
manifestFilePath := path.Join(pluginPath, "dist", "mf-manifest.json")
data, err := os.ReadFile(manifestFilePath)
if err != nil {
return nil, err
}
manifestData := &Manifest{}
return manifestData, json.Unmarshal(data, manifestData)
}
44 changes: 44 additions & 0 deletions scripts/upload-archive/upload-archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"flag"
"fmt"
"os/exec"
"path"
"regexp"

"github.com/perses/perses-plugins/scripts/npm"
"github.com/sirupsen/logrus"
)

var tagNamePattern = regexp.MustCompile(".*-\\d.\\d.\\d")

func main() {
tag := flag.String("tag", "", "Name of the tag")
flag.Parse()
tagSplitted := tagNamePattern.FindStringSubmatch(*tag)
if len(tagSplitted) != 2 {
logrus.Fatalf("Invalid tag name: %s", *tag)
}
pluginName := tagSplitted[1]
manifest, err := npm.ReadManifest(pluginName)
if err != nil {
logrus.Fatal(err)
}
if execErr := exec.Command("gh", "release", "upload", *tag, path.Join(pluginName, fmt.Sprintf("%s.tar.gz", manifest.ID))).Run(); execErr != nil {
logrus.WithError(err).Fatalf("unable to upload archive %s", pluginName)
}
}

0 comments on commit ba8ced1

Please sign in to comment.