diff --git a/cloud/deploy/deploy.go b/cloud/deploy/deploy.go index e9cc0b0c2..3537c98b8 100644 --- a/cloud/deploy/deploy.go +++ b/cloud/deploy/deploy.go @@ -103,6 +103,39 @@ func getRegistryURL(domain string) string { return registry } +func removeDagsFromDockerIgnore(fullpath string) error { + f, err := os.Open(fullpath) + if err != nil { + return err + } + + defer f.Close() + + var bs []byte + buf := bytes.NewBuffer(bs) + + scanner := bufio.NewScanner(f) + for scanner.Scan() { + text := scanner.Text() + if text != "dags/" { + _, err = buf.WriteString(text + "\n") + if err != nil { + return err + } + } + } + + if err := scanner.Err(); err != nil { + return err + } + err = os.WriteFile(fullpath, bytes.Trim(buf.Bytes(), "\n"), 0o666) //nolint:gosec, gomnd + if err != nil { + return err + } + + return nil +} + func deployDags(path, dagsPath, runtimeID string, client astro.Client) (string, error) { // Check the dags directory monitoringDagPath := filepath.Join(dagsPath, "astronomer_monitoring_dag.py") @@ -258,6 +291,11 @@ func Deploy(deployInput InputDeploy, client astro.Client) error { //nolint fmt.Sprintf("\n Deployment View: %s", ansi.Bold(deploymentURL)) + fmt.Sprintf("\n Airflow UI: %s", ansi.Bold(deployInfo.webserverURL))) } else { + fullpath := filepath.Join(deployInput.Path, ".dockerignore") + err := removeDagsFromDockerIgnore(fullpath) + if err != nil { + return errors.New("Found dags entry in .dockerignore file. Remove this entry and try again") + } envFileExists, _ := fileutil.Exists(deployInput.EnvFile, nil) if !envFileExists && deployInput.EnvFile != ".env" { return fmt.Errorf("%w %s", envFileMissing, deployInput.EnvFile) @@ -477,6 +515,17 @@ func buildImageWithoutDags(path string, imageHandler airflow.ImageHandler) error dockerIgnoreCreate := false fullpath := filepath.Join(path, ".dockerignore") + defer func() { + // remove dags from .dockerignore file if we set it + if dagsIgnoreSet { + removeDagsFromDockerIgnore(fullpath) //nolint:errcheck + } + // remove created docker ignore file + if dockerIgnoreCreate { + os.Remove(fullpath) + } + }() + fileExist, _ := fileutil.Exists(fullpath, nil) if !fileExist { // Create a dockerignore file and add the dags folder entry @@ -510,40 +559,9 @@ func buildImageWithoutDags(path string, imageHandler airflow.ImageHandler) error return err } - defer func() { - // remove created docker ignore file - if dockerIgnoreCreate { - os.Remove(fullpath) - } - }() - // remove dags from .dockerignore file if we set it if dagsIgnoreSet { - f, err := os.Open(fullpath) - if err != nil { - return err - } - - defer f.Close() - - var bs []byte - buf := bytes.NewBuffer(bs) - - scanner := bufio.NewScanner(f) - for scanner.Scan() { - text := scanner.Text() - if text != "dags/" { - _, err = buf.WriteString(text + "\n") - if err != nil { - return err - } - } - } - - if err := scanner.Err(); err != nil { - return err - } - err = os.WriteFile(fullpath, bytes.Trim(buf.Bytes(), "\n"), 0o666) //nolint:gosec, gomnd + err = removeDagsFromDockerIgnore(fullpath) if err != nil { return err } diff --git a/cloud/deploy/deploy_test.go b/cloud/deploy/deploy_test.go index 60d2d9d64..242e62721 100644 --- a/cloud/deploy/deploy_test.go +++ b/cloud/deploy/deploy_test.go @@ -258,6 +258,8 @@ func TestDeployWithDagsDeploySuccess(t *testing.T) { os.Mkdir("./testfiles1/", os.ModePerm) fileutil.WriteStringToFile("./testfiles1/Dockerfile", "FROM quay.io/astronomer/astro-runtime:4.2.5") fileutil.WriteStringToFile("./testfiles1/.env", "") + fileutil.WriteStringToFile("./testfiles1/.dockerignore", "") + deployInput = InputDeploy{ Path: "./testfiles1/", RuntimeID: "",