Skip to content

Commit

Permalink
feat(engine): add release action for artifactory (#5849)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored Jun 18, 2021
1 parent 12a4efb commit 095de77
Show file tree
Hide file tree
Showing 18 changed files with 636 additions and 56 deletions.
18 changes: 14 additions & 4 deletions cli/cdsctl/workflow_artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ var workflowArtifactDownloadCmd = cli.Command{
Usage: "exclude files from download - could be a regex: *.log",
Default: "",
},
{
Name: "cdn-url",
Usage: "overwrite cdn url",
Default: "",
},
},
}

Expand All @@ -106,10 +111,15 @@ func workflowArtifactDownloadRun(v cli.Values) error {
return cli.NewError("number parameter have to be an integer")
}

confCDN, err := client.ConfigCDN()
if err != nil {
return err
cdnURL := v.GetString("cdn-url")
if cdnURL == "" {
confCDN, err := client.ConfigCDN()
if err != nil {
return err
}
cdnURL = confCDN.HTTPURL
}

ok, err := downloadFromCDSAPI(v, number)
if err != nil {
return err
Expand Down Expand Up @@ -176,7 +186,7 @@ func workflowArtifactDownloadRun(v cli.Values) error {
return err
}
fmt.Printf("Downloading %s...\n", artifactData.Name)
r, err := client.CDNItemDownload(context.Background(), confCDN.HTTPURL, artifactData.CDNRefHash, sdk.CDNTypeItemRunResult)
r, err := client.CDNItemDownload(context.Background(), cdnURL, artifactData.CDNRefHash, sdk.CDNTypeItemRunResult)
if err != nil {
return err
}
Expand Down
19 changes: 19 additions & 0 deletions contrib/integrations/artifactory/artifactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ import (
"encoding/json"
"fmt"
"net/url"
"strings"
"time"

"github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/artifactory/auth"
"github.com/jfrog/jfrog-client-go/artifactory/services"
"github.com/jfrog/jfrog-client-go/config"
"github.com/jfrog/jfrog-client-go/distribution"
authdistrib "github.com/jfrog/jfrog-client-go/distribution/auth"


"github.com/ovh/cds/sdk"
)
Expand Down Expand Up @@ -43,6 +47,21 @@ type FileChildren struct {
Folder bool `json:"folder"`
}

func CreateDistributionClient(url, token string) (*distribution.DistributionServicesManager, error) {
dtb := authdistrib.NewDistributionDetails()
dtb.SetUrl(strings.Replace(url, "/artifactory/", "/distribution/", -1))
dtb.SetAccessToken(token)
serviceConfig, err := config.NewConfigBuilder().
SetServiceDetails(dtb).
SetThreads(1).
SetDryRun(false).
Build()
if err != nil {
return nil, fmt.Errorf("unable to create service config: %v", err)
}
return distribution.New(serviceConfig)
}

func CreateArtifactoryClient(url, token string) (artifactory.ArtifactoryServicesManager, error) {
rtDetails := auth.NewArtifactoryDetails()
rtDetails.SetUrl(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ type executionContext struct {
workflowName string
version string
lowMaturitySuffix string
cdsRepo string
}

func (e *artifactoryBuildInfoPlugin) Manifest(_ context.Context, _ *empty.Empty) (*integrationplugin.IntegrationPluginManifest, error) {
Expand All @@ -67,7 +66,6 @@ func (e *artifactoryBuildInfoPlugin) Run(_ context.Context, opts *integrationplu
token := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactManagerConfigToken)]
tokenName := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactManagerConfigTokenName)]
lowMaturitySuffix := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactManagerConfigPromotionLowMaturity)]
cdsRepo := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactManagerConfigCdsRepository)]

