github.com/GoogleContainerTools/skaffold/v2@v2.13.2/docs-v2/content/en/docs/init.md (about)

     1  ---
     2  title: "Init"
     3  linkTitle: "Init"
     4  weight: 41
     5  featureId: init
     6  aliases: [/docs/pipeline-stages/init]
     7  ---
     8  
     9  `skaffold init` helps you get started using Skaffold by running you through a wizard and
    10  generating the required `skaffold.yaml` file in the root of your project directory.
    11  
    12  The generated `skaffold.yaml` defines your [build](#build-config-initialization)
    13  and [deploy](#deploy-config-initialization) config.
    14  
    15  ## Build Config Initialization
    16  
    17  `skaffold init` currently supports build detection for those builders:
    18  
    19  1. [Docker]({{<relref "/docs/builders/builder-types/docker">}})
    20  2. [Jib]({{<relref "/docs/builders/builder-types/jib">}})
    21  3. [Ko]({{<relref "/docs/builders/builder-types/ko">}})
    22  4. [Buildpacks]({{<relref "/docs/builders/builder-types/buildpacks">}})
    23  
    24  `skaffold init` walks your project directory and looks for any build configuration files such as `Dockerfile`,
    25  `build.gradle/pom.xml`, `package.json`, `requirements.txt` or `go.mod`. `init` skips files that are larger
    26  than 500MB.
    27  
    28  If there are multiple build configuration files, Skaffold will prompt you to pair your build configuration files
    29  with any images detected in your deploy configuration.
    30  
    31  E.g. For an application with [two microservices](https://github.com/GoogleContainerTools/skaffold/tree/main/examples/microservices):
    32  
    33  ```bash
    34  skaffold init
    35  ```
    36  ![microservices](/images/microservices-init-flow.png)
    37  
    38  
    39  {{< alert title="Note" >}}
    40  You can choose <code>None (image not built from these sources)</code> if none of the suggested
    41  options are correct, or this image is not built by any of your source code.<br>
    42  If this image is one you want Skaffold to build, you'll need to manually set up the build configuration for this artifact.
    43  {{</alert>}}
    44  
    45  `skaffold` init also recognizes Maven and Gradle projects, and will auto-suggest the [`jib`]({{<relref "/docs/builders#/local#jib-maven-and-gradle">}}) builder.
    46  
    47  You can try this out on our example [jib project](https://github.com/GoogleContainerTools/skaffold/tree/main/examples/jib-multimodule)
    48  
    49  ```bash
    50  skaffold init
    51  ```
    52  
    53  ![jib-multimodule](/images/jib-multimodule-init-flow.png)
    54  
    55  
    56  ## Deploy Config Initialization
    57  `skaffold init` support bootstrapping projects set up to deploy with [`kubectl`]({{<relref "/docs/deployers#deploying-with-kubectl" >}})
    58  or [`kustomize`]({{<relref "/docs/deployers#deploying-with-kubectl" >}}).
    59  
    60  ### kubectl
    61  For projects deploying straight through `kubectl`, Skaffold will walk through all the `yaml` files in your project and find valid Kubernetes manifest files.
    62  
    63  These files will be added to `deploy` config in `skaffold.yaml`.
    64  
    65  ```yaml
    66  deploy:
    67    kubectl:
    68      manifests:
    69      - leeroy-app/kubernetes/deployment.yaml
    70      - leeroy-web/kubernetes/deployment.yaml
    71  ```
    72  
    73  ### kustomize
    74  For projects deploying with `kustomize`, Skaffold will scan your project and look for `kustomization.yaml`s as well as Kubernetes manifests.
    75  It will attempt to infer the project structure based on the recommended project structure from the Kustomize project: thus,
    76  **it is highly recommended to match your project structure to the recommended base/ and overlay/ structure from Kustomize!**
    77  
    78  This generally looks like this:
    79  
    80  ```yaml
    81  app/      # application source code, along with build configuration
    82    main.go
    83    Dockerfile
    84  ...
    85  base/     # base deploy configuration
    86    kustomization.yaml
    87    deployment.yaml
    88  overlays/ # one or more nested directories, each with modified environment configuration
    89    dev/
    90      deployment.yaml
    91      kustomization.yaml
    92    prod/
    93  ...
    94  ```
    95  
    96  When overlay directories are found, these will be listed in the generated Skaffold config as `paths` in the `kustomize` deploy stanza. However, it generally does not make sense to have multiple overlays applied at the same time, so **Skaffold will attempt to choose a default overlay, and put each other overlay into its own profile**. This can be specified by the user through the flag `--default-kustomization`; otherwise, Skaffold will use the following heuristic:
    97  
    98  1) Any overlay with the name `dev`
    99  2) If none present, the **first** overlay that isn't named `prod`
   100  
   101  *Note: order is guaranteed, since Skaffold's directory parsing is always deterministic.*
   102  
   103  ## `--generate-manifests` Flag
   104  {{< maturity "init.generate_manifests" >}}
   105  `skaffold init` allows for use of a `--generate-manifests` flag, which will try to generate basic kubernetes manifests for a user's project to help get things up and running.
   106  
   107  If bringing a project to skaffold that has no kubernetes manifests yet, it may be helpful to run `skaffold init` with this flag.
   108  
   109  
   110  ## `--force` Flag
   111  `skaffold init` allows for use of a `--force` flag, which removes the prompts from vanilla `skaffold init`, and allows skaffold to make a best effort attempt to automatically generate a config for your project.
   112  
   113  In a situation where one image is detected, but multiple possible builders are detected, skaffold will choose a builder as follows: Docker > Jib > Ko > Bazel > Buildpacks.
   114  
   115  *Note: This feature is still under development, and doesn't currently support use cases such as multiple images in a project.*
   116  
   117  ## Init API
   118  `skaffold init` also exposes an API which tools like IDEs can integrate with via flags.
   119  
   120  This API can be used to
   121  
   122  1. Analyze a project workspace and discover all build definitions (e.g. `Dockerfile`s) and artifacts (image names from the Kubernetes manifests) - this then provides an ability for tools to ask the user to pair the artifacts with Dockerfiles interactively.
   123  2. Given a pairing between the image names (artifacts) and build definitions (e.g. Dockerfiles), generate Skaffold `build` config for a given artifact.
   124  
   125  The resulting `skaffold.yaml` will look something like this:
   126  
   127  ```yaml
   128  apiVersion: skaffold/v2beta5
   129  ...
   130  deploy:
   131    kustomize:
   132      paths:
   133      - overlays/dev
   134  profiles:
   135  - name: prod
   136    deploy:
   137      kustomize:
   138        paths:
   139        - overlays/prod
   140  ```
   141  
   142  **Init API contract**
   143  
   144  | API | flag | input/output |
   145  | ---- | --- | --- |
   146  | Analyze | `--analyze` | json encoded output of builders and images|
   147  | Generate | `--artifact`| "`=` delimited" build definition/image pair (for example: `=path1/Dockerfile=artifact1`) or <br>JSON string (for example: `{"builder":"Docker","payload":{"path":"Dockerfile"},"image":"artifact")`|
   148  
   149  
   150  ### Analyze API
   151  Analyze API walks through all files in your project workspace and looks for
   152  `Dockerfile` files.
   153  
   154  To get all image names and dockerfiles, run
   155  ```bash
   156  skaffold init --analyze | jq
   157  ```
   158  will give you a json output
   159  ```json
   160  {
   161    "dockerfiles": [
   162      "leeroy-app/Dockerfile",
   163      "leeroy-web/Dockerfile"
   164    ],
   165    "images": [
   166      "gcr.io/k8s-skaffold/leeroy-app",
   167      "gcr.io/k8s-skaffold/leeroy-web"
   168    ]
   169  }
   170  ```
   171  
   172  ### Generate API
   173  To generate a skaffold `build` config, use the `--artifact` flag per artifact.
   174  
   175  For multiple artifacts, use `--artifact` multiple times.
   176  
   177  ```bash
   178  skaffold init \
   179    -a '{"builder":"Docker","payload":{"path":"leeroy-app/Dockerfile"},"image":"gcr.io/k8s-skaffold/leeroy-app"}' \
   180    -a '{"builder":"Docker","payload":{"path":"leeroy-web/Dockerfile"},"image":"gcr.io/k8s-skaffold/leeroy-web","context":"path/to/context"}'
   181  ```
   182  
   183  will produce an `skaffold.yaml` config like this
   184  
   185  {{% readfile file="samples/pipeline-stages/init-example.yaml" %}}
   186  
   187  ### Exit Codes
   188  
   189  When `skaffold init` fails, it exits with an code that depends on the error:
   190  
   191  | Exit Code | Error |
   192  | ---- | --- |
   193  | 101 | No build configuration could be found |
   194  | 102 | No k8s manifest could be found or generated |
   195  | 103 | An existing skaffold.yaml was found |
   196  | 104 | Couldn't match builder with image names automatically |