mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-19 09:16:29 +00:00
include source repo, revision and go package version as build-args
Signed-off-by: Avi Deitcher <avi@deitcher.net>
This commit is contained in:
parent
aad12b570f
commit
de13ee521d
@ -264,6 +264,23 @@ linuxkit pkg build --platforms=linux/arm64 --builders linux/arm64=my-remote-arm6
|
|||||||
|
|
||||||
linuxkit will try to build for `linux/arm64` using the context `my-remote-arm64`. Since that context does not exist, you will get an error.
|
linuxkit will try to build for `linux/arm64` using the context `my-remote-arm64`. Since that context does not exist, you will get an error.
|
||||||
|
|
||||||
|
##### Preset build arguments
|
||||||
|
|
||||||
|
When building packages, the following build-args automatically are set for you:
|
||||||
|
|
||||||
|
* `SOURCE` - the source repository of the package
|
||||||
|
* `REVISION` - the git commit that was used for the build
|
||||||
|
* `GOPKGVERSION` - the go package version or pseudo-version per https://go.dev/ref/mod#glos-pseudo-version
|
||||||
|
|
||||||
|
Note that the above are set **only** if you do not set them in `build.yaml`. Your settings _always_
|
||||||
|
override these built-in ones.
|
||||||
|
|
||||||
|
To use them, simply address them in your `Dockerfile`:
|
||||||
|
|
||||||
|
```dockerfile
|
||||||
|
ARG SOURCE
|
||||||
|
```
|
||||||
|
|
||||||
### Build packages as a maintainer
|
### Build packages as a maintainer
|
||||||
|
|
||||||
All official LinuxKit packages are multi-arch manifests and most of
|
All official LinuxKit packages are multi-arch manifests and most of
|
||||||
|
@ -309,12 +309,23 @@ func (p Pkg) Build(bos ...BuildOpt) error {
|
|||||||
if p.git != nil && p.gitRepo != "" {
|
if p.git != nil && p.gitRepo != "" {
|
||||||
imageBuildOpts.Labels["org.opencontainers.image.source"] = p.gitRepo
|
imageBuildOpts.Labels["org.opencontainers.image.source"] = p.gitRepo
|
||||||
}
|
}
|
||||||
if p.git != nil && !p.dirty {
|
var (
|
||||||
commit, err := p.git.commitHash("HEAD")
|
gitCommit string
|
||||||
|
goPkgVersion string
|
||||||
|
)
|
||||||
|
if p.git != nil {
|
||||||
|
if !p.dirty {
|
||||||
|
gitCommit, err = p.git.commitHash("HEAD")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
imageBuildOpts.Labels["org.opencontainers.image.revision"] = gitCommit
|
||||||
|
}
|
||||||
|
// get the go version or pseudo-version
|
||||||
|
goPkgVersion, err = p.git.goPkgVersion()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
imageBuildOpts.Labels["org.opencontainers.image.revision"] = commit
|
|
||||||
}
|
}
|
||||||
|
|
||||||
imageBuildOpts.NetworkMode = "default"
|
imageBuildOpts.NetworkMode = "default"
|
||||||
@ -342,6 +353,16 @@ func (p Pkg) Build(bos ...BuildOpt) error {
|
|||||||
imageBuildOpts.BuildArgs[parts[0]] = &parts[1]
|
imageBuildOpts.BuildArgs[parts[0]] = &parts[1]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// add in information about the build process that might be useful
|
||||||
|
if _, ok := imageBuildOpts.BuildArgs["SOURCE"]; !ok && p.gitRepo != "" {
|
||||||
|
imageBuildOpts.BuildArgs["SOURCE"] = &p.gitRepo
|
||||||
|
}
|
||||||
|
if _, ok := imageBuildOpts.BuildArgs["REVISION"]; !ok && gitCommit != "" {
|
||||||
|
imageBuildOpts.BuildArgs["REVISION"] = &gitCommit
|
||||||
|
}
|
||||||
|
if _, ok := imageBuildOpts.BuildArgs["GOPKGVERSION"]; !ok && goPkgVersion != "" {
|
||||||
|
imageBuildOpts.BuildArgs["GOPKGVERSION"] = &goPkgVersion
|
||||||
|
}
|
||||||
|
|
||||||
// build for each arch and save in the linuxkit cache
|
// build for each arch and save in the linuxkit cache
|
||||||
for _, platform := range platformsToBuild {
|
for _, platform := range platformsToBuild {
|
||||||
|
@ -211,3 +211,36 @@ func (g git) isDirty(pkg, commit string) (bool, error) {
|
|||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// goPkgVersion return a version that is compliant with go package versioning.
|
||||||
|
// This would either be:
|
||||||
|
//
|
||||||
|
// - The tag name if the most recent commit is tagged
|
||||||
|
// - The structure <version>-<count>-<commmit> if the most recent commit is not tagged
|
||||||
|
//
|
||||||
|
// See https://go.dev/ref/mod for more information
|
||||||
|
func (g git) goPkgVersion() (string, error) {
|
||||||
|
lastSemver, _ := g.commandStdout(nil, "--no-pager", "describe", "--match='v[0-9].[0-9].[0-9]*'", "--abbrev=0", "--tags")
|
||||||
|
if lastSemver == "" {
|
||||||
|
lastSemver = "v0.0.0"
|
||||||
|
}
|
||||||
|
commitList := "HEAD"
|
||||||
|
if lastSemver != "v0.0.0" {
|
||||||
|
commitList = fmt.Sprintf("%s..HEAD", lastSemver)
|
||||||
|
}
|
||||||
|
count, err := g.commandStdout(nil, "rev-list", commitList, "--count")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
version := ""
|
||||||
|
if count == "0" {
|
||||||
|
version = lastSemver
|
||||||
|
} else {
|
||||||
|
dateCommit, err := g.commandStdout(nil, "--no-pager", "show", "--quiet", "--abbrev=12", "--date=format-local:%Y%m%d%H%M%S", "--format=%cd-%h")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
version = fmt.Sprintf("%s-%s", lastSemver, dateCommit)
|
||||||
|
}
|
||||||
|
return version, nil
|
||||||
|
}
|
||||||
|
@ -10,7 +10,7 @@ set -ex
|
|||||||
. "${RT_PROJECT_ROOT}/_lib/lib.sh"
|
. "${RT_PROJECT_ROOT}/_lib/lib.sh"
|
||||||
|
|
||||||
# Test code goes here
|
# Test code goes here
|
||||||
echo Linuxkig is "$(which linuxkit)"
|
echo Linuxkit is "$(which linuxkit)"
|
||||||
RESULT="$(2>&1 linuxkit pkg build --force . | grep PASSED)"
|
RESULT="$(2>&1 linuxkit pkg build --force . | grep PASSED)"
|
||||||
echo RESULT="${RESULT}"
|
echo RESULT="${RESULT}"
|
||||||
echo "${RESULT}" | grep "Build-arg test PASSED"
|
echo "${RESULT}" | grep "Build-arg test PASSED"
|
||||||
|
11
test/cases/000_build/031_fixed_build_args/Dockerfile
Normal file
11
test/cases/000_build/031_fixed_build_args/Dockerfile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
FROM alpine:3.17
|
||||||
|
|
||||||
|
ARG SOURCE=FAILED
|
||||||
|
ARG REVISION=FAILED
|
||||||
|
ARG GOPKGVERSION=FAILED
|
||||||
|
|
||||||
|
RUN echo "printf \"Build-arg test source $SOURCE\\n\"" >> check.sh
|
||||||
|
RUN echo "printf \"Build-arg test revision $REVISION\\n\"" >> check.sh
|
||||||
|
RUN echo "printf \"Build-arg test gopkgversion $GOPKGVERSION\\n\"" >> check.sh
|
||||||
|
|
||||||
|
ENTRYPOINT ["/bin/sh", "/check.sh"]
|
5
test/cases/000_build/031_fixed_build_args/build.yml
Normal file
5
test/cases/000_build/031_fixed_build_args/build.yml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
image: build-args-test
|
||||||
|
network: true
|
||||||
|
arches:
|
||||||
|
- amd64
|
||||||
|
- arm64
|
18
test/cases/000_build/031_fixed_build_args/test.sh
Executable file
18
test/cases/000_build/031_fixed_build_args/test.sh
Executable file
@ -0,0 +1,18 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# SUMMARY: Check that the build-args are correctly passed to Dockerfiles
|
||||||
|
# LABELS:
|
||||||
|
# REPEAT:
|
||||||
|
|
||||||
|
set -ex
|
||||||
|
|
||||||
|
# Source libraries. Uncomment if needed/defined
|
||||||
|
#. "${RT_LIB}"
|
||||||
|
. "${RT_PROJECT_ROOT}/_lib/lib.sh"
|
||||||
|
|
||||||
|
# Test code goes here
|
||||||
|
echo Linuxkit is "$(which linuxkit)"
|
||||||
|
RESULT="$(2>&1 linuxkit pkg build --force . | grep 'Build-arg test' || echo)"
|
||||||
|
echo RESULT="${RESULT}"
|
||||||
|
echo "${RESULT}" | grep -v "FAILED"
|
||||||
|
|
||||||
|
exit 0
|
Loading…
Reference in New Issue
Block a user