|
| 1 | +name: Build Docker Image |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + description: 'Image version tag, without v prefix. Defaults to VERSION.' |
| 8 | + required: false |
| 9 | + type: string |
| 10 | + push: |
| 11 | + branches: [main] |
| 12 | + tags: |
| 13 | + - 'v*' |
| 14 | + paths: |
| 15 | + - 'rust/**' |
| 16 | + - 'docker/**' |
| 17 | + - 'VERSION' |
| 18 | + - '.github/workflows/docker-build.yml' |
| 19 | + release: |
| 20 | + types: [published] |
| 21 | + workflow_dispatch: |
| 22 | + inputs: |
| 23 | + version: |
| 24 | + description: 'Image version tag, without v prefix. Defaults to VERSION.' |
| 25 | + required: false |
| 26 | + type: string |
| 27 | + |
| 28 | +env: |
| 29 | + REGISTRY: ghcr.io |
| 30 | + IMAGE_NAME: openmined/bioscript |
| 31 | + |
| 32 | +jobs: |
| 33 | + build-and-push: |
| 34 | + runs-on: ubuntu-latest |
| 35 | + permissions: |
| 36 | + contents: read |
| 37 | + packages: write |
| 38 | + attestations: write |
| 39 | + id-token: write |
| 40 | + |
| 41 | + steps: |
| 42 | + - name: Checkout repository |
| 43 | + uses: actions/checkout@v4 |
| 44 | + with: |
| 45 | + submodules: recursive |
| 46 | + |
| 47 | + - name: Set up Docker Buildx |
| 48 | + uses: docker/setup-buildx-action@v3 |
| 49 | + |
| 50 | + - name: Log in to GitHub Container Registry |
| 51 | + uses: docker/login-action@v3 |
| 52 | + with: |
| 53 | + registry: ${{ env.REGISTRY }} |
| 54 | + username: ${{ github.actor }} |
| 55 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 56 | + |
| 57 | + - name: Resolve version |
| 58 | + id: version |
| 59 | + shell: bash |
| 60 | + env: |
| 61 | + VERSION_INPUT: ${{ inputs.version || '' }} |
| 62 | + run: | |
| 63 | + if [[ -n "${VERSION_INPUT}" ]]; then |
| 64 | + VERSION="${VERSION_INPUT#v}" |
| 65 | + elif [[ "${GITHUB_REF_TYPE}" == "tag" && "${GITHUB_REF_NAME}" == v* ]]; then |
| 66 | + VERSION="${GITHUB_REF_NAME#v}" |
| 67 | + else |
| 68 | + VERSION="$(tr -d '[:space:]' < VERSION)" |
| 69 | + fi |
| 70 | +
|
| 71 | + if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then |
| 72 | + echo "Invalid version: $VERSION" >&2 |
| 73 | + exit 1 |
| 74 | + fi |
| 75 | +
|
| 76 | + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" |
| 77 | + echo "Resolved version: ${VERSION}" |
| 78 | +
|
| 79 | + - name: Extract metadata for Docker |
| 80 | + id: meta |
| 81 | + uses: docker/metadata-action@v5 |
| 82 | + with: |
| 83 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 84 | + tags: | |
| 85 | + type=semver,pattern={{version}},value=v${{ steps.version.outputs.version }} |
| 86 | + type=semver,pattern={{major}}.{{minor}},value=v${{ steps.version.outputs.version }} |
| 87 | + type=semver,pattern={{major}},value=v${{ steps.version.outputs.version }} |
| 88 | + type=raw,value=${{ steps.version.outputs.version }} |
| 89 | + type=raw,value=latest,enable={{is_default_branch}} |
| 90 | + type=sha,prefix={{branch}}- |
| 91 | +
|
| 92 | + - name: Build and push Docker image |
| 93 | + id: build |
| 94 | + uses: docker/build-push-action@v6 |
| 95 | + with: |
| 96 | + context: . |
| 97 | + file: ./docker/Dockerfile |
| 98 | + platforms: linux/amd64,linux/arm64 |
| 99 | + push: true |
| 100 | + tags: ${{ steps.meta.outputs.tags }} |
| 101 | + labels: ${{ steps.meta.outputs.labels }} |
| 102 | + cache-from: type=gha |
| 103 | + cache-to: type=gha,mode=max |
| 104 | + |
| 105 | + - name: Generate artifact attestation |
| 106 | + uses: actions/attest-build-provenance@v2 |
| 107 | + continue-on-error: true |
| 108 | + with: |
| 109 | + subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 110 | + subject-digest: ${{ steps.build.outputs.digest }} |
| 111 | + push-to-registry: true |
0 commit comments