Files
ubicloud/.github/workflows/build.yml
Enes Cakir de5411cbd4 Set workflow job permissions explicitly
CodeQL has started scanning GitHub Actions workflows as well. It's not a
major issue, but it's good to follow best practices.

https://github.com/ubicloud/ubicloud/security/code-scanning/11

    Workflow does not contain permissions

    If a GitHub Actions job or workflow has no explicit permissions set,
    then the repository permissions are used. Repositories created under
    organizations inherit the organization permissions. The
    organizations or repositories created before February 2023 have the
    default permissions set to read-write. Often these permissions do
    not adhere to the principle of least privilege and can be reduced to
    read-only, leaving the write permission only to a specific types as
    issues: write or pull-requests: write.
2025-03-11 13:15:37 +03:00

128 lines
3.5 KiB
YAML

name: Build
permissions:
contents: read
on:
workflow_dispatch:
inputs:
push_image:
description: 'Push the image to Docker Hub'
default: false
type: boolean
push:
branches:
- 'main'
tags:
- 'v*'
env:
IMAGE_NAME: ubicloud/ubicloud
jobs:
docker:
name: Docker ${{ matrix.platform }}
runs-on: ${{ matrix.runner }}
strategy:
fail-fast: false
matrix:
include:
- runner: ubicloud-standard-8
platform: linux/amd64
- runner: ubicloud-standard-8-arm
platform: linux/arm64
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=sha,prefix=
type=ref,event=branch
type=raw,value=latest,enable={{is_default_branch}}
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push by digest
id: build
uses: docker/build-push-action@v6
with:
context: .
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=ubicloud-${{ matrix.runner }}
cache-to: type=gha,mode=max,scope=ubicloud-${{ matrix.runner }}
outputs: type=image,name=${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
- name: Export digest
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
- name: Upload digest
if: ${{ inputs.push_image }}
uses: actions/upload-artifact@v4
with:
name: digests-${{ matrix.runner }}
path: /tmp/digests/*
if-no-files-found: error
retention-days: 1
merge:
if: ${{ inputs.push_image }}
runs-on: ubicloud
needs:
- docker
steps:
- name: Download digests
uses: actions/download-artifact@v4
with:
pattern: digests-*
merge-multiple: true
path: /tmp/digests
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=sha,prefix=
type=ref,event=branch
type=raw,value=latest,enable={{is_default_branch}}
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.IMAGE_NAME }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}