buildInfo := opts.GetOptions()[fmt.Sprintf("cds.integration.artifact_manager.%s", sdk.ArtifactManagerConfigBuildInfoPath)]
version := opts.GetOptions()["cds.version"]
Expand Down Expand Up @@ -117,7 +115,6 @@ func (e *artifactoryBuildInfoPlugin) Run(_ context.Context, opts *integrationplu
workflowName: workflowName,
version: version,
projectKey: projectKey,
cdsRepo: cdsRepo,
}
modules, err := e.computeBuildInfoModules(artiClient, execContext)
if err != nil {
Expand Down Expand Up @@ -195,9 +192,7 @@ func (e *artifactoryBuildInfoPlugin) retrieveModulesArtifacts(client artifactory
props["build.number"] = execContext.version
props["build.timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
repoSrc := repoName
if repoName != execContext.cdsRepo {
repoSrc += "-" + execContext.lowMaturitySuffix
}
repoSrc += "-" + execContext.lowMaturitySuffix
if err := art.SetProperties(client, repoSrc, path, props); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
.PHONY: clean

VERSION := $(if ${CDS_SEMVER},${CDS_SEMVER},snapshot)
GITHASH := $(if ${GIT_HASH},${GIT_HASH},`git log -1 --format="%H"`)
BUILDTIME := `date "+%m/%d/%y-%H:%M:%S"`
CDSCTL := $(if ${CDSCTL},${CDSCTL},cdsctl)

TARGET_DIR = ./dist
TARGET_NAME = plugin-artifactory-release

define PLUGIN_MANIFEST_BINARY
os: %os%
arch: %arch%
cmd: ./%filename%
endef
export PLUGIN_MANIFEST_BINARY

TARGET_LDFLAGS = -ldflags "-X github.com/ovh/cds/sdk.VERSION=$(VERSION) -X github.com/ovh/cds/sdk.GOOS=$$GOOS -X github.com/ovh/cds/sdk.GOARCH=$$GOARCH -X github.com/ovh/cds/sdk.GITHASH=$(GITHASH) -X github.com/ovh/cds/sdk.BUILDTIME=$(BUILDTIME) -X github.com/ovh/cds/sdk.BINARY=$(TARGET_NAME)"
TARGET_OS = $(if ${OS},${OS},windows darwin linux freebsd)
TARGET_ARCH = $(if ${ARCH},${ARCH},amd64 arm 386 arm64)

GO_BUILD = go build

$(TARGET_DIR):
$(info create $(TARGET_DIR) directory)
@mkdir -p $(TARGET_DIR)

default: build

clean:
@rm -rf $(TARGET_DIR)

build: $(TARGET_DIR)
@cp $(TARGET_NAME).yml $(TARGET_DIR)/$(TARGET_NAME).yml
@for GOOS in $(TARGET_OS); do \
for GOARCH in $(TARGET_ARCH); do \
EXTENSION=""; \
if test "$$GOOS" = "windows" ; then EXTENSION=".exe"; fi; \
echo Compiling $(TARGET_DIR)/$(TARGET_NAME)-$$GOOS-$$GOARCH$$EXTENSION $(VERSION); \
FILENAME=$(TARGET_NAME)-$$GOOS-$$GOARCH$$EXTENSION; \
GOOS=$$GOOS GOARCH=$$GOARCH CGO_ENABLED=0 $(GO_BUILD) $(TARGET_LDFLAGS) -o $(TARGET_DIR)/$$FILENAME; \
echo "$$PLUGIN_MANIFEST_BINARY" > $(TARGET_DIR)/plugin-artifactory-release-$$GOOS-$$GOARCH.yml; \
perl -pi -e s,%os%,$$GOOS,g $(TARGET_DIR)/plugin-artifactory-release-$$GOOS-$$GOARCH.yml; \
perl -pi -e s,%arch%,$$GOARCH,g $(TARGET_DIR)/plugin-artifactory-release-$$GOOS-$$GOARCH.yml; \
perl -pi -e s,%filename%,$$FILENAME,g $(TARGET_DIR)/plugin-artifactory-release-$$GOOS-$$GOARCH.yml; \
done; \
done

publish:
@echo "Updating plugin..."
$(CDSCTL) admin plugins import $(TARGET_DIR)/$(TARGET_NAME).yml
@for GOOS in $(TARGET_OS); do \
for GOARCH in $(TARGET_ARCH); do \
EXTENSION=""; \
if test "$$GOOS" = "windows" ; then EXTENSION=".exe"; fi; \
echo "Updating plugin binary $(TARGET_NAME)-$$GOOS-$$GOARCH$$EXTENSION"; \
$(CDSCTL) admin plugins binary-add artifactory-release-plugin $(TARGET_DIR)/$(TARGET_NAME)-$$GOOS-$$GOARCH.yml $(TARGET_DIR)/$(TARGET_NAME)-$$GOOS-$$GOARCH$$EXTENSION; \
done; \
done
Loading

0 comments on commit 095de77

Please sign in to comment.