From fa161c85715d2a234aed7ca738f2e318162b46a0 Mon Sep 17 00:00:00 2001 From: Tho Ho Date: Wed, 15 Nov 2023 16:37:46 +0700 Subject: [PATCH] Move to Github Actions build docker crushftp-source (#7) * Add github actions * Build image * Provide imageTag * quote string * Debug parameter * Debug params, fix missing quote string * Cleanup * supports WhatIf * Build on pull request * Build on pull request --- .github/workflows/source.yml | 51 +++++++++++++++++++++ crushftp-source/README.md | 2 +- crushftp-source/docker-build.ps1 | 74 ++++++++++++++++++++++++++++++ crushftp-source/hooks/build | 77 -------------------------------- 4 files changed, 126 insertions(+), 78 deletions(-) create mode 100644 .github/workflows/source.yml create mode 100644 crushftp-source/docker-build.ps1 delete mode 100644 crushftp-source/hooks/build diff --git a/.github/workflows/source.yml b/.github/workflows/source.yml new file mode 100644 index 0000000..e50b98a --- /dev/null +++ b/.github/workflows/source.yml @@ -0,0 +1,51 @@ +name: CrushFTP Source + +on: + push: + branches: ["dev-source-*", "source/*", "dev-source/*"] + tags: ["source-*.*.*"] + pull_request: + branches: [main] + +jobs: + build: + strategy: + fail-fast: false + name: crushftp-source + runs-on: ubuntu-latest + env: + CRUSHFTP_VERSION: "10.5.4" + IS_TAG: ${{ startsWith(github.ref, 'refs/tags/') }} + SKIP_DOCKER: false + steps: + - uses: actions/checkout@v3 + + - name: Set up Docker Buildx + if: ${{ success() && env.SKIP_DOCKER != 'true' }} + uses: docker/setup-buildx-action@v2 + + - name: Login to Docker Hub + if: ${{ success() && env.SKIP_DOCKER != 'true' && github.event_name != 'pull_request' }} + uses: docker/login-action@v2 + with: + username: "${{ secrets.DOCKER_HUB_USERNAME }}" + password: "${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}" + + - name: Build and push + if: ${{ success() && env.SKIP_DOCKER != 'true' }} + shell: pwsh + working-directory: ./crushftp-source + run: | + $pattern = '^source-(?\d+\.\d+(\.\d+)?(_.+)?)$' + $crushFtpVersion = '${{ env.CRUSHFTP_VERSION }}' + $imageTag = '${{ github.ref_name }}'.Replace('/','-') + $whatIf = '${{ github.event_name == 'pull_request' }}' -eq $true + if ('${{ env.IS_TAG }}' -eq $true) { + $repos += @('${{ vars.DOCKER_HUB_NETLAH_SOURCE_REPOS }}') + if ('${{ github.ref_name }}' -match $pattern) { + $crushFtpVersion = '${{ github.ref_name }}' -replace $pattern,'${version}' + $imageTag = $crushFtpVersion + } + } + $repos += @('${{ secrets.DOCKER_HUB_SOURCE_REPOS }}') + ./docker-build.ps1 -dockerRepository $repos -crushFtpVersion $crushFtpVersion -imageTag $imageTag -WhatIf:$whatIf -Verbose diff --git a/crushftp-source/README.md b/crushftp-source/README.md index 7ed6727..d621e40 100644 --- a/crushftp-source/README.md +++ b/crushftp-source/README.md @@ -5,7 +5,7 @@ This intermediate docker image contains crushftp.zip for next docker build image ## Docker Specifications - Base: Alpine 3.13 Linux -- CrushFTP 10 (v10.1.0_62) +- CrushFTP 10 (v10.5.4) - Repository: [hub.docker.com/r/netlah/crushftp](hub.docker.com/r/netlah/crushftp) - Docker: [hub.docker.com/r/netlah/crushftp-source](hub.docker.com/r/netlah/crushftp-source) diff --git a/crushftp-source/docker-build.ps1 b/crushftp-source/docker-build.ps1 new file mode 100644 index 0000000..1080786 --- /dev/null +++ b/crushftp-source/docker-build.ps1 @@ -0,0 +1,74 @@ +[CmdletBinding()] +param ( + [Parameter(Mandatory = $true)] [string] $imageTag = 'dev', + [Parameter(Mandatory = $false)] [string[]] $dockerRepository, + [Parameter(Mandatory = $false)] [string] $baseImageRepos, + [Parameter(Mandatory = $false)] [string] $sourceMethod, + [Parameter(Mandatory = $false)] [string] $sourceZip, + [Parameter(Mandatory = $false)] [string] $crushFtpVersion, + [Parameter(Mandatory = $false)] [switch] $WhatIf +) + +$dockerImages = @() +foreach ($dockerRepos in $dockerRepository) { + $dockerImages += @("$($dockerRepos):$($imageTag)") +} + +$params = @('build', '.') + +if ($baseImageRepos) { + Write-Verbose "REPO: $baseImageRepos" + $params += @('--build-arg', "REPO=$baseImageRepos") +} + +if ($sourceMethod) { + Write-Verbose "SOURCE_METHOD: $sourceMethod" + $params += @('--build-arg', "SOURCE_METHOD=$sourceMethod") +} + +if ($sourceZip) { + Write-Verbose "SOURCE_ZIP: $sourceZip" + $params += @('--build-arg', "SOURCE_ZIP=$sourceZip") +} + +if ($crushFtpVersion) { + Write-Verbose "CRUSHFTP_VERSION: $crushFtpVersion" + $params += @('--build-arg', "CRUSHFTP_VERSION=$crushFtpVersion") +} + +$params += @('--pull', '--progress=plain') + +Write-Output "Docker images: $dockerImages" +[bool]$isGitHubAction = "$Env:GITHUB_ACTIONS" -eq $true +if (!$isGitHubAction) { + foreach ($dockerImage in $dockerImages) { + $params += @("--cache-from=$($dockerImage)") + } +} + +foreach ($dockerImage in $dockerImages) { + $params += @("--tag=$($dockerImage)") +} + +Write-Verbose "Execute: docker $params" +docker @params +if (!$?) { + $saveLASTEXITCODE = $LASTEXITCODE + Write-Error "docker build failed (exit=$saveLASTEXITCODE)" + exit $saveLASTEXITCODE +} + +if ($WhatIf) { + Write-Host "Skipped pushing docker image" +} +elseif ($dockerImages) { + Write-Host "Pushing docker images" + foreach ($dockerImage in $dockerImages) { + docker push $dockerImage + if (!$?) { + $saveLASTEXITCODE = $LASTEXITCODE + Write-Error "docker push failed (exit=$saveLASTEXITCODE)" + exit $saveLASTEXITCODE + } + } +} diff --git a/crushftp-source/hooks/build b/crushftp-source/hooks/build deleted file mode 100644 index 7f5814d..0000000 --- a/crushftp-source/hooks/build +++ /dev/null @@ -1,77 +0,0 @@ -#!/bin/bash - -IMAGE_NAME1="${DOCKER_REPO}:${SOURCE_COMMIT:0:7}" -IMAGE_DEV="${DOCKER_REPO}:dev" -IMAGE_LATEST="${DOCKER_REPO}:latest" - -cat <DOCKER_TAG: ${DOCKER_TAG}" - DOCKER_BUID="$DOCKER_BUID --build-arg CRUSHFTP_VERSION=$DOCKER_TAG" -fi - -if [[ "${DOCKER_TAG}" =~ ^[0-9]+(.[0-9]+){0,3}(_[0-9]+)?$ ]] ; then - if [[ -n "${REPO2}" ]]; then - IMAGE_NAME2="${REPO2}:${DOCKER_TAG}" - DOCKER_BUID="$DOCKER_BUID -t $IMAGE_NAME2" - fi - - if [[ -n "${IMAGE_NAME3}" ]]; then - USED_IMAGE_NAME3=1 - DOCKER_BUID="$DOCKER_BUID -t $IMAGE_NAME3" - fi -fi - -echo "Docker build=> ${DOCKER_BUID}" -eval $DOCKER_BUID - -ret_val=$? -if [[ ${ret_val} -eq 0 ]]; then - echo "Pushing=> ${IMAGE_NAME1} for ${SOURCE_COMMIT}" - docker push ${IMAGE_NAME1} - - if [[ -n "${IMAGE_NAME2}" ]]; then - echo "Pushing=> ${IMAGE_NAME2}" - docker push ${IMAGE_NAME2} - fi - - if [[ -n "${USED_IMAGE_NAME3}" ]]; then - echo "Pushing=> ${IMAGE_NAME3}" - docker push ${IMAGE_NAME3} - fi -else - exit ret_val -fi