Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GH-49] Ensure that the latest version will be returned. #51

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Go
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: macos-10.15
shasum: shasum -a 512
- os: ubuntu-20.04
shasum: sha512sum
- os: windows-2019
shasum: sha512sum
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15.7
- name: Set DIP deliverable environment variable
run: echo "dip-deliverable=dip-${{ matrix.os }}" >> $GITHUB_ENV
- name: Use the value
run: |
echo "${{ env.dip-deliverable }}"
- name: Build
run: ./scripts/build.sh
env:
DIP_DELIVERABLE: ${{ env.dip-deliverable }}
SHA512_CMD: ${{ matrix.shasum }}
- name: Unit test
run: go test -short -cover -v -coverprofile=coverage.txt -covermode=atomic ./...
if: ${{ startsWith(matrix.os, 'ubuntu') }}
- name: Integration test
run: ./test/integration-tests.sh
env:
DIP_DELIVERABLE: ${{ env.dip-deliverable }}
DIP_VERSION: ${{ matrix.nexus-version }}
DIP_API_VERSION: ${{ matrix.nexus-api-version }}
DIP_APT_GPG_SECRET: ${{ secrets.DIP_APT_GPG_SECRET }}
if: ${{ startsWith(matrix.os, 'ubuntu') }}
66 changes: 66 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Release
on:
push:
tags:
- "*"
jobs:
release:
name: Create Release
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: macos-10.15
shasum: shasum -a 512
- os: ubuntu-20.04
shasum: sha512sum
- os: windows-2019
shasum: sha512sum
steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15.7
- name: Set DIP deliverable environment variable
run: echo "dip-deliverable=dip-${{ matrix.os }}" >> $GITHUB_ENV
if: ${{ startsWith(matrix.os, 'mac') || startsWith(matrix.os, 'ubuntu') }}
- name: Set DIP deliverable environment variable on Windows
run: echo "dip-deliverable=dip-${{ matrix.os }}" >> $env:GITHUB_ENV
if: ${{ startsWith(matrix.os, 'windows') }}
- name: Use the value
run: |
echo "${{ env.dip-deliverable }}"
- name: Create release
run: ./scripts/build.sh
env:
DIP_DELIVERABLE: ${{ env.dip-deliverable }}
GITHUB_TAG: ${{ github.ref }}
SHA512_CMD: ${{ matrix.shasum }}
if: ${{ startsWith(matrix.os, 'mac') || startsWith(matrix.os, 'ubuntu') }}

- name: Create release windows
shell: cmd
run: |
echo "GITHUB_TAG: '${{ github.ref }}' DIP_DELIVERABLE: '${{ env.dip-deliverable }}'"
cd cmd/dip
go build -ldflags "-X main.Version=${{ github.ref }}" -o "${{ env.dip-deliverable }}"
sha512sum "${{ env.dip-deliverable }}" > "${{ env.dip-deliverable }}.sha512.txt"
chmod +x "${{ env.dip-deliverable }}"
ls dip-windows-2019
if: ${{ startsWith(matrix.os, 'windows') }}

- name: Upload binaries to release
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: cmd/dip/${{ env.dip-deliverable }}
asset_name: ${{ env.dip-deliverable }}
tag: ${{ github.ref }}
- name: Upload checksums
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: cmd/dip/${{ env.dip-deliverable }}.sha512.txt
asset_name: ${{ env.dip-deliverable }}.sha512.txt
tag: ${{ github.ref }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
cmd/dip/dip.sha512.txt
cmd/dip/dip
36 changes: 0 additions & 36 deletions .travis.yml

This file was deleted.

18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [2.1.3] - 2020-03-5

### Fixed

- Return latest tag.

[Unreleased]: https://github.com/030/dip/compare/2.1.3...HEAD
[2.1.3]: https://github.com/030/dip/compare/2.1.2...2.1.3
[2.1.2]: https://github.com/030/dip/releases/tag/2.1.2
4 changes: 2 additions & 2 deletions cmd/dip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
log "github.com/sirupsen/logrus"
)

var ver string
var Version string

