Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
csandanov committed Mar 1, 2019
1 parent 25169fc commit 8767054
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 66 deletions.
36 changes: 16 additions & 20 deletions cmd/wodby/ci/initialize/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ import (
)

type options struct {
uuid string
context string
dind bool
buildNumber string
username string
email string
url string
fixPermissionDir string
provider string
message string
uuid string
context string
dind bool
buildNumber string
username string
email string
url string
fixPermissions bool
provider string
message string
}

var opts options
Expand Down Expand Up @@ -179,15 +179,11 @@ var Cmd = &cobra.Command{
}

// We will fix permissions either when it was instructed or when a it's a managed stack and a known CI environment.
if opts.fixPermissionDir != "" || (!config.BuildConfig.Custom && metadata.ProjectDir != "") {
var dir string

if opts.fixPermissionDir != "" {
dir = opts.fixPermissionDir
fmt.Println("Fixing codebase permissions in a provided directory...")
if opts.fixPermissions || (!config.BuildConfig.Custom && metadata.Provider != "Unknown") {
if opts.fixPermissions {
fmt.Println("Instructed to fix codebase permissions...")
} else {
dir = metadata.ProjectDir
fmt.Println(fmt.Sprintf("Managed stack detected in a known CI environment %s – fixing codebase permissions in %s...", metadata.Provider, metadata.ProjectDir))
fmt.Println(fmt.Sprintf("Managed stack detected in a known CI environment %s – automatically fixing codebase permissions...", metadata.Provider))
}

defaultUser, err := dockerClient.GetImageDefaultUser(defaultService.Image)
Expand All @@ -208,7 +204,7 @@ var Cmd = &cobra.Command{
runConfig.Volumes = append(runConfig.Volumes, fmt.Sprintf("%s:%s", config.Context, config.WorkingDir))
}

args := []string{"chown", "-R", fmt.Sprintf("%s:%s", defaultUser, defaultUser), dir}
args := []string{"chown", "-R", fmt.Sprintf("%s:%s", defaultUser, defaultUser), "."}
err := dockerClient.Run(args, runConfig)

if err != nil {
Expand Down Expand Up @@ -262,7 +258,7 @@ var Cmd = &cobra.Command{
func init() {
Cmd.Flags().StringVarP(&opts.context, "context", "c", "", "Build context (default: current directory)")
Cmd.Flags().BoolVar(&opts.dind, "dind", false, "Use data container for sharing files between commands")
Cmd.Flags().StringVar(&opts.fixPermissionDir, "fix-permissions-dir", "", "Fix permissions to the default service user id in provided directory. Performed automatically for known CI environments")
Cmd.Flags().BoolVar(&opts.fixPermissions, "fix-permissions", false, "Fix codebase permissions. Performed automatically for known CI environments. WARNING: make sure you run wodby ci init from the project directory")
Cmd.Flags().StringVarP(&opts.buildNumber, "build-num", "n", "", "Custom build number (used if can't identify automatically)")
Cmd.Flags().StringVar(&opts.url, "url", "", "Custom build url (used if can't acquire automatically)")
Cmd.Flags().StringVar(&opts.provider, "provider", "p", "Custom build provider name (used if can't identify automatically)")
Expand Down
87 changes: 41 additions & 46 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,70 +72,65 @@ type BuildConfig struct {
}

type BuildMetadata struct {
Provider string `json,mapstructure:"provider"`
Username string `json,mapstructure:"username"`
Email string `json,mapstructure:"email"`
Number string `json,mapstructure:"number"`
URL string `json,mapstructure:"url"`
Branch string `json,mapstructure:"branch"`
Commit string `json,mapstructure:"commit"`
Message string `json,mapstructure:"message"`
Tag string `json,mapstructure:"tag"`
Slug string `json,mapstructure:"slug"`
RepoURL string `json,mapstructure:"repo_url"`
Id string `json,mapstructure:"id"`
ProjectDir string `json,mapstructure:"project_dir"`
Provider string `json,mapstructure:"provider"`
Username string `json,mapstructure:"username"`
Email string `json,mapstructure:"email"`
Number string `json,mapstructure:"number"`
URL string `json,mapstructure:"url"`
Branch string `json,mapstructure:"branch"`
Commit string `json,mapstructure:"commit"`
Message string `json,mapstructure:"message"`
Tag string `json,mapstructure:"tag"`
Slug string `json,mapstructure:"slug"`
RepoURL string `json,mapstructure:"repo_url"`
Id string `json,mapstructure:"id"`
}

func NewBuildMetadata(provider string, buildNumber string, url string) (*BuildMetadata, error) {
var metadata *BuildMetadata

if os.Getenv("TRAVIS") != "" {
metadata = &BuildMetadata{
Provider: TravisCI,
URL: url,
Id: os.Getenv("TRAVIS_BUILD_ID"),
Number: os.Getenv("TRAVIS_BUILD_NUMBER"),
Branch: os.Getenv("TRAVIS_BRANCH"),
Commit: os.Getenv("TRAVIS_COMMIT"),
Message: os.Getenv("TRAVIS_COMMIT_MESSAGE"),
Tag: os.Getenv("TRAVIS_TAG"),
Slug: os.Getenv("TRAVIS_REPO_SLUG"),
ProjectDir: os.Getenv("TRAVIS_REPO_SLUG"),
Provider: TravisCI,
URL: url,
Id: os.Getenv("TRAVIS_BUILD_ID"),
Number: os.Getenv("TRAVIS_BUILD_NUMBER"),
Branch: os.Getenv("TRAVIS_BRANCH"),
Commit: os.Getenv("TRAVIS_COMMIT"),
Message: os.Getenv("TRAVIS_COMMIT_MESSAGE"),
Tag: os.Getenv("TRAVIS_TAG"),
Slug: os.Getenv("TRAVIS_REPO_SLUG"),
}
} else if os.Getenv("CIRCLECI") != "" {
metadata = &BuildMetadata{
Provider: CircleCI,
URL: os.Getenv("CIRCLE_BUILD_URL"),
Number: os.Getenv("CIRCLE_BUILD_NUM"),
Branch: os.Getenv("CIRCLE_BRANCH"),
Commit: os.Getenv("CIRCLE_SHA1"),
Tag: os.Getenv("CIRCLE_TAG"),
Slug: os.Getenv("CIRCLE_PROJECT_REPONAME"),
RepoURL: os.Getenv("CIRCLE_REPOSITORY_URL"),
ProjectDir: os.Getenv("CIRCLE_WORKING_DIRECTORY"),
Provider: CircleCI,
URL: os.Getenv("CIRCLE_BUILD_URL"),
Number: os.Getenv("CIRCLE_BUILD_NUM"),
Branch: os.Getenv("CIRCLE_BRANCH"),
Commit: os.Getenv("CIRCLE_SHA1"),
Tag: os.Getenv("CIRCLE_TAG"),
Slug: os.Getenv("CIRCLE_PROJECT_REPONAME"),
RepoURL: os.Getenv("CIRCLE_REPOSITORY_URL"),
}

} else if os.Getenv("BITBUCKET_BUILD_NUMBER") != "" {
metadata = &BuildMetadata{
Provider: BitbucketPipelines,
Number: os.Getenv("BITBUCKET_BUILD_NUMBER"),
Branch: os.Getenv("BITBUCKET_BRANCH"),
Commit: os.Getenv("BITBUCKET_COMMIT"),
Tag: os.Getenv("BITBUCKET_TAG"),
Slug: os.Getenv("BITBUCKET_REPO_SLUG"),
ProjectDir: os.Getenv("BITBUCKET_CLONE_DIR"),
Provider: BitbucketPipelines,
Number: os.Getenv("BITBUCKET_BUILD_NUMBER"),
Branch: os.Getenv("BITBUCKET_BRANCH"),
Commit: os.Getenv("BITBUCKET_COMMIT"),
Tag: os.Getenv("BITBUCKET_TAG"),
Slug: os.Getenv("BITBUCKET_REPO_SLUG"),
}
// @todo acquire repo slug and git tag from Jenkins.
} else if os.Getenv("JENKINS_HOME") != "" {
metadata = &BuildMetadata{
Provider: Jenkins,
URL: os.Getenv("JOB_URL"),
Number: os.Getenv("BUILD_NUMBER"),
Branch: os.Getenv("GIT_BRANCH"),
Commit: os.Getenv("GIT_COMMIT"),
RepoURL: os.Getenv("GIT_URL"),
ProjectDir: os.Getenv("WORKSPACE"),
Provider: Jenkins,
URL: os.Getenv("JOB_URL"),
Number: os.Getenv("BUILD_NUMBER"),
Branch: os.Getenv("GIT_BRANCH"),
Commit: os.Getenv("GIT_COMMIT"),
RepoURL: os.Getenv("GIT_URL"),
}
} else {
metadata = &BuildMetadata{}
Expand Down

0 comments on commit 8767054

Please sign in to comment.