github.com/GoogleContainerTools/skaffold/v2@v2.13.2/examples/jib-sync/README.md (about)

     1  ### Example: Jib Sync
     2  
     3  [![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https://github.com/GoogleContainerTools/skaffold&cloudshell_open_in_editor=README.md&cloudshell_workspace=examples/jib-sync)
     4  
     5  [Jib](https://github.com/GoogleContainerTools/jib) is one of the supported builders in Skaffold. Jib
     6  has special sync support using the `auto` configuration.
     7  
     8  ## Running the example
     9  
    10  Run the maven or gradle version of the example.
    11  
    12  #### Gradle
    13  ```
    14  $ skaffold dev -f skaffold-gradle.yaml
    15  ```
    16  
    17  #### Maven
    18  ```
    19  $ skaffold dev -f skaffold-maven.yaml
    20  ```
    21  
    22  You can now see sync in action:
    23  1. See the original response from the `HelloController` in the spring application 
    24    ```
    25    $ curl localhost:8080
    26    text-to-replace
    27    ```
    28  1. Edit the hello controller `src/main/java/hello/HelloController.java`
    29    ```diff
    30    +       return "some-new-text\n";
    31    -       return "text-to-replace\n";
    32    ```
    33  1. Give skaffold a few seconds to synchronize the file to the container, and give Spring
    34     Boot Developer Tools a chance to reload your application.
    35  1. See the updated response from the `HelloController`
    36    ```
    37    $ curl localhost:8080
    38    some-new-text
    39    ```
    40  1. You've now seen auto sync in action!
    41  
    42  ## How it works
    43  
    44  This example contains both maven and gradle build configs and separate skaffold.yamls.
    45  
    46  - **gradle**: `skaffold-gradle.yaml`
    47  - **maven**: `skaffold-maven.yaml`
    48  
    49  use the `-f` flag to specify the correct buildfile when running (or rename your preferred option to `skaffold.yaml`)
    50  ```
    51  $ skaffold -f skaffold-gradle.yaml ...
    52  ```
    53  
    54  We configure it in `skaffold.yaml`, by enabling sync on the jib artifact.
    55  
    56  ```yaml
    57  build:
    58    artifacts:
    59    - image: skaffold-example
    60      context: .
    61      jib: {}
    62      sync: 
    63        auto: true
    64  ```
    65  
    66  This example is designed around the functionality available in [Spring Boot Developer Tools](https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-devtools) for developing against running applications.
    67  
    68  Some additional steps are required for this to work:
    69  - Sync requires `tar` on the running container to copy files over. The default base image that Jib uses `gcr.io/distroless/java` does not include `tar` or any utilities. During development, you must use a base image that includes `tar`, in this example we use the `debug` flavor of distroless: `gcr.io/distroless/java:debug` 
    70  
    71  This can be done directly in the artifact configuration by overriding the `fromImage` property.
    72  
    73  ```yaml
    74  build:
    75    artifacts:
    76    - image: skaffold-example
    77      context: .
    78      jib: 
    79        fromImage: gcr.io/distroless/java:debug
    80      sync: 
    81        auto: true
    82  ```
    83  
    84  
    85  - You must include the `spring-boot-devtools` dependency at the `compile/implementation` scope, which is contrary to the configuration outlined in the [official docs](https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-devtools). Because jib is unaware of any special spring only configuration in your builds, we recommend using profiles to turn on or off devtools support in your jib container builds.
    86  
    87  `maven`
    88  ```xml
    89  <profiles>
    90    <profile>
    91      <id>sync</id>
    92      <dependencies>
    93        <dependency>
    94          <groupId>org.springframework.boot</groupId>
    95          <artifactId>spring-boot-devtools</artifactId>
    96          <!-- <optional>true</optional> not required -->
    97        </dependency>
    98      </dependencies>
    99    </profile>
   100  </profiles>
   101  ```
   102  
   103  `gradle`
   104  ```groovy
   105  dependencies {
   106    ...
   107    if (project.hasProperty('sync')) {
   108      implementation "org.springframework.boot:spring-boot-devtools"
   109      // do not use developmentOnly
   110    }
   111  }
   112  ```
   113  
   114  To activate these profiles, we add `-Psync` to the maven/gradle build command. This can be done directly in the artifact configuration
   115  
   116  `skaffold.yaml`
   117  ```
   118  build:
   119    artifacts:
   120    - image: skaffold-example
   121      context: .
   122      jib: 
   123        args: 
   124        - -Psync
   125      sync: 
   126        auto: true
   127  ```
   128  
   129  You can also take advantage of [skaffold profiles](https://skaffold.dev/docs/environment/profiles/) to control when to activate sync on your project.
   130  
   131  `skaffold.yaml`
   132  ```
   133  build:
   134    artifacts:
   135    - image: test-file-sync
   136      jib: {}
   137  
   138  profiles:
   139  - name: sync
   140    patches:
   141      # assuming jib is the first artifact (index 0) in your build.artifacts list
   142    - op: add
   143      # we want to activate sync on our skaffold artifact
   144      path: /build/artifacts/0/sync
   145      value:
   146        - auto: true
   147    - op: add
   148      # we activate the sync profile in our java builds
   149      path: /build/artifacts/0/jib/args
   150      value:
   151      - -Psync
   152  ```
   153  
   154  skaffold profiles can be activated using the the `-p` flag when running
   155  
   156  ```
   157  $ skaffold -p sync ...
   158  ```