func dockerfileTag(i string) (string, error) {
b, err := ioutil.ReadFile("Dockerfile")
Expand Down Expand Up @@ -45,7 +45,7 @@ func main() {
}

if *version {
fmt.Println(ver)
fmt.Println(Version)
return
}

Expand Down
3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand All @@ -29,14 +28,12 @@ github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1 h1:Y/KGZSOdz/2r0WJ9Mkmz6NJBusp0kiNx1Cn82lzJQ6w=
golang.org/x/net v0.0.0-20181011144130-49bb7cea24b1/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4=
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
Expand Down
20 changes: 15 additions & 5 deletions internal/sort/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import (
"regexp"
"sort"
"strconv"

log "github.com/sirupsen/logrus"
)

func semantic(tag string) (bool, error) {
matched, err := regexp.Match("^([0-9]+\\.){2}[0-9]+", []byte(tag))
matched, err := regexp.Match("^v?([0-9]+\\.){2}[0-9]+", []byte(tag))
if err != nil {
return false, err
}
return matched, nil
}

func something(tags []string) (string, error) {
func Tags(tags []string) (string, error) {
log.Debugf("Input: '%v'", tags)
var latestTag string
var m = make(map[int]string)
for _, tag := range tags {
Expand All @@ -24,14 +27,16 @@ func something(tags []string) (string, error) {
return "", err
}
if !sem {
return "", fmt.Errorf("Tag: '%s' is not a semantic version", tag)
continue
}

reg, err := regexp.Compile("[a-zA-Z-]+")
reg, err := regexp.Compile("[_a-zA-Z-]+")
if err != nil {
return "", err
}
log.Debugf("tag: '%v'", tag)
tagWithoutChars := reg.ReplaceAllString(tag, "")
log.Debugf("tagWithoutChars: '%v'", tagWithoutChars)
semver := regexp.MustCompile("\\.").Split(tagWithoutChars, 3)
major, err := strconv.Atoi(semver[0])
if err != nil {
Expand All @@ -48,17 +53,22 @@ func something(tags []string) (string, error) {
version := major*1e6 + minor*1e3 + patch
m[version] = tag
}

log.Debugf("Map: '%v'", m)
keys := make([]int, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Ints(keys)
log.Debugf("Sorted keys: '%v'", keys)

for i, k := range keys {
if i == len(keys)-1 {
latestTag = m[k]
}
}
if latestTag == "" {
return "", fmt.Errorf("Cannot find the latest tag. Check whether the tags are semantic")
}

return latestTag, nil
}
10 changes: 5 additions & 5 deletions internal/sort/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ func TestSemantic(t *testing.T) {
assert.Equal(t, true, act)
}

func TestSomething(t *testing.T) {
// [latest stable-alpine-perl stable-alpine 1.18.0-alpine-perl 1.18.0-alpine 1.18-alpine-perl 1.18-alpine mainline-alpine-perl mainline-alpine alpine-perl alpine 1.19.7-alpine-perl 1.19.7-alpine 1.19-alpine-perl 1.19-alpine 1-alpine-perl 1-alpine 1 perl mainline-perl mainline 1.19.7-perl 1.19.7 1.19-perl 1.19 1-perl stable-perl stable 1.19.6-perl 1.19.6 1.18.0-perl 1.18.0 1.18-perl 1.18 1.19.6-alpine-perl 1.19.6-alpine 1.19.5-perl 1.19.5-alpine-perl 1.19.5-alpine 1.19.5 1.19.4-perl 1.19.4 1.19.4-alpine-perl 1.19.4-alpine 1.19.3-alpine-perl 1.19.3-alpine 1.19.3-perl 1.19.3 1.19.2-perl 1.19.2 1.19.2-alpine-perl 1.19.2-alpine 1.19.1-perl 1.19.1 1.19.1-alpine-perl 1.19.1-alpine 1.19.0-perl 1.19.0 1.19.0-alpine-perl 1.19.0-alpine 1.17.10-perl 1.17.10 1.17-perl 1.17 1.17.10-alpine-perl 1.17.10-alpine 1.17-alpine-perl 1.17-alpine 1.16.1-perl 1.16.1 1.16-perl 1.16 1.17.9-perl 1.17.9 1.17.9-alpine-perl 1.17.9-alpine 1.17.8-perl 1.17.8 1.17.8-alpine-perl 1.17.8-alpine 1.16.1-alpine-perl 1.16.1-alpine 1.16-alpine-perl 1.16-alpine 1.17.7-perl 1.17.7-alpine-perl 1.17.7-alpine 1.17.7 1.17.6-perl 1.17.6 1.17.6-alpine-perl 1.17.6-alpine 1.17.5-alpine-perl 1.17.5-alpine 1.17.5-perl 1.17.5 1.17.4-alpine-perl 1.17.4-alpine 1.17.4-perl 1.17.4 1.17.3-perl 1.17.3 1.17.3-alpine-perl 1.17.3-alpine 1.17.2-perl 1.17.2 1.16.0-perl 1.16.0 1.17.2-alpine-perl 1.17.2-alpine 1.17.1-perl 1.17.1 1.17.1-alpine-perl 1.17.1-alpine 1.17.0-alpine-perl 1.17.0-alpine 1.16.0-alpine-perl 1.16.0-alpine 1.17.0-perl 1.17.0 1.15.12-alpine-perl 1.15.12-alpine 1.15-alpine-perl 1.15-alpine 1.15.12-perl 1.15.12 1.15-perl 1.15 1.15.11-alpine-perl 1.15.11-alpine 1.15.11-perl 1.15.11 1.14-alpine-perl 1.14.2-alpine-perl 1.14-alpine 1.14.2-alpine 1.15.10-alpine-perl 1.15.10-alpine 1.15.10-perl 1.15.10 1.14.2 1.14-perl 1.14.2-perl 1.14 1.15.9-alpine-perl 1.15.9-alpine 1.15.9-perl 1.15.9 1.15.8-perl 1.15.8 1.15.8-alpine-perl 1.15.8-alpine 1.15.7-alpine-perl 1.15.7-alpine 1.15.7-perl 1.15.7 1.14.1-alpine-perl 1.14.1-alpine 1.15.6-alpine-perl 1.15.6-alpine 1.14.1-perl 1.14.1 1.15.6-perl 1.15.6 1.15.5-perl 1.15.5-alpine-perl 1.14.0-perl 1.14.0 1.15.5 1.14.0-alpine-perl 1.14.0-alpine 1.15.5-alpine 1.15.4-alpine-perl 1.15.4-alpine 1.15.4-perl 1.15.4 1.15.3-alpine-perl 1.15.3-alpine 1.15.3-perl 1.15.3 1.15.2-alpine-perl 1.15.2 1.15.2-alpine 1.15.2-perl 1.15.1-alpine 1.15.1-perl 1.15.1 1.15.1-alpine-perl 1.15.0-alpine 1.15.0-perl 1.15.0 1.15.0-alpine-perl 1.13-alpine-perl 1.13.12-alpine-perl 1.13-alpine 1.13.12-alpine 1.13-perl 1.13.12-perl 1.13 1.13.12 1.12-perl 1.12.2-perl 1.12 1.12.2 1.13.11-alpine-perl 1.13.11-alpine 1.13.11-perl 1.13.11 1.13.10-alpine-perl 1.13.10-alpine 1.13.10-perl 1.13.10 1.12-alpine 1.13.9-perl 1.13.9 1.13.9-alpine 1.13.9-alpine-perl 1.13.8-perl 1.13.8 1.12-alpine-perl 1.12.2-alpine-perl 1.12.2-alpine 1.13.8-alpine-perl 1.13.8-alpine 1.13.7-perl 1.13.7 1.13.7-alpine-perl 1.13.7-alpine 1.13.6-perl 1.13.6 1.13.6-alpine-perl 1.13.6-alpine 1.12.1-alpine-perl 1.12.1-alpine 1.13.5-alpine-perl 1.13.5-alpine 1.12.1-perl 1.12.1 1.13.5-perl 1.13.5 1.13.3-alpine-perl 1.13.3-alpine 1.13.3-perl 1.13.3 1.12.0-alpine-perl 1.12.0-alpine 1.12.0-perl 1.12.0 1.13.2-alpine-perl 1.13.2-alpine 1.13.2-perl 1.13.2 1.13.1-alpine-perl 1.13.1-alpine 1.13.1-perl 1.13.1 1.13.0-perl 1.13.0-alpine-perl 1.13.0-alpine 1.13.0 1.11-alpine 1.11.13-alpine 1.11 1.11.13 1.10-alpine 1.10.3-alpine 1.10 1.10.3 1.11.12-alpine 1.11.12 1.11.10-alpine 1.11.10 1.11.9-alpine 1.11.9 1.10.2-alpine 1.10.2 1.11.8 1.11.8-alpine 1.11.7-alpine 1.11.7 1.11.6-alpine 1.11.6 1.11.5 1.11.5-alpine 1.10.1-alpine 1.10.1 1.11.4 1.11.4-alpine 1.11.3-alpine 1.11.3 1.11.1-alpine 1.11.0-alpine 1.11.0 1.11.1 1.10.0-alpine 1.10.0 1.9.15-alpine 1.9-alpine 1.9.15 1.9 1.8.1-alpine 1.8-alpine 1.9.14-alpine 1.8.1 1.8 1.9.14 1.9.12 1.9.11 1.9.10 1.9.9 1.9.8 1.9.7 1.7.8 1.7.7 1.7.6 1.7.9 1.7.10 1.7.1 1.7.5 1.9.6 1.9.5 1.9.4 1.9.3 1.9.2 1.9.0 1.9.1 1.7.11 1.7.12 1.7]
act, err := something([]string{"1.2.3", "1.2.300", "latest", "10.9.0", "10.0.0", "10.0.0-alpine", "7.80.9", "7.8.9", "4.5.6"})
func TestTags(t *testing.T) {
exp := "11.0.1"
act, err := Tags([]string{"1.2.3", "1.2.300", "latest", "10.9.0", "11.0.1", "10.0.0", "10.0.0-alpine", "7.80.9", "7.8.9", "4.5.6"})
if err != nil {
log.Fatal(err)
t.Error(err)
}
assert.Equal(t, "50.51.600", act)
assert.Equal(t, exp, act)
}
26 changes: 21 additions & 5 deletions pkg/dockerhub/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"strconv"

"github.com/030/dip/internal/sort"
"github.com/levigross/grequests"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
Expand Down Expand Up @@ -59,23 +60,38 @@ func LatestTagBasedOnRegex(official bool, latest string, image string) string {
}
log.Debug(tags)
log.Debug(len(tags))
log.Debugf("Regex: '%s'", latest)

r, err := regexp.Compile(latest)
if err != nil {
log.Fatal(err)
}

var latestTag string
var latestTags []string
for _, tag := range tags {
log.Debugf("Check whether: '%s', matches regex: '%s'", tag, latest)
if r.MatchString(tag) {
latestTag = r.FindString(tag)
break
latestTags = append(latestTags, r.FindString(tag))
}
}
tags = tags[:0] // reset slice to prevent that tags related to other image will be found on checking another image
if latestTag == "" {
log.Fatal("No tag found. Check whether regex is correct")
if latestTags == nil {
log.Fatal("No tags were found. Check whether regex is correct")
}
log.Debug(latestTags)
latestTag, err := sort.Tags(latestTags)
if err != nil {
log.Fatal(err)
}

log.Debugf("Latest tag: '%s'", latestTag)
return latestTag
}

func semantic(tag string) (bool, error) {
matched, err := regexp.Match("^([0-9]+\\.){2}[0-9]+", []byte(tag))
if err != nil {
return false, err
}
return matched, nil
}
11 changes: 11 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash -e
GITHUB_TAG="${GITHUB_TAG:-local}"
SHA512_CMD="${SHA512_CMD:-sha512sum}"
export DIP_DELIVERABLE="${DIP_DELIVERABLE:-dip}"

echo "GITHUB_TAG: '$GITHUB_TAG' DIP_DELIVERABLE: '${DIP_DELIVERABLE}'"
cd cmd/dip
go build -ldflags "-X main.Version=${GITHUB_TAG}" -o "${DIP_DELIVERABLE}"
$SHA512_CMD "${DIP_DELIVERABLE}" > "${DIP_DELIVERABLE}.sha512.txt"
chmod +x "${DIP_DELIVERABLE}"
cd ../..
28 changes: 28 additions & 0 deletions test/integration-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash -e

source ./scripts/build.sh
cd cmd/dip

echo -e "\nAdoptopenjdk"
./"${DIP_DELIVERABLE}" -image=adoptopenjdk -latest="14.*-jre-hotspot-bionic" -official

echo -e "\nAlpine"
./"${DIP_DELIVERABLE}" -image alpine -latest "(\d+\.){2}\d" --official | grep "[0-9]"

echo -e "\nMinio"
./"${DIP_DELIVERABLE}" -image minio/minio -latest "RELEASE\.2019.*" | grep "Cannot find the latest tag. Check whether the tags are semantic" | grep "[0-9]" || true

echo -e "\nNginx"
./"${DIP_DELIVERABLE}" -image=nginx -latest=1\..* -official | grep "[0-9]"

echo -e "\nSonatype Nexus3"
./"${DIP_DELIVERABLE}" -image=sonatype/nexus3 -latest=3\..* | grep "[0-9]"

echo -e "\nSonarqube"
./"${DIP_DELIVERABLE}" -image sonarqube -latest ".*-community$" -official | grep "[0-9]"

echo -e "\nTraefik"
./"${DIP_DELIVERABLE}" --image=traefik --latest="^v(\d+\.){1,2}\d+$" -official | grep "[0-9]"

echo -e "\nUbuntu"
./"${DIP_DELIVERABLE}" -image ubuntu -latest "^xenial.*" -official | grep "[0-9]" || true