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

feat: get version-id during build command #288

Merged
merged 4 commits into from
Feb 10, 2023
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
5 changes: 5 additions & 0 deletions .github/workflows/deploy_prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ jobs:
run: git fetch --tags

- name: Build
env:
STORAGE_URL: ${{ secrets.STORAGE_API_STAGE }}
run: make build ENVFILE=./env/prod

- name: Cross-build
env:
STORAGE_URL: ${{ secrets.STORAGE_API_STAGE }}
run: make cross-build ENVFILE=./env/prod

- name: Install AWS CLI
Expand Down Expand Up @@ -76,5 +80,6 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.RELEASE_GITHUB_TOKEN }}
API_URL: https://api.azionapi.net
STORAGE_URL: ${{ secrets.STORAGE_API_STAGE }}
AUTH_URL: https://sso.azion.com/api/user/me
TEMPLATE_BRANCH: main
2 changes: 2 additions & 0 deletions .github/workflows/deploy_stage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ jobs:
env:
AZION_API: ${{ secrets.AZION_API_STAGE }}
AZION_SSO: ${{ secrets.AZION_SSO_STAGE }}
STORAGE_URL: ${{ secrets.STORAGE_API_STAGE }}
run: make build ENVFILE=./env/stage

- name: Cross-build
env:
AZION_API: ${{ secrets.AZION_API_STAGE }}
AZION_SSO: ${{ secrets.AZION_SSO_STAGE }}
STORAGE_URL: ${{ secrets.STORAGE_API_STAGE }}
run: make cross-build ENVFILE=./env/stage

- name: Install AWS CLI
Expand Down
1 change: 1 addition & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ builds:
- -X github.com/aziontech/azion-cli/pkg/cmd/version.BinVersion={{ .Env.BIN_VERSION }}
- -X github.com/aziontech/azion-cli/pkg/constants.AuthURL={{ .Env.AUTH_URL }}
- -X github.com/aziontech/azion-cli/pkg/constants.ApiURL={{ .Env.API_URL }}
- -X github.com/aziontech/azion-cli/pkg/constants.StorageApiURL={{ .Env.STORAGE_URL }}
- -X github.com/aziontech/azion-cli/pkg/pkg/cmd/edge_applications/init.TemplateBranch={{ .Env.TEMPLATE_BRANCH }}
binary: azioncli
goos:
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ BIN_VERSION=$(shell git describe --tags)
LDFLAGS=-X github.com/aziontech/azion-cli/pkg/cmd/version.BinVersion=$(BIN_VERSION) \
-X github.com/aziontech/azion-cli/pkg/constants.AuthURL=$$AUTH_URL \
-X github.com/aziontech/azion-cli/pkg/constants.ApiURL=$$API_URL \
-X github.com/aziontech/azion-cli/pkg/constants.StorageApiURL=$$STORAGE_URL \
-X github.com/aziontech/azion-cli/pkg/cmd/edge_applications/init.TemplateBranch=$$TEMPLATE_BRANCH \
-X github.com/aziontech/azion-cli/pkg/cmd/edge_applications/init.TemplateMajor=$$TEMPLATE_MAJOR
LDFLAGS_STRIP=-s -w
Expand Down
1 change: 1 addition & 0 deletions env/prod
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
API_URL=https://api.azionapi.net
AUTH_URL=https://sso.azion.com/api/user/me
STORAGE_URL=https://${STORAGE_API}
TEMPLATE_BRANCH=main
TEMPLATE_MAJOR=0
1 change: 1 addition & 0 deletions env/stage
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ API_URL=https://${AZION_API}
AUTH_URL=https://${AZION_SSO}/api/user/me
TEMPLATE_BRANCH=dev
TEMPLATE_MAJOR=0
STORAGE_URL=https://${STORAGE_API}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/aziontech/azion-cli
go 1.17

