Skip to content

Load .env file during documentation build with a docker image #11

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

Merged
merged 2 commits into from
Jul 9, 2019
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
9 changes: 8 additions & 1 deletion core/core.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

import (
"fmt"
"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -225,8 +226,14 @@ func buildDocumentation(branches []string, versionsInfo types.VersionsInformatio
return err
}

args := []string{"run", "--rm", "-v", versionsInfo.CurrentPath + ":/mkdocs"}
if _, err = os.Stat(filepath.Join(versionsInfo.CurrentPath, ".env")); err == nil {
args = append(args, fmt.Sprintf("--env-file=%s", filepath.Join(versionsInfo.CurrentPath, ".env")))
}
args = append(args, dockerImageFullName, "mkdocs", "build")

// Run image
output, err := docker.Exec(config.Debug, "run", "--rm", "-v", versionsInfo.CurrentPath+":/mkdocs", dockerImageFullName, "mkdocs", "build")
output, err := docker.Exec(config.Debug, args...)
if err != nil {
log.Println(output)
return err
Expand Down
25 changes: 22 additions & 3 deletions manifest/manifest.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package manifest

import (
"fmt"
"io/ioutil"
"os"
"path"
Expand All @@ -12,8 +13,12 @@ import (
"gopkg.in/yaml.v2"
)

// FileName file name of the mkdocs manifest file.
const FileName = "mkdocs.yml"
const (
// FileName file name of the mkdocs manifest file.
FileName = "mkdocs.yml"
// TempPrefixEnvName temp prefix for environment variable.
TempPrefixEnvName = "STRUCTOR_TEMP_"
)

// Read Reads the manifest.
func Read(manifestFilePath string) (map[string]interface{}, error) {
Expand All @@ -38,7 +43,19 @@ func replaceEnvVariables(bytes []byte) []byte {
var re = regexp.MustCompile(`!!python\/object\/apply:os\.getenv\s\[[\'|\"]?([A-Z0-9_-]+)[\'|\"]?\]`)
result := re.FindAllStringSubmatch(data, -1)
for _, value := range result {
data = strings.ReplaceAll(data, value[0], os.Getenv(value[1]))
data = strings.ReplaceAll(data, value[0], TempPrefixEnvName+value[1])
}

return []byte(data)
}

func rewriteEnvVariables(bytes []byte) []byte {
data := string(bytes)
var re = regexp.MustCompile(TempPrefixEnvName + `([A-Z0-9_-]+)`)
result := re.FindAllStringSubmatch(data, -1)

for _, value := range result {
data = strings.ReplaceAll(data, value[0], fmt.Sprintf(`!!python/object/apply:os.getenv ["%s"]`, value[1]))
}

return []byte(data)
Expand All @@ -51,6 +68,8 @@ func Write(manifestFilePath string, manif map[string]interface{}) error {
return errors.Wrap(err, "error when marshal MkDocs Manifest.")
}

out = rewriteEnvVariables(out)

return ioutil.WriteFile(manifestFilePath, out, os.ModePerm)
}

Expand Down
5 changes: 1 addition & 4 deletions manifest/manifest_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package manifest

import (
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -38,7 +37,7 @@ func TestRead(t *testing.T) {
"site_url": "https://docs.traefik.io",
"copyright": "Copyright © 2016-2019 Containous",
"extra": map[interface{}]interface{}{
"traefikVersion": "powpow",
"traefikVersion": TempPrefixEnvName + "TRAEFIK_VERSION",
},
"theme": map[interface{}]interface{}{
"include_sidebar": true,
Expand Down Expand Up @@ -113,8 +112,6 @@ func TestRead(t *testing.T) {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

err := os.Setenv("TRAEFIK_VERSION", "powpow")
require.NoError(t, err)
content, err := Read(filepath.Join(".", "fixtures", test.filename))
require.NoError(t, err)

Expand Down