-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c6d7bd
commit f0e5dc0
Showing
7 changed files
with
624 additions
and
10 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
FROM python:3.12-alpine AS ansible | ||
ARG ANSIBLE_VERSION=10.0 | ||
RUN apk -U upgrade --available &&\ | ||
# Required to install ansible pip package, bear in mind to remove those build deps at the end of this RUN directive | ||
apk add --no-cache gcc musl-dev libffi-dev openssl-dev &&\ | ||
# Add here package mandatory to be able to run ansible | ||
apk add --no-cache openssh-client ca-certificates&&\ | ||
pip install --no-cache-dir --upgrade pip &&\ | ||
pip install --no-cache-dir ansible==${ANSIBLE_VERSION}.* ansible-runner~=2.4 &&\ | ||
# Cleanup package manager cache and remove build deps | ||
rm -rf /var/cache/apk/* &&\ | ||
pip cache purge &&\ | ||
apk del gcc musl-dev gcc musl-dev libffi-dev openssl-dev | ||
|
||
FROM ansible AS base | ||
USER 1983 | ||
|
||
FROM ansible AS gcp | ||
RUN pip install --no-cache-dir requests==2.32.3 google-auth==2.32.0 | ||
USER 1983 | ||
|
||
FROM ansible AS aws | ||
RUN pip install --no-cache-dir boto3==1.34.151 | ||
USER 1983 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module github.com/spacelift-io/build-matrix | ||
|
||
go 1.22.1 | ||
|
||
require ( | ||
github.com/Masterminds/semver/v3 v3.2.1 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/testify v1.9.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0= | ||
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"sort" | ||
|
||
"github.com/Masterminds/semver/v3" | ||
) | ||
|
||
const ( | ||
// Define the oldest major version we care about, we do not want to build image starting ansible 1.0 | ||
minSupportedMajor = 7 | ||
) | ||
|
||
type ReleaseResponse struct { | ||
Releases map[string]any `json:"releases"` | ||
} | ||
|
||
type matrixVersion struct { | ||
Ansible string `json:"ansible"` | ||
AdditionalTags []string `json:"additional_tags"` | ||
} | ||
type Matrix []matrixVersion | ||
|
||
// This small script reads ansible versions from pypi and returns an aggregated list of deduplicated minor version. | ||
// This is used to compute the build matrix in github to build all minor versions in parallel. | ||
// This script will also find the latest minor version for every major to be able to tag the docker image dynamically. | ||
// For example if we have the following version returned: | ||
// - 10.1.1 | ||
// - 10.3.1 | ||
// - 10.4.3 | ||
// - 10.4.3. | ||
// The script will return the following versions: | ||
// - 10.1 | ||
// - 10.3 | ||
// - 10.4, additional_tags: 10 | ||
// Check main_test.go for a quick overview of the expected behavior. | ||
func main() { | ||
resp, err := http.Get("https://pypi.org/pypi/ansible/json") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
matrixOutput := GenerateBuildMatrix(resp.Body, minSupportedMajor) | ||
|
||
output, err := json.Marshal(matrixOutput) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Print(string(output)) | ||
} | ||
|
||
func GenerateBuildMatrix(reader io.Reader, minSupportedMajor uint64) Matrix { | ||
releases := ReleaseResponse{} | ||
if err := json.NewDecoder(reader).Decode(&releases); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
var versions []*semver.Version | ||
|
||
for v := range releases.Releases { | ||
version, err := semver.NewVersion(v) | ||
if err != nil { | ||
log.Printf("Unable to parse version %s\n", v) | ||
continue | ||
} | ||
versions = append(versions, version) | ||
} | ||
|
||
sort.Slice(versions, func(i, j int) bool { | ||
return versions[j].LessThan(versions[i]) | ||
}) | ||
|
||
versionGroupedByMajor := make(map[int][]*semver.Version) | ||
// Just used for stable ordering | ||
var majorVersions []int | ||
|
||
for _, version := range versions { | ||
if version.Major() < minSupportedMajor { | ||
break | ||
} | ||
major := int(version.Major()) | ||
if _, exists := versionGroupedByMajor[major]; !exists { | ||
majorVersions = append(majorVersions, major) | ||
} | ||
versionGroupedByMajor[major] = append(versionGroupedByMajor[major], version) | ||
} | ||
|
||
sort.Sort(sort.Reverse(sort.IntSlice(majorVersions))) | ||
|
||
minorVersionDeduplication := map[string]any{} | ||
matrix := Matrix{} | ||
for _, majorVersion := range majorVersions { | ||
for i, version := range versionGroupedByMajor[majorVersion] { | ||
key := fmt.Sprintf("%d.%d", version.Major(), version.Minor()) | ||
if _, exists := minorVersionDeduplication[key]; exists { | ||
continue | ||
} | ||
additionalTags := make([]string, 0) | ||
if i == 0 { | ||
additionalTags = append(additionalTags, fmt.Sprintf("%d", version.Major())) | ||
} | ||
minorVersionDeduplication[key] = struct{}{} | ||
matrix = append(matrix, matrixVersion{ | ||
Ansible: key, | ||
AdditionalTags: additionalTags, | ||
}) | ||
} | ||
} | ||
|
||
return matrix | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGenerateBuildMatrix(t *testing.T) { | ||
fakePythonVersions := ReleaseResponse{ | ||
Releases: map[string]any{ | ||
"1.1.0": struct{}{}, | ||
"1.1.1": struct{}{}, | ||
"2.10.0": struct{}{}, | ||
"2.11.0": struct{}{}, | ||
"2.11.2": struct{}{}, | ||
"3.1.0": struct{}{}, | ||
"3.1.1": struct{}{}, | ||
"3.2.0": struct{}{}, | ||
}, | ||
} | ||
|
||
fakeJsonResponse, err := json.Marshal(fakePythonVersions) | ||
require.NoError(t, err) | ||
|
||
matrix := GenerateBuildMatrix(bytes.NewReader(fakeJsonResponse), 2) | ||
expectedMatrix := Matrix{ | ||
{ | ||
Ansible: "3.2", | ||
AdditionalTags: []string{"3"}, | ||
}, | ||
{ | ||
Ansible: "3.1", | ||
AdditionalTags: []string{}, | ||
}, | ||
{ | ||
Ansible: "2.11", | ||
AdditionalTags: []string{"2"}, | ||
}, | ||
{ | ||
Ansible: "2.10", | ||
AdditionalTags: []string{}, | ||
}, | ||
} | ||
assert.Equal(t, expectedMatrix, matrix) | ||
} |