From 2b21df73e8bdb651d32fdd010af15fc653ce8ee8 Mon Sep 17 00:00:00 2001 From: Olli Janatuinen Date: Mon, 16 Sep 2024 13:54:25 +0000 Subject: [PATCH] Add Docker publish automation --- .github/docker-versions-to-skip | 0 .github/publish-docker.ps1 | 83 +++++++++++++++++++ .github/workflows/build-docker.yml | 3 +- .github/workflows/publish-docker.yml | 30 +++++++ .github/workflows/pull-request-validation.yml | 2 +- images/10-docker-26.1.4 | 1 + .../{10-docker-26.1.4 => docker}/Dockerfile | 0 .../{10-docker-26.1.4 => docker}/prebuild.sh | 3 - 8 files changed, 117 insertions(+), 5 deletions(-) create mode 100644 .github/docker-versions-to-skip create mode 100644 .github/publish-docker.ps1 create mode 100644 .github/workflows/publish-docker.yml create mode 120000 images/10-docker-26.1.4 rename images/{10-docker-26.1.4 => docker}/Dockerfile (100%) rename images/{10-docker-26.1.4 => docker}/prebuild.sh (69%) diff --git a/.github/docker-versions-to-skip b/.github/docker-versions-to-skip new file mode 100644 index 00000000..e69de29b diff --git a/.github/publish-docker.ps1 b/.github/publish-docker.ps1 new file mode 100644 index 00000000..43208335 --- /dev/null +++ b/.github/publish-docker.ps1 @@ -0,0 +1,83 @@ +$files = Get-ChildItem -Path ./d -Include "docker-*" -Exclude "docker-compose.yml" -Recurse +$filesShorted = $files | Sort-Object -Descending +$latestPublished = $filesShorted[0].Name -replace "docker-","" -replace ".yml","" + +[array]$dockerVersionsToSkip = Get-Content ./.github/docker-versions-to-skip + +Write-Host "Getting latest Docker release tag to publish" +$dockerReleases = Invoke-RestMethod -UseBasicParsing https://api.github.com/repos/moby/moby/releases +$nonPreviewReleases = $dockerReleases | Where-Object {$_.prerelease -eq $false} +$versionsShorted = $nonPreviewReleases.name | Sort-Object -Descending +$latestMajor = "" +$latestToPublish = "" +forEach($v in $versionsShorted) { + if ($latestMajor -eq "") { + $latestMajor = ($v -split "\.")[0] -replace "^v","" + } + if ($v -like "v$($latestMajor).*") { + continue + } + $latestToPublish = $v -replace "^v","" + break +} + +if ($latestToPublish -eq $latestPublished) { + Write-Host "Version $latestToPublish is latest and already published" + echo "CREATE_PR=false" >> $env:GITHUB_ENV + return +} +if ($latestToPublish -in $dockerVersionsToSkip) { + Write-Host "Version $latestToPublish is in skip list" + echo "CREATE_PR=false" >> $env:GITHUB_ENV + return +} +Write-Host "Version $latestToPublish is latest, trying to publish" +try { + $tarUrl = "https://download.docker.com/linux/static/stable/x86_64/docker-" + $latestToPublish + ".tgz" + Invoke-RestMethod -Uri $tarUrl -Method HEAD +} catch { + Write-Host "Package $tarUrl is not available" + $latestToPublish | Out-File ./.github/docker-versions-to-skip -Append + echo "CREATE_PR=true" >> $env:GITHUB_ENV + echo "PR_TITLE=Skip Docker $latestToPublish" >> $env:GITHUB_ENV + echo "DOCKER_VERSION=$latestToPublish" >> $env:GITHUB_ENV + return +} + +"- docker-$latestToPublish" | Out-File ./index.yml -Append + +$dockerYML = @' +docker: + image: ${REGISTRY_DOMAIN}/burmilla/os-docker: +'@ + +$dockerYML += $latestToPublish +$dockerYML += @' +${SUFFIX} + command: ros user-docker + environment: + - HTTP_PROXY + - HTTPS_PROXY + - NO_PROXY + labels: + io.rancher.os.scope: system + io.rancher.os.after: console + net: host + pid: host + ipc: host + uts: host + privileged: true + restart: always + volumes_from: + - all-volumes + volumes: + - /sys:/host/sys + - /var/lib/system-docker:/var/lib/system-docker:shared + +'@ +$dockerYML | Out-File "./d/docker-$latestToPublish.yml" -Append +ln -s docker "./images/10-docker-$latestToPublish" + +echo "CREATE_PR=true" >> $env:GITHUB_ENV +echo "PR_TITLE=Add Docker $latestToPublish" >> $env:GITHUB_ENV +echo "DOCKER_VERSION=$latestToPublish" >> $env:GITHUB_ENV diff --git a/.github/workflows/build-docker.yml b/.github/workflows/build-docker.yml index 8d83fc8a..f2434b6d 100644 --- a/.github/workflows/build-docker.yml +++ b/.github/workflows/build-docker.yml @@ -4,12 +4,13 @@ on: push: branches: - v2.0.1 + - v2.0.x jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 with: fetch-depth: '0' diff --git a/.github/workflows/publish-docker.yml b/.github/workflows/publish-docker.yml new file mode 100644 index 00000000..fbad205c --- /dev/null +++ b/.github/workflows/publish-docker.yml @@ -0,0 +1,30 @@ +name: publish-docker + +on: + workflow_dispatch: + schedule: + - cron: '38 01 * * 1,3' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Publish Docker + shell: pwsh + run: . ./.github/publish-docker.ps1 + + - name: Create Pull Request + if: env.CREATE_PR == 'true' + uses: peter-evans/create-pull-request@v7 + with: + base: v2.0.x + branch: docker-${{ env.DOCKER_VERSION }} + title: ${{ env.PR_TITLE }} + body: ${{ env.PR_TITLE }} + commit-message: ${{ env.PR_TITLE }} + author: "PR Bot " + add-paths: . diff --git a/.github/workflows/pull-request-validation.yml b/.github/workflows/pull-request-validation.yml index b16da742..2a457d28 100644 --- a/.github/workflows/pull-request-validation.yml +++ b/.github/workflows/pull-request-validation.yml @@ -5,7 +5,7 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Test with dapper run: | diff --git a/images/10-docker-26.1.4 b/images/10-docker-26.1.4 new file mode 120000 index 00000000..6d0eac4b --- /dev/null +++ b/images/10-docker-26.1.4 @@ -0,0 +1 @@ +docker \ No newline at end of file diff --git a/images/10-docker-26.1.4/Dockerfile b/images/docker/Dockerfile similarity index 100% rename from images/10-docker-26.1.4/Dockerfile rename to images/docker/Dockerfile diff --git a/images/10-docker-26.1.4/prebuild.sh b/images/docker/prebuild.sh similarity index 69% rename from images/10-docker-26.1.4/prebuild.sh rename to images/docker/prebuild.sh index 1df6d9ce..afc5c778 100755 --- a/images/10-docker-26.1.4/prebuild.sh +++ b/images/docker/prebuild.sh @@ -6,7 +6,6 @@ ARCH=$2 if [ "$ARCH" == "amd64" ]; then DOCKERARCH="x86_64" URL="https://download.docker.com/linux/static/stable/${DOCKERARCH}/docker-${VERSION}.tgz" - #ROOTLESS_URL="https://download.docker.com/linux/static/stable/${DOCKERARCH}/docker-rootless-extras-${VERSION}.tgz" COMPLETION_URL="https://raw.githubusercontent.com/docker/cli/v${VERSION}/contrib/completion/bash/docker" fi @@ -14,7 +13,5 @@ DEST="./images/10-docker-${VERSION}${SUFFIX}" mkdir -p $DEST curl -sL ${URL} | tar xzf - -C $DEST -#curl -sL ${ROOTLESS_URL} | tar xzf - -C $DEST curl -sL -o $DEST/docker/completion ${COMPLETION_URL} mv $DEST/docker $DEST/engine -#mv $DEST/docker-rootless-extras/* $DEST/engine