From 3312e2f43e71f7c8c63f93e69100574d1ba85675 Mon Sep 17 00:00:00 2001 From: huanghs Date: Mon, 21 Oct 2024 16:56:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20.gitea/actions/action-dock?= =?UTF-8?q?er-build-push/action.yml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../action-docker-build-push/action.yml | 130 ++++++++++++++++-- 1 file changed, 121 insertions(+), 9 deletions(-) diff --git a/.gitea/actions/action-docker-build-push/action.yml b/.gitea/actions/action-docker-build-push/action.yml index ca12606..f406f74 100644 --- a/.gitea/actions/action-docker-build-push/action.yml +++ b/.gitea/actions/action-docker-build-push/action.yml @@ -1,23 +1,135 @@ -name: 'Docker build and push' +name: 'action-docker-build-push' description: 'Setup docker, build image and push' +branding: + icon: package + color: orange inputs: + registry: + description: 'Server address of Docker registry. If not set then will default to Docker Hub' + required: false + username: + description: 'Username used to log against the Docker registry' + required: false password: description: 'Password or personal access token used to log against the Docker registry' required: false + context: + description: "Build's context is the set of files located in the specified PATH or URL" + required: false + default: '.' + file: + description: "Path to the Dockerfile" + required: false + default: 'Dockerfile' + tags: + description: "List/CSV of tags" + required: false + labels: + description: "List of labels" + required: false + platforms: + description: "List/CSV of target platforms for build" + required: false + push: + description: "Push is a shorthand for --output=type=registry" + required: false + default: 'true' + runs: using: 'composite' steps: - - run: "echo a: Set up QEMU" + - name: Set up Proxy env + run: | + echo "[$(date +'%Y-%m-%d %H:%M:%S')] ⚙️ Set up Proxy env" + if [ -n "${{ env.HTTP_PROXY }}" ]; then + echo "http_proxy=${{ env.HTTP_PROXY }}" >> $GITHUB_ENV + fi + if [ -n "${{ env.HTTPS_PROXY }}" ]; then + echo "https_proxy=${{ env.HTTPS_PROXY }}" >> $GITHUB_ENV + fi + if [ -n "${{ env.NO_PROXY }}" ]; then + echo "no_proxy=${{ env.NO_PROXY }}" >> $GITHUB_ENV + fi + echo "http_proxy=${{ env.http_proxy }}" + echo "https_proxy=${{ env.https_proxy }}" + echo "no_proxy=${{ env.no_proxy }}" - - run: | - sleep 10 - echo 'sleep 10 end' + - name: echo Set up QEMU + run: echo -e "\n[$(date +'%Y-%m-%d %H:%M:%S')] ⚙️ Set up QEMU" - - run: "echo b: Login to DockerHub" + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: echo Set up Docker BuildX + run: echo -e "\n[$(date +'%Y-%m-%d %H:%M:%S')] ⚙️ Set up Docker BuildX" + + - name: Set up Docker BuildX + uses: docker/setup-buildx-action@v3 + with: + driver-opts: | + env.http_proxy=${{ env.http_proxy }} + env.https_proxy=${{ env.https_proxy }} + "env.no_proxy='${{ env.no_proxy }}'" + + - name: echo Login to DockerHub + run: echo -e "\n[$(date +'%Y-%m-%d %H:%M:%S')] ⚙️ Login to DockerHub" + if: ${{ inputs.username != '' }} + + - name: Login to DockerHub + uses: docker/login-action@v3 if: ${{ inputs.password != '' }} + with: + registry: ${{ inputs.registry }} + username: ${{ inputs.username }} + password: ${{ inputs.password }} - - run: | - sleep 10 - echo 'c: Set up Docker BuildX' \ No newline at end of file + - name: echo Cache docker build + run: echo -e "\n[$(date +'%Y-%m-%d %H:%M:%S')] ⚙️ Cache docker build" + + - name: Cache docker build + uses: actions/cache@v3 + with: + path: /opt/docker-cache/.build-cache + key: ${{ runner.os }}-docker-build-cache + + - name: echo Build and push + run: echo -e "\n[$(date +'%Y-%m-%d %H:%M:%S')] ⚙️ Build and push" + + - name: Get docker meta + id: meta + run: | + if [ -n "${{ inputs.tags }}" ]; then + echo TAGS=$(echo "${{ inputs.tags }}" | tr '\n' ',' | sed 's/,$//') >> $GITHUB_OUTPUT + else + REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') + REPO_VERSION=$(echo ${{ github.ref }} | awk -F"/" '{print $3}' | awk -F"v" '{print (NF>1) ? $2 : $1}') + if [ -n "${{ inputs.registry }}" ]; then + TAGS="${{ inputs.registry }}/${{ inputs.username }}/$REPO_NAME:latest" + echo "TAGS=$TAGS,${{ inputs.registry }}/${{ inputs.username }}/$REPO_NAME:$REPO_VERSION" >> $GITHUB_OUTPUT + else + TAGS="${{ inputs.username }}/$REPO_NAME:latest" + echo "TAGS=$TAGS,${{ inputs.username }}/$REPO_NAME:$REPO_VERSION" >> $GITHUB_OUTPUT + fi + fi + + - name: echo Get docker meta + run: | + echo -e "\n[$(date +'%Y-%m-%d %H:%M:%S')] ⚙️ Get docker meta" + echo TAGS=${{ steps.meta.outputs.TAGS }} + + - name: Build and push + uses: docker/build-push-action@v4 + with: + context: ${{ inputs.context }} + file: ${{ inputs.file }} + platforms: ${{ inputs.platforms }} + push: ${{ inputs.push }} + tags: ${{ steps.meta.outputs.TAGS }} + labels: ${{ inputs.labels }} + cache-from: type=local,src=/opt/docker-cache/.build-cache + cache-to: type=local,dest=/opt/docker-cache/.build-cache,mode=max + build-args: | + "http_proxy='${{ env.http_proxy }}'" + "https_proxy='${{ env.https_proxy }}'" \ No newline at end of file