Skip to content

Commit

Permalink
revert docker build context flag and command line input to generate w…
Browse files Browse the repository at this point in the history
…orkflow (#225)
  • Loading branch information
davidgamero authored Aug 7, 2023
1 parent 5602b8a commit 1357c89
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 37 deletions.
1 change: 1 addition & 0 deletions cmd/generate-workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func newGenerateWorkflowCmd() *cobra.Command {
Long: `This command will generate a Github workflow to build and deploy an application containerized
with draft on AKS. This command assumes the 'setup-gh' command has been run properly.`,
RunE: func(cmd *cobra.Command, args []string) error {
flagValuesMap = make(map[string]string)
if cmd.Flags().NFlag() != 0 {
flagValuesMap = gwCmd.workflowConfig.SetFlagValuesToMap()
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/workflows/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
"path"
"strings"

"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/client-go/kubernetes/scheme"

"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"

"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/embedutils"
"github.com/Azure/draft/pkg/osutil"
Expand All @@ -39,6 +40,9 @@ type Workflows struct {
}

func CreateWorkflows(dest string, deployType string, flagVariables []string, templateWriter templatewriter.TemplateWriter, flagValuesMap map[string]string) error {
if flagValuesMap == nil {
return fmt.Errorf("flagValuesMap is nil")
}
var err error
for _, flagVar := range flagVariables {
flagVarName, flagVarValue, ok := strings.Cut(flagVar, "=")
Expand Down Expand Up @@ -73,7 +77,7 @@ func CreateWorkflows(dest string, deployType string, flagVariables []string, tem

maps.Copy(customInputs, flagValuesMap)

if err = updateProductionDeployments(deployType, dest, flagValuesMap, templateWriter); err != nil {
if err = updateProductionDeployments(deployType, dest, customInputs, templateWriter); err != nil {
return err
}
return workflow.createWorkflowFiles(deployType, customInputs, templateWriter)
Expand Down
115 changes: 89 additions & 26 deletions pkg/workflows/workflows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"testing"
"testing/fstest"

"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/client-go/kubernetes/scheme"

"github.com/stretchr/testify/assert"

"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/embedutils"
"github.com/Azure/draft/pkg/templatewriter/writers"
Expand All @@ -28,34 +29,96 @@ func TestCreateWorkflows(t *testing.T) {
flagValuesMap := map[string]string{"AZURECONTAINERREGISTRY": "testAcr", "CONTAINERNAME": "testContainer", "RESOURCEGROUP": "testRG", "CLUSTERNAME": "testCluster", "BRANCHNAME": "testBranch", "BUILDCONTEXTPATH": "."}
flagValuesMapNoRoot := map[string]string{"AZURECONTAINERREGISTRY": "testAcr", "CONTAINERNAME": "testContainer", "RESOURCEGROUP": "testRG", "CLUSTERNAME": "testCluster", "BRANCHNAME": "testBranch", "BUILDCONTEXTPATH": "test"}

err := createTempDeploymentFile("charts", "charts/production.yaml", "../../test/templates/helm/charts/production.yaml")
assert.Nil(t, err)
assert.Nil(t, CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMap))
assert.Nil(t, CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMapNoRoot))
os.RemoveAll("charts")
os.RemoveAll(".github")
tests := []struct {
name string
deployType string
flagValues map[string]string
shouldError bool
tempDirPath string
tempFileName string
tempPath string
cleanUp func()
}{
{
name: "helm",
deployType: "helm",
flagValues: flagValuesMap,
shouldError: false,
tempDirPath: "charts",
tempFileName: "charts/production.yaml",
tempPath: "../../test/templates/helm/charts/production.yaml",
cleanUp: func() {
os.Remove(".charts")
os.Remove(".github")
},
},
{
name: "kustomize",
deployType: "kustomize",
flagValues: flagValuesMap,
shouldError: false,
tempDirPath: "overlays/production",
tempFileName: "overlays/production/deployment.yaml",
tempPath: "../../test/templates/kustomize/overlays/production/deployment.yaml",
cleanUp: func() {
os.Remove(".overlays")
os.Remove(".github")
},
}, {
name: "manifests",
deployType: "manifests",
flagValues: flagValuesMap,
shouldError: false,
tempDirPath: "manifests",
tempFileName: "manifests/deployment.yaml",
tempPath: "../../test/templates/manifests/manifests/deployment.yaml",
cleanUp: func() {
os.Remove(".manifests")
os.Remove(".github")
},
},
{
name: "missing manifest",
deployType: "manifests",
flagValues: flagValuesMap,
shouldError: true,
tempDirPath: "manifests",
tempFileName: "manifests/deployment.yaml",
tempPath: "../../test/templates/manifests/manifests/deployment.yaml",
cleanUp: func() {
os.Remove(".manifests")
os.Remove(".github")
},
},
{
name: "invalid deploy type",
deployType: "invalid",
flagValues: flagValuesMap,
shouldError: true,
tempDirPath: "manifests",
tempFileName: "manifests/deployment.yaml",
tempPath: "../../test/templates/manifests/manifests/deployment.yaml",
cleanUp: func() {
},
},
}

deployType = "kustomize"
err = createTempDeploymentFile("overlays/production", "overlays/production/deployment.yaml", "../../test/templates/kustomize/overlays/production/deployment.yaml")
assert.Nil(t, err)
assert.Nil(t, CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMap))
assert.Nil(t, CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMapNoRoot))
os.RemoveAll("overlays")
os.RemoveAll(".github")
for _, tt := range tests {

deployType = "manifests"
err = createTempDeploymentFile("manifests", "manifests/deployment.yaml", "../../test/templates/manifests/manifests/deployment.yaml")
assert.Nil(t, err)
assert.Nil(t, CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMap))
assert.Nil(t, CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMapNoRoot))
os.RemoveAll("manifests")
os.RemoveAll(".github")
//test for missing deployment file path
assert.NotNil(t, CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMap))
err := createTempDeploymentFile("charts", "charts/production.yaml", "../../test/templates/helm/charts/production.yaml")
assert.Nil(t, err)

//test for invalid deployType
deployType = "testInvalidDeployType"
assert.NotNil(t, CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMap))
err = CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMap)
if err != nil {
t.Errorf("Default Build Context CreateWorkflows() error = %v, wantErr %v", err, tt.shouldError)
}
err = CreateWorkflows(dest, deployType, flagVariables, templatewriter, flagValuesMapNoRoot)
if err != nil {
t.Errorf("Custom Build Context CreateWorkflows() error = %v, wantErr %v", err, tt.shouldError)
}

tt.cleanUp()
}
}

