Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 818b139

Browse files
authored
feat: add support for no push environment variable (#2983)
* feat: add support for no push environment variable * Update to Readme * feat: add support for no push environment variable Update to Readme
1 parent 9651084 commit 818b139

2 files changed

Lines changed: 106 additions & 82 deletions

File tree

README.md

Lines changed: 96 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ specify the location of your build context:
231231
| Source | Prefix | Example |
232232
| ------------------ | --------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
233233
| Local Directory | dir://[path to a directory in the kaniko container] | `dir:///workspace` |
234-
| Local Tar Gz | tar://[path to a .tar.gz in the kaniko container] | `tar:///path/to/context.tar.gz` |
234+
| Local Tar Gz | tar://[path to a .tar.gz in the kaniko container] | `tar:///path/to/context.tar.gz` |
235235
| Standard Input | tar://[stdin] | `tar://stdin` |
236236
| GCS Bucket | gs://[bucket name]/[path to .tar.gz] | `gs://kaniko-bucket/path/to/context.tar.gz` |
237237
| S3 Bucket | s3://[bucket name]/[path to .tar.gz] | `s3://kaniko-bucket/path/to/context.tar.gz` |
@@ -516,8 +516,8 @@ docker run -v $(pwd):/workspace gcr.io/kaniko-project/warmer:latest --cache-dir=
516516
docker run -v $(pwd):/workspace gcr.io/kaniko-project/warmer:latest --cache-dir=/workspace/cache --dockerfile=<path to dockerfile> --build-arg version=1.19
517517
```
518518

519-
`--image` can be specified for any number of desired images. `--dockerfile` can
520-
be specified for the path of dockerfile for cache.These command will combined to
519+
`--image` can be specified for any number of desired images. `--dockerfile` can
520+
be specified for the path of dockerfile for cache.These command will combined to
521521
cache those images by digest in a local directory named `cache`. Once the cache
522522
is populated, caching is opted into with the same `--cache=true` flag as above.
523523
The location of the local cache is provided via the `--cache-dir` flag,
@@ -795,9 +795,9 @@ and
795795
This flag allows you to pass in ARG values at build time, similarly to Docker.
796796
You can set it multiple times for multiple arguments.
797797

798-
Note that passing values that contain spaces is not natively supported - you need
799-
to ensure that the IFS is set to null before your executor command. You can set
800-
this by adding `export IFS=''` before your executor call. See the following
798+
Note that passing values that contain spaces is not natively supported - you
799+
need to ensure that the IFS is set to null before your executor command. You can
800+
set this by adding `export IFS=''` before your executor call. See the following
801801
example
802802

803803
```bash
@@ -945,7 +945,8 @@ Set this flag as `--log-timestamp=<true|false>` to add timestamps to
945945
#### Flag `--no-push`
946946

947947
Set this flag if you only want to build the image, without pushing to a
948-
registry.
948+
registry. This can also be defined through `KANIKO_NO_PUSH` environment
949+
variable.
949950

950951
#### Flag `--oci-layout-path`
951952

@@ -980,19 +981,24 @@ communication with a given
980981
[registry that requires mTLS](https://docs.docker.com/engine/security/certificates/)
981982
for authentication.
982983

983-
Expected format is `my.registry.url=/path/to/client/cert.crt,/path/to/client/key.key`
984+
Expected format is
985+
`my.registry.url=/path/to/client/cert.crt,/path/to/client/key.key`
984986

985987
#### Flag `--registry-map`
986988

987-
Set this flag if you want to remap registries references. Usefull for air gap environement for example.
988-
You can use this flag more than once, if you want to set multiple mirrors for a given registry.
989-
You can mention several remap in a single flag too, separated by semi-colon.
990-
If an image is not found on the first mirror, Kaniko will try
991-
the next mirror(s), and at the end fallback on the original registry.
989+
Set this flag if you want to remap registries references. Usefull for air gap
990+
environement for example. You can use this flag more than once, if you want to
991+
set multiple mirrors for a given registry. You can mention several remap in a
992+
single flag too, separated by semi-colon. If an image is not found on the first
993+
mirror, Kaniko will try the next mirror(s), and at the end fallback on the
994+
original registry.
992995

993-
Registry maps can also be defined through `KANIKO_REGISTRY_MAP` environment variable.
996+
Registry maps can also be defined through `KANIKO_REGISTRY_MAP` environment
997+
variable.
994998

995-
Expected format is `original-registry=remapped-registry[;another-reg=another-remap[;...]]` for example.
999+
Expected format is
1000+
`original-registry=remapped-registry[;another-reg=another-remap[;...]]` for
1001+
example.
9961002

9971003
Note that you can't specify a URL with scheme for this flag. Some valid options
9981004
are:
@@ -1001,7 +1007,8 @@ are:
10011007
- `gcr.io=127.0.0.1`
10021008
- `quay.io=192.168.0.1:5000`
10031009
- `index.docker.io=docker-io.mirrors.corp.net;index.docker.io=mirror.gcr.io;gcr.io=127.0.0.1`
1004-
will try `docker-io.mirrors.corp.net` then `mirror.gcr.io` for `index.docker.io` and `127.0.0.1` for `gcr.io`
1010+
will try `docker-io.mirrors.corp.net` then `mirror.gcr.io` for
1011+
`index.docker.io` and `127.0.0.1` for `gcr.io`
10051012

10061013
#### Flag `--registry-mirror`
10071014

@@ -1010,7 +1017,8 @@ Set this flag if you want to use a registry mirror instead of the default
10101017
multiple mirrors. If an image is not found on the first mirror, Kaniko will try
10111018
the next mirror(s), and at the end fallback on the default registry.
10121019

1013-
Mirror can also be defined through `KANIKO_REGISTRY_MIRROR` environment variable.
1020+
Mirror can also be defined through `KANIKO_REGISTRY_MIRROR` environment
1021+
variable.
10141022

10151023
Expected format is `mirror.gcr.io` for example.
10161024

@@ -1024,9 +1032,13 @@ are:
10241032

10251033
#### Flag `--skip-default-registry-fallback`
10261034

1027-
Set this flag if you want the build process to fail if none of the mirrors listed in flag [registry-mirror](#flag---registry-mirror) can pull some image. This should be used with mirrors that implements a whitelist or some image restrictions.
1035+
Set this flag if you want the build process to fail if none of the mirrors
1036+
listed in flag [registry-mirror](#flag---registry-mirror) can pull some image.
1037+
This should be used with mirrors that implements a whitelist or some image
1038+
restrictions.
10281039

1029-
If [registry-mirror](#flag---registry-mirror) is not set or is empty, this flag is ignored.
1040+
If [registry-mirror](#flag---registry-mirror) is not set or is empty, this flag
1041+
is ignored.
10301042

10311043
#### Flag `--reproducible`
10321044

@@ -1040,8 +1052,8 @@ only one layer will be appended to the base image.
10401052

10411053
#### Flag `--skip-push-permission-check`
10421054

1043-
Set this flag to skip push permission check. This can be useful to delay Kanikos first request for delayed
1044-
network-policies.
1055+
Set this flag to skip push permission check. This can be useful to delay Kanikos
1056+
first request for delayed network-policies.
10451057

10461058
#### Flag `--skip-tls-verify`
10471059

@@ -1065,8 +1077,8 @@ multiple times for multiple registries.
10651077
#### Flag `--skip-unused-stages`
10661078

10671079
This flag builds only used stages if defined to `true`. Otherwise it builds by
1068-
default all stages, even the unnecessary ones until it reaches the target
1069-
stage / end of Dockerfile
1080+
default all stages, even the unnecessary ones until it reaches the target stage
1081+
/ end of Dockerfile
10701082

10711083
#### Flag `--snapshot-mode`
10721084

@@ -1077,8 +1089,8 @@ kaniko will snapshot the filesystem.
10771089
considered when snapshotting. This is the least performant option, but also
10781090
the most robust.
10791091

1080-
- If `--snapshot-mode=redo` is set, the file mtime, size, mode, owner uid and gid
1081-
will be considered when snapshotting. This may be up to 50% faster than
1092+
- If `--snapshot-mode=redo` is set, the file mtime, size, mode, owner uid and
1093+
gid will be considered when snapshotting. This may be up to 50% faster than
10821094
"full", particularly if your project has a large number files.
10831095

10841096
- If `--snapshot-mode=time` is set, only file mtime will be considered when
@@ -1096,11 +1108,15 @@ Set this flag to indicate which build stage is the target build stage.
10961108

10971109
#### Flag `--use-new-run`
10981110

1099-
Using this flag enables an experimental implementation of the Run command which does not rely on snapshotting at all.
1100-
In this approach, in order to compute which files were changed, a marker file is created before executing the Run command.
1101-
Then the entire filesystem is walked (takes ~1-3 seconds for 700Kfiles) to find all files whose ModTime is greater than the marker file.
1102-
With this new run command implementation, the total build time is reduced seeing performance improvements in the range of ~75%. This new run mode trades
1103-
off accuracy/correctness in some cases (potential for missed files in a "snapshot") for improved performance by avoiding the full filesystem snapshots.
1111+
Using this flag enables an experimental implementation of the Run command which
1112+
does not rely on snapshotting at all. In this approach, in order to compute
1113+
which files were changed, a marker file is created before executing the Run
1114+
command. Then the entire filesystem is walked (takes ~1-3 seconds for 700Kfiles)
1115+
to find all files whose ModTime is greater than the marker file. With this new
1116+
run command implementation, the total build time is reduced seeing performance
1117+
improvements in the range of ~75%. This new run mode trades off
1118+
accuracy/correctness in some cases (potential for missed files in a "snapshot")
1119+
for improved performance by avoiding the full filesystem snapshots.
11041120

11051121
#### Flag `--verbosity`
11061122

@@ -1124,8 +1140,8 @@ image filesystem. Defaults to `0`.
11241140

11251141
#### Flag `--image-download-retry`
11261142

1127-
Set this flag to the number of retries that should happen when downloading the
1128-
remote image. Consecutive retries occur with exponential backoff and an initial
1143+
Set this flag to the number of retries that should happen when downloading the
1144+
remote image. Consecutive retries occur with exponential backoff and an initial
11291145
delay of 1 second. Defaults to 0`.
11301146

11311147
### Debug Image
@@ -1199,18 +1215,19 @@ profiling,
11991215
## Creating Multi-arch Container Manifests Using Kaniko and Manifest-tool
12001216

12011217
While Kaniko itself currently does not support creating multi-arch manifests
1202-
(contributions welcome), one can use tools such as [manifest-tool](https://github.com/estesp/manifest-tool)
1203-
to stitch multiple separate builds together into a single container manifest.
1218+
(contributions welcome), one can use tools such as
1219+
[manifest-tool](https://github.com/estesp/manifest-tool) to stitch multiple
1220+
separate builds together into a single container manifest.
12041221

12051222
### General Workflow
12061223

12071224
The general workflow for creating multi-arch manifests is as follows:
12081225

12091226
1. Build separate container images using Kaniko on build hosts matching your
1210-
target architecture and tag them with the appropriate ARCH tag.
1227+
target architecture and tag them with the appropriate ARCH tag.
12111228
2. Push the separate images to your container registry.
12121229
3. Manifest-tool identifies the separate manifests in your container registry,
1213-
according to a given template.
1230+
according to a given template.
12141231
4. Manifest-tool pushes a combined manifest referencing the separate manifests.
12151232

12161233
![Workflow Multi-arch](docs/images/multi-arch.drawio.svg)
@@ -1219,27 +1236,30 @@ according to a given template.
12191236

12201237
The following conditions must be met:
12211238

1222-
1. You need access to build-machines running the desired architectures
1223-
(running Kaniko in an emulator, e.g. QEMU should also be possible but goes
1224-
beyond the scope of this documentation). This is something to keep in mind
1225-
when using SaaS build tools such as github.com or gitlab.com, of which at the
1226-
time of writing neither supports any non-x86_64 SaaS runners ([GitHub](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources),[GitLab](https://docs.gitlab.com/ee/ci/runners/saas/linux_saas_runner.html#machine-types-available-for-private-projects-x86-64)),
1227-
so be prepared to bring your own machines ([GitHub](https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners),[GitLab](https://docs.gitlab.com/runner/register/).
1239+
1. You need access to build-machines running the desired architectures (running
1240+
Kaniko in an emulator, e.g. QEMU should also be possible but goes beyond the
1241+
scope of this documentation). This is something to keep in mind when using
1242+
SaaS build tools such as github.com or gitlab.com, of which at the time of
1243+
writing neither supports any non-x86_64 SaaS runners
1244+
([GitHub](https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners#supported-runners-and-hardware-resources),[GitLab](https://docs.gitlab.com/ee/ci/runners/saas/linux_saas_runner.html#machine-types-available-for-private-projects-x86-64)),
1245+
so be prepared to bring your own machines
1246+
([GitHub](https://docs.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners),[GitLab](https://docs.gitlab.com/runner/register/).
12281247
2. Kaniko needs to be able to run on the desired architectures. At the time of
1229-
writing, the official Kaniko container supports [linux/amd64, linux/arm64,
1230-
linux/s390x and linux/ppc64le (not on *-debug images)](https://github.com/GoogleContainerTools/kaniko/blob/main/.github/workflows/images.yaml).
1248+
writing, the official Kaniko container supports
1249+
[linux/amd64, linux/arm64, linux/s390x and linux/ppc64le (not on \*-debug images)](https://github.com/GoogleContainerTools/kaniko/blob/main/.github/workflows/images.yaml).
12311250
3. The container registry of your choice must be OCIv1 or Docker v2.2
1232-
compatible.
1251+
compatible.
12331252

12341253
### Example CI Pipeline (GitLab)
12351254

1236-
It is up to you to find an automation tool that suits your needs best.
1237-
We recommend using a modern CI/CD system such as GitHub workflows or GitLab CI.
1238-
As we (the authors) happen to use GitLab CI, the following examples are
1239-
tailored to this specific platform but the underlying principles should apply
1240-
anywhere else and the examples are kept simple enough, so that you should be
1241-
able to follow along, even without any previous experiences with this specific
1242-
platform. When in doubt, visit the [gitlab-ci.yml reference page](https://docs.gitlab.com/ee/ci/yaml/index.html)
1255+
It is up to you to find an automation tool that suits your needs best. We
1256+
recommend using a modern CI/CD system such as GitHub workflows or GitLab CI. As
1257+
we (the authors) happen to use GitLab CI, the following examples are tailored to
1258+
this specific platform but the underlying principles should apply anywhere else
1259+
and the examples are kept simple enough, so that you should be able to follow
1260+
along, even without any previous experiences with this specific platform. When
1261+
in doubt, visit the
1262+
[gitlab-ci.yml reference page](https://docs.gitlab.com/ee/ci/yaml/index.html)
12431263
for a comprehensive overview of the GitLab CI keywords.
12441264

12451265
#### Building the Separate Container Images
@@ -1264,11 +1284,10 @@ build-container:
12641284
script:
12651285
# build the container image for the current arch using kaniko
12661286
- >-
1267-
/kaniko/executor
1268-
--context "${CI_PROJECT_DIR}"
1269-
--dockerfile "${CI_PROJECT_DIR}/Dockerfile"
1270-
# push the image to the GitLab container registry, add the current arch as tag.
1271-
--destination "${CI_REGISTRY_IMAGE}:${ARCH}"
1287+
/kaniko/executor --context "${CI_PROJECT_DIR}" --dockerfile
1288+
"${CI_PROJECT_DIR}/Dockerfile" # push the image to the GitLab container
1289+
registry, add the current arch as tag. --destination
1290+
"${CI_REGISTRY_IMAGE}:${ARCH}"
12721291
```
12731292
12741293
#### Merging the Container Manifests
@@ -1288,42 +1307,37 @@ merge-manifests:
12881307
# may run on any architecture supported by manifest-tool image
12891308
- runner-xyz
12901309
image:
1291-
name: mplatform/manifest-tool:alpine
1292-
entrypoint: [""]
1310+
name: mplatform/manifest-tool:alpine
1311+
entrypoint: [""]
12931312
script:
12941313
- >-
1295-
manifest-tool
1296-
# authorize against your container registry
1297-
--username=${CI_REGISTRY_USER}
1298-
--password=${CI_REGISTRY_PASSWORD}
1299-
push from-args
1300-
# define the architectures you want to merge
1301-
--platforms linux/amd64,linux/arm64
1302-
# "ARCH" will be automatically replaced by manifest-tool
1303-
# with the appropriate arch from the platform definitions
1304-
--template ${CI_REGISTRY_IMAGE}:ARCH
1305-
# The name of the final, combined image which will be pushed to your registry
1306-
--target ${CI_REGISTRY_IMAGE}
1314+
manifest-tool # authorize against your container registry
1315+
--username=${CI_REGISTRY_USER} --password=${CI_REGISTRY_PASSWORD} push
1316+
from-args # define the architectures you want to merge --platforms
1317+
linux/amd64,linux/arm64 # "ARCH" will be automatically replaced by
1318+
manifest-tool # with the appropriate arch from the platform definitions
1319+
--template ${CI_REGISTRY_IMAGE}:ARCH # The name of the final, combined
1320+
image which will be pushed to your registry --target ${CI_REGISTRY_IMAGE}
13071321
```
13081322
13091323
#### On the Note of Adding Versioned Tags
13101324
1311-
For simplicity's sake we deliberately refrained from using versioned
1312-
tagged images (all builds will be tagged as "latest") in the
1313-
previous examples, as we feel like this adds to much platform and workflow
1314-
specific code.
1325+
For simplicity's sake we deliberately refrained from using versioned tagged
1326+
images (all builds will be tagged as "latest") in the previous examples, as we
1327+
feel like this adds to much platform and workflow specific code.
13151328
13161329
Nethertheless, for anyone interested in how we handle (dynamic) versioning in
13171330
GitLab, here is a short rundown:
13181331
1319-
- If you are only interested in building tagged releases, you can simply
1320-
use the [GitLab predefined](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html) `CI_COMMIT_TAG` variable when running a tag pipeline.
1332+
- If you are only interested in building tagged releases, you can simply use the
1333+
[GitLab predefined](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html)
1334+
`CI_COMMIT_TAG` variable when running a tag pipeline.
13211335
- When you (like us) want to additionally build container images outside of
1322-
releases, things get a bit messier. In our case, we added a additional job
1323-
which runs before the build and merge jobs (don't forget to extend the `needs`
1324-
section of the build and merge jobs accordingly), which will set the tag to
1325-
`latest` when running on the default branch, to the commit hash when run on
1326-
other branches and to the release tag when run on a tag pipeline.
1336+
releases, things get a bit messier. In our case, we added a additional job
1337+
which runs before the build and merge jobs (don't forget to extend the `needs`
1338+
section of the build and merge jobs accordingly), which will set the tag to
1339+
`latest` when running on the default branch, to the commit hash when run on
1340+
other branches and to the release tag when run on a tag pipeline.
13271341

13281342
gitlab-ci.yml:
13291343

cmd/executor/cmd/root.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os/exec"
2323
"path/filepath"
2424
"regexp"
25+
"strconv"
2526
"strings"
2627
"time"
2728

@@ -71,6 +72,15 @@ func validateFlags() {
7172
opts.RegistryMirrors.Set(val)
7273
}
7374

75+
// Allow setting --no-push using an environment variable.
76+
if val, ok := os.LookupEnv("KANIKO_NO_PUSH"); ok {
77+
valBoolean, err := strconv.ParseBool(val)
78+
if err != nil {
79+
errors.New("invalid value (true/false) for KANIKO_NO_PUSH environment variable")
80+
}
81+
opts.NoPush = valBoolean
82+
}
83+
7484
// Allow setting --registry-maps using an environment variable.
7585
if val, ok := os.LookupEnv("KANIKO_REGISTRY_MAP"); ok {
7686
opts.RegistryMaps.Set(val)

0 commit comments

Comments
 (0)