require (
github.com/aziontech/azionapi-go-sdk v0.22.0
github.com/MaxwelMazur/tablecli v0.0.0-20230208145104-c9458b902b58
github.com/aziontech/azionapi-go-sdk v0.20.0
github.com/fatih/color v1.13.0
github.com/go-git/go-git/v5 v5.4.2
github.com/spf13/cobra v1.6.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
github.com/aziontech/azionapi-go-sdk v0.20.0 h1:a4hYtDlYMehwDBNGKaSgTFnKG9NNyjyE37HIsjlmGc8=
github.com/aziontech/azionapi-go-sdk v0.20.0/go.mod h1:2caFKH52viwZI346MWCGDZzU0jGll6udJZZMDJNrWgg=
github.com/aziontech/azionapi-go-sdk v0.22.0 h1:3b0UJr7zTH9ZOJRCaF6eEYGGxBfxTpIN23jkgLsp4uM=
github.com/aziontech/azionapi-go-sdk v0.22.0/go.mod h1:2caFKH52viwZI346MWCGDZzU0jGll6udJZZMDJNrWgg=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down
2 changes: 2 additions & 0 deletions messages/edge_applications/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,6 @@ var (
ErrorMissingApplicationIdJson = errors.New("Application ID is missing from azion.json. Please initialize and publish your project first before using cascade delete")
ErrorMissingAzionJson = errors.New("Azion.json file is missing. Please initialize and publish your project first before using cascade delete")
ErrorFailedUpdateAzionJson = errors.New("Failed to update azion.json file to remove IDs of deleted resource")

ErrorGetVersionId = errors.New("Failed to get Version Id: %s")
)
50 changes: 50 additions & 0 deletions pkg/api/storage_api/storage_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package storageapi

import (
"context"
"net/http"
"time"

"github.com/aziontech/azion-cli/pkg/cmd/version"
"github.com/aziontech/azion-cli/utils"
sdk "github.com/aziontech/azionapi-go-sdk/storageapi"
)

type Client struct {
apiClient *sdk.APIClient
}

type CreateRequest struct {
sdk.ApiCreateVersionRequest
}

type VersionIdResponse interface {
GetVersionId() string
}

func NewClient(c *http.Client, url string, token string) *Client {
conf := sdk.NewConfiguration()
conf.HTTPClient = c
conf.AddDefaultHeader("Authorization", "token "+token)
conf.AddDefaultHeader("Accept", "application/json;version=3")
conf.UserAgent = "Azion_CLI/" + version.BinVersion
conf.Servers = sdk.ServerConfigurations{
{URL: url},
}
conf.HTTPClient.Timeout = 30 * time.Second

return &Client{
apiClient: sdk.NewAPIClient(conf),
}
}

func (c *Client) CreateVersionId(ctx context.Context, id string) (VersionIdResponse, error) {
req := c.apiClient.EdgeApplicationsApi.CreateVersion(ctx, id)

res, httpResp, err := req.Execute()
if err != nil {
return nil, utils.ErrorPerStatusCode(httpResp, err)
}

return res, nil
}
82 changes: 60 additions & 22 deletions pkg/cmd/edge_applications/build/build.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package build

import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"

"github.com/tidwall/gjson"
"github.com/tidwall/sjson"

"github.com/MakeNowJust/heredoc"
msg "github.com/aziontech/azion-cli/messages/edge_applications"
api "github.com/aziontech/azion-cli/pkg/api/storage_api"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/contracts"
"github.com/aziontech/azion-cli/pkg/iostreams"
Expand All @@ -27,6 +30,8 @@ type BuildCmd struct {
GetWorkDir func() (string, error)
EnvLoader func(path string) ([]string, error)
Stat func(path string) (fs.FileInfo, error)
GetVerId func(cmd *BuildCmd, appID string) (string, error)
f *cmdutil.Factory
}

func NewCmd(f *cmdutil.Factory) *cobra.Command {
Expand Down Expand Up @@ -65,6 +70,8 @@ func newBuildCmd(f *cmdutil.Factory) *BuildCmd {
EnvLoader: utils.LoadEnvVarsFromFile,
WriteFile: os.WriteFile,
Stat: os.Stat,
GetVerId: GetVersionID,
f: f,
}
}

Expand All @@ -78,36 +85,32 @@ func (cmd *BuildCmd) run() error {
return err
}

jsonConf := path + "/azion/azion.json"
file, err := cmd.FileReader(jsonConf)
if err != nil {
return msg.ErrorOpeningAzionFile
}

typeLang := gjson.Get(string(file), "type")

if typeLang.String() == "cdn" {
fmt.Fprintf(cmd.Io.Out, "%s\n", msg.EdgeApplicationsBuildCdn)
return nil
}

err = RunBuildCmdLine(cmd, typeLang.String())
err = RunBuildCmdLine(cmd, path)
if err != nil {
return err
}

return nil
}

