2021-06-26 16:36:31 -03:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
feat: add digest to artifacts info of published docker images (#3540)
Extract the digest (sha256) of docker images from the `docker push`
command for dockers published to the docker registry.
Outputting the digest is required to avoid a race condition when
referencing the image, where the image tag is being modified before the
reference is done.
See this [blog
post](https://github.com/goreleaser/goreleaser/issues/3496) for more
info.
This PR fixes https://github.com/goreleaser/goreleaser/issues/3496.
Note that the 'publish' pipe now must run before the 'metadata' pipe, so
that the information extracted during the 'publish' pipe would appear in
the metadata.
Previously, the published docker images metadata wasn't printed (because
of the order). It made sense because the content of the published image
was just a subset of the local one.
Now that it is printed to the metadata, it should have a different name
to avoid confusion.
As I mentioned, it wasn't printed before - so there shouldn't be any
backward-compatibility issues.
---
Local tests:
```
go test -v .
=== RUN TestVersion
=== RUN TestVersion/only_version
=== RUN TestVersion/version_and_date
=== RUN TestVersion/version,_date,_built_by
=== RUN TestVersion/all_empty
=== RUN TestVersion/complete
--- PASS: TestVersion (0.00s)
--- PASS: TestVersion/only_version (0.00s)
--- PASS: TestVersion/version_and_date (0.00s)
--- PASS: TestVersion/version,_date,_built_by (0.00s)
--- PASS: TestVersion/all_empty (0.00s)
--- PASS: TestVersion/complete (0.00s)
PASS
ok github.com/goreleaser/goreleaser 0.764s
```
Output example:
```
{
"name": "gallegit/hello-world:latest",
"path": "gallegit/hello-world:latest",
"goos": "linux",
"goarch": "amd64",
"internal_type": 10,
"type": "Published Docker Image",
"extra": {
"digest": "sha256:c3f7dd196a046dc061236d3c6ae1e2946269e90da30b0a959240ca799750e632"
}
}
```
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2022-11-12 19:51:53 +02:00
|
|
|
"regexp"
|
2021-11-24 14:42:12 +01:00
|
|
|
|
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
2021-06-26 16:36:31 -03:00
|
|
|
)
|
|
|
|
|
2021-06-27 17:55:00 -03:00
|
|
|
func init() {
|
|
|
|
registerManifester(useDocker, dockerManifester{})
|
|
|
|
|
|
|
|
registerImager(useDocker, dockerImager{})
|
|
|
|
registerImager(useBuildx, dockerImager{
|
|
|
|
buildx: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-06-26 16:36:31 -03:00
|
|
|
type dockerManifester struct{}
|
|
|
|
|
2021-11-24 14:42:12 +01:00
|
|
|
func (m dockerManifester) Create(ctx *context.Context, manifest string, images, flags []string) error {
|
2021-07-24 21:32:04 -03:00
|
|
|
_ = runCommand(ctx, ".", "docker", "manifest", "rm", manifest)
|
2021-06-26 16:36:31 -03:00
|
|
|
|
|
|
|
args := []string{"manifest", "create", manifest}
|
|
|
|
args = append(args, images...)
|
|
|
|
args = append(args, flags...)
|
|
|
|
|
2021-07-24 21:32:04 -03:00
|
|
|
if err := runCommand(ctx, ".", "docker", args...); err != nil {
|
2021-06-26 16:36:31 -03:00
|
|
|
return fmt.Errorf("failed to create %s: %w", manifest, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-28 21:30:16 -03:00
|
|
|
func (m dockerManifester) Push(ctx *context.Context, manifest string, flags []string) (string, error) {
|
2021-06-26 16:36:31 -03:00
|
|
|
args := []string{"manifest", "push", manifest}
|
|
|
|
args = append(args, flags...)
|
2022-11-28 21:30:16 -03:00
|
|
|
bts, err := runCommandWithOutput(ctx, ".", "docker", args...)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to push %s: %w", manifest, err)
|
2021-06-26 16:36:31 -03:00
|
|
|
}
|
2022-11-28 21:30:16 -03:00
|
|
|
digest := dockerDigestPattern.FindString(string(bts))
|
|
|
|
if digest == "" {
|
|
|
|
return "", fmt.Errorf("failed to find docker digest in docker push output: %s", string(bts))
|
|
|
|
}
|
|
|
|
return digest, nil
|
2021-06-26 16:36:31 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
type dockerImager struct {
|
|
|
|
buildx bool
|
|
|
|
}
|
|
|
|
|
feat: add digest to artifacts info of published docker images (#3540)
Extract the digest (sha256) of docker images from the `docker push`
command for dockers published to the docker registry.
Outputting the digest is required to avoid a race condition when
referencing the image, where the image tag is being modified before the
reference is done.
See this [blog
post](https://github.com/goreleaser/goreleaser/issues/3496) for more
info.
This PR fixes https://github.com/goreleaser/goreleaser/issues/3496.
Note that the 'publish' pipe now must run before the 'metadata' pipe, so
that the information extracted during the 'publish' pipe would appear in
the metadata.
Previously, the published docker images metadata wasn't printed (because
of the order). It made sense because the content of the published image
was just a subset of the local one.
Now that it is printed to the metadata, it should have a different name
to avoid confusion.
As I mentioned, it wasn't printed before - so there shouldn't be any
backward-compatibility issues.
---
Local tests:
```
go test -v .
=== RUN TestVersion
=== RUN TestVersion/only_version
=== RUN TestVersion/version_and_date
=== RUN TestVersion/version,_date,_built_by
=== RUN TestVersion/all_empty
=== RUN TestVersion/complete
--- PASS: TestVersion (0.00s)
--- PASS: TestVersion/only_version (0.00s)
--- PASS: TestVersion/version_and_date (0.00s)
--- PASS: TestVersion/version,_date,_built_by (0.00s)
--- PASS: TestVersion/all_empty (0.00s)
--- PASS: TestVersion/complete (0.00s)
PASS
ok github.com/goreleaser/goreleaser 0.764s
```
Output example:
```
{
"name": "gallegit/hello-world:latest",
"path": "gallegit/hello-world:latest",
"goos": "linux",
"goarch": "amd64",
"internal_type": 10,
"type": "Published Docker Image",
"extra": {
"digest": "sha256:c3f7dd196a046dc061236d3c6ae1e2946269e90da30b0a959240ca799750e632"
}
}
```
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2022-11-12 19:51:53 +02:00
|
|
|
var dockerDigestPattern = regexp.MustCompile("sha256:[a-z0-9]{64}")
|
|
|
|
|
2023-03-19 22:17:18 -03:00
|
|
|
func (i dockerImager) Push(ctx *context.Context, image string, _ []string) (string, error) {
|
2022-11-28 21:30:16 -03:00
|
|
|
bts, err := runCommandWithOutput(ctx, ".", "docker", "push", image)
|
feat: add digest to artifacts info of published docker images (#3540)
Extract the digest (sha256) of docker images from the `docker push`
command for dockers published to the docker registry.
Outputting the digest is required to avoid a race condition when
referencing the image, where the image tag is being modified before the
reference is done.
See this [blog
post](https://github.com/goreleaser/goreleaser/issues/3496) for more
info.
This PR fixes https://github.com/goreleaser/goreleaser/issues/3496.
Note that the 'publish' pipe now must run before the 'metadata' pipe, so
that the information extracted during the 'publish' pipe would appear in
the metadata.
Previously, the published docker images metadata wasn't printed (because
of the order). It made sense because the content of the published image
was just a subset of the local one.
Now that it is printed to the metadata, it should have a different name
to avoid confusion.
As I mentioned, it wasn't printed before - so there shouldn't be any
backward-compatibility issues.
---
Local tests:
```
go test -v .
=== RUN TestVersion
=== RUN TestVersion/only_version
=== RUN TestVersion/version_and_date
=== RUN TestVersion/version,_date,_built_by
=== RUN TestVersion/all_empty
=== RUN TestVersion/complete
--- PASS: TestVersion (0.00s)
--- PASS: TestVersion/only_version (0.00s)
--- PASS: TestVersion/version_and_date (0.00s)
--- PASS: TestVersion/version,_date,_built_by (0.00s)
--- PASS: TestVersion/all_empty (0.00s)
--- PASS: TestVersion/complete (0.00s)
PASS
ok github.com/goreleaser/goreleaser 0.764s
```
Output example:
```
{
"name": "gallegit/hello-world:latest",
"path": "gallegit/hello-world:latest",
"goos": "linux",
"goarch": "amd64",
"internal_type": 10,
"type": "Published Docker Image",
"extra": {
"digest": "sha256:c3f7dd196a046dc061236d3c6ae1e2946269e90da30b0a959240ca799750e632"
}
}
```
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2022-11-12 19:51:53 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to push %s: %w", image, err)
|
2021-06-26 16:36:31 -03:00
|
|
|
}
|
2022-11-28 21:30:16 -03:00
|
|
|
digest := dockerDigestPattern.FindString(string(bts))
|
feat: add digest to artifacts info of published docker images (#3540)
Extract the digest (sha256) of docker images from the `docker push`
command for dockers published to the docker registry.
Outputting the digest is required to avoid a race condition when
referencing the image, where the image tag is being modified before the
reference is done.
See this [blog
post](https://github.com/goreleaser/goreleaser/issues/3496) for more
info.
This PR fixes https://github.com/goreleaser/goreleaser/issues/3496.
Note that the 'publish' pipe now must run before the 'metadata' pipe, so
that the information extracted during the 'publish' pipe would appear in
the metadata.
Previously, the published docker images metadata wasn't printed (because
of the order). It made sense because the content of the published image
was just a subset of the local one.
Now that it is printed to the metadata, it should have a different name
to avoid confusion.
As I mentioned, it wasn't printed before - so there shouldn't be any
backward-compatibility issues.
---
Local tests:
```
go test -v .
=== RUN TestVersion
=== RUN TestVersion/only_version
=== RUN TestVersion/version_and_date
=== RUN TestVersion/version,_date,_built_by
=== RUN TestVersion/all_empty
=== RUN TestVersion/complete
--- PASS: TestVersion (0.00s)
--- PASS: TestVersion/only_version (0.00s)
--- PASS: TestVersion/version_and_date (0.00s)
--- PASS: TestVersion/version,_date,_built_by (0.00s)
--- PASS: TestVersion/all_empty (0.00s)
--- PASS: TestVersion/complete (0.00s)
PASS
ok github.com/goreleaser/goreleaser 0.764s
```
Output example:
```
{
"name": "gallegit/hello-world:latest",
"path": "gallegit/hello-world:latest",
"goos": "linux",
"goarch": "amd64",
"internal_type": 10,
"type": "Published Docker Image",
"extra": {
"digest": "sha256:c3f7dd196a046dc061236d3c6ae1e2946269e90da30b0a959240ca799750e632"
}
}
```
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2022-11-12 19:51:53 +02:00
|
|
|
if digest == "" {
|
2022-11-28 21:30:16 -03:00
|
|
|
return "", fmt.Errorf("failed to find docker digest in docker push output: %s", string(bts))
|
feat: add digest to artifacts info of published docker images (#3540)
Extract the digest (sha256) of docker images from the `docker push`
command for dockers published to the docker registry.
Outputting the digest is required to avoid a race condition when
referencing the image, where the image tag is being modified before the
reference is done.
See this [blog
post](https://github.com/goreleaser/goreleaser/issues/3496) for more
info.
This PR fixes https://github.com/goreleaser/goreleaser/issues/3496.
Note that the 'publish' pipe now must run before the 'metadata' pipe, so
that the information extracted during the 'publish' pipe would appear in
the metadata.
Previously, the published docker images metadata wasn't printed (because
of the order). It made sense because the content of the published image
was just a subset of the local one.
Now that it is printed to the metadata, it should have a different name
to avoid confusion.
As I mentioned, it wasn't printed before - so there shouldn't be any
backward-compatibility issues.
---
Local tests:
```
go test -v .
=== RUN TestVersion
=== RUN TestVersion/only_version
=== RUN TestVersion/version_and_date
=== RUN TestVersion/version,_date,_built_by
=== RUN TestVersion/all_empty
=== RUN TestVersion/complete
--- PASS: TestVersion (0.00s)
--- PASS: TestVersion/only_version (0.00s)
--- PASS: TestVersion/version_and_date (0.00s)
--- PASS: TestVersion/version,_date,_built_by (0.00s)
--- PASS: TestVersion/all_empty (0.00s)
--- PASS: TestVersion/complete (0.00s)
PASS
ok github.com/goreleaser/goreleaser 0.764s
```
Output example:
```
{
"name": "gallegit/hello-world:latest",
"path": "gallegit/hello-world:latest",
"goos": "linux",
"goarch": "amd64",
"internal_type": 10,
"type": "Published Docker Image",
"extra": {
"digest": "sha256:c3f7dd196a046dc061236d3c6ae1e2946269e90da30b0a959240ca799750e632"
}
}
```
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
Co-authored-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2022-11-12 19:51:53 +02:00
|
|
|
}
|
|
|
|
return digest, nil
|
2021-06-26 16:36:31 -03:00
|
|
|
}
|
|
|
|
|
2021-11-24 14:42:12 +01:00
|
|
|
func (i dockerImager) Build(ctx *context.Context, root string, images, flags []string) error {
|
2021-07-24 21:32:04 -03:00
|
|
|
if err := runCommand(ctx, root, "docker", i.buildCommand(images, flags)...); err != nil {
|
2021-06-26 16:36:31 -03:00
|
|
|
return fmt.Errorf("failed to build %s: %w", images[0], err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i dockerImager) buildCommand(images, flags []string) []string {
|
|
|
|
base := []string{"build", "."}
|
|
|
|
if i.buildx {
|
2022-06-24 21:13:33 +02:00
|
|
|
base = []string{"buildx", "--builder", "default", "build", ".", "--load"}
|
2021-06-26 16:36:31 -03:00
|
|
|
}
|
|
|
|
for _, image := range images {
|
|
|
|
base = append(base, "-t", image)
|
|
|
|
}
|
|
|
|
base = append(base, flags...)
|
|
|
|
return base
|
|
|
|
}
|