func TestUpdateProductionDeploymentsValid(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
# Builds and pushes an image up to your Azure Container Registry
- name: Build and push image to ACR
run: |
az acr build --image ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }} --registry ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} -x ${{ env.BUILD_CONTEXT_PATH }}
az acr build --image ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }} --registry ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} .
deploy:
permissions:
actions: read
Expand Down Expand Up @@ -123,4 +123,4 @@ jobs:
action: deploy
manifests: ${{ steps.bake.outputs.manifestsBundle }}
images: |
${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }}
${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:
# Builds and pushes an image up to your Azure Container Registry
- name: Build and push image to ACR
run: |
az acr build --image ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }} --registry ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} -x ${{ env.BUILD_CONTEXT_PATH }}
az acr build --image ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }} --registry ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} .
deploy:
permissions:
actions: read
Expand Down Expand Up @@ -118,4 +118,4 @@ jobs:
action: deploy
manifests: ${{ steps.bake.outputs.manifestsBundle }}
images: |
${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }}
${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }}
2 changes: 1 addition & 1 deletion template/workflows/kustomize/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ variableDefaults:
value: "./overlays/production"
disablePrompt: true
- name: "BUILDCONTEXTPATH"
value: "."
value: "."
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
# Builds and pushes an image up to your Azure Container Registry
- name: Build and push image to ACR
run: |
az acr build --image ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }} --registry ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} -x ${{ env.BUILD_CONTEXT_PATH }}
az acr build --image ${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }} --registry ${{ env.AZURE_CONTAINER_REGISTRY }} -g ${{ env.RESOURCE_GROUP }} .
deploy:
permissions:
actions: read
Expand Down Expand Up @@ -105,4 +105,4 @@ jobs:
action: deploy
manifests: ${{ env.DEPLOYMENT_MANIFEST_PATH }}
images: |
${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }}
${{ env.AZURE_CONTAINER_REGISTRY }}.azurecr.io/${{ env.CONTAINER_NAME }}:${{ github.sha }}
2 changes: 1 addition & 1 deletion template/workflows/manifests/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ variableDefaults:
value: "./manifests"
disablePrompt: true
- name: "BUILDCONTEXTPATH"
value: "."
value: "."

0 comments on commit 1357c89

Please sign in to comment.