func RunBuildCmdLine(cmd *BuildCmd, typeLang string) error {
func RunBuildCmdLine(cmd *BuildCmd, path string) error {
var err error

conf, err := getConfig(cmd)
azionJson := path + "/azion/azion.json"
file, err := cmd.FileReader(azionJson)
if err != nil {
return err
return msg.ErrorOpeningAzionFile
}

path, err := utils.GetWorkingDir()
typeLang := gjson.Get(string(file), "type")
applicationId := gjson.Get(string(file), "application.id")

if typeLang.String() == "cdn" {
fmt.Fprintf(cmd.Io.Out, "%s\n", msg.EdgeApplicationsBuildCdn)
return nil
}

conf, err := getConfig(cmd)
if err != nil {
return err
}
Expand All @@ -122,7 +125,7 @@ func RunBuildCmdLine(cmd *BuildCmd, typeLang string) error {
return msg.ErrReadEnvFile
}
} else if errors.Is(err, os.ErrNotExist) {
if typeLang == "nextjs" || typeLang == "flareact" {
if typeLang.String() == "nextjs" || typeLang.String() == "flareact" {
envs = insertAWSCredentials(cmd)
notFound = true
}
Expand All @@ -135,9 +138,25 @@ func RunBuildCmdLine(cmd *BuildCmd, typeLang string) error {
return err
}

switch typeLang {
case "javascript", "nextjs", "flareact":
err := runCommand(cmd, conf, envs)
switch typeLang.String() {
case "nextjs", "flareact":

verId, err := cmd.GetVerId(cmd, applicationId.String())
if err != nil {
return err
}

jsonReplaceFunc, err := sjson.Set(string(file), "version-id", verId)
if err != nil {
return msg.ErrorFailedUpdateAzionJson
}

err = cmd.WriteFile(azionJson, []byte(jsonReplaceFunc), 0644)
if err != nil {
return fmt.Errorf(utils.ErrorCreateFile.Error(), azionJson)
}

err = runCommand(cmd, conf, envs)
if err != nil {
return err
}
Expand All @@ -147,6 +166,11 @@ func RunBuildCmdLine(cmd *BuildCmd, typeLang string) error {
return errEnv
}
}
case "javascript":
err := runCommand(cmd, conf, envs)
if err != nil {
return err
}

default:
return utils.ErrorUnsupportedType
Expand Down Expand Up @@ -284,3 +308,17 @@ func writeWebdevEnvFile(cmd *BuildCmd, path string, envs []string) error {
}
return nil
}

func GetVersionID(cmd *BuildCmd, appID string) (string, error) {

client := api.NewClient(cmd.f.HttpClient, cmd.f.Config.GetString("storage_url"), cmd.f.Config.GetString("token"))

ctx := context.Background()

verId, err := client.CreateVersionId(ctx, appID)
if err != nil {
return "", fmt.Errorf(msg.ErrorGetVersionId.Error(), err)
}

return verId.GetVersionId(), nil
}
8 changes: 8 additions & 0 deletions pkg/cmd/edge_applications/edge_applications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ func TestEdgeApplicationsCmd(t *testing.T) {
return "Build completed", 0, nil
}

buildCommand.GetVerId = func(cmd *buildcmd.BuildCmd, appID string) (string, error) {
return "v123456789", nil
}

buildCommand.Stat = func(path string) (fs.FileInfo, error) {
return nil, nil
}
Expand Down Expand Up @@ -406,6 +410,10 @@ func TestEdgeApplicationsCmd(t *testing.T) {
return "Build completed", 0, nil
}

buildCommand.GetVerId = func(cmd *buildcmd.BuildCmd, appID string) (string, error) {
return "v123456789", nil
}

buildCommand.EnvLoader = func(path string) ([]string, error) {
return buildEnvs, nil
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

msg "github.com/aziontech/azion-cli/messages/root"
"github.com/aziontech/azion-cli/pkg/cmd/configure"
"github.com/aziontech/azion-cli/pkg/cmd/domains"
"github.com/aziontech/azion-cli/pkg/cmd/edge_applications"
"github.com/aziontech/azion-cli/pkg/cmd/edge_functions"
"github.com/aziontech/azion-cli/pkg/cmd/edge_services"
"github.com/aziontech/azion-cli/pkg/cmd/domains"
"github.com/aziontech/azion-cli/pkg/cmd/origins"
"github.com/aziontech/azion-cli/pkg/cmd/origins"
"github.com/aziontech/azion-cli/pkg/cmd/version"
"github.com/aziontech/azion-cli/pkg/cmdutil"
"github.com/aziontech/azion-cli/pkg/constants"
Expand Down Expand Up @@ -69,6 +69,7 @@ func Execute() {
viper.AutomaticEnv()
viper.SetDefault("token", tok)
viper.SetDefault("api_url", constants.ApiURL)
viper.SetDefault("storage_url", constants.StorageApiURL)

factory := &cmdutil.Factory{
HttpClient: httpClient,
Expand Down
5 changes: 3 additions & 2 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package constants

// These variables have their values set at link time through the Makefile
var (
ApiURL string
AuthURL string
ApiURL string
AuthURL string
StorageApiURL string
)