|
1 | | -name: Publish Docker image to ghcr.io |
| 1 | +name: Publish Docker Image |
| 2 | + |
2 | 3 | on: |
3 | | - push: |
4 | | - tags: |
5 | | - - "*" |
| 4 | + push: |
| 5 | + branches: [ main, master ] |
| 6 | + tags: [ '*', '!*-*' ] # Match v1.2.3 but not v1.2.3-rc1 |
| 7 | + pull_request: |
| 8 | + branches: [ main, master ] |
| 9 | + workflow_dispatch: |
| 10 | + inputs: |
| 11 | + version: |
| 12 | + description: 'Version to build (e.g., v1.2.3)' |
| 13 | + required: false |
| 14 | + |
| 15 | +# Set job-level environment variables |
| 16 | +env: |
| 17 | + REGISTRY: ghcr.io |
| 18 | + IMAGE_NAME: ${{ github.repository }} |
| 19 | + DOCKERFILE_PATH: ./Dockerfile |
| 20 | + BUILDX_CACHE_DIR: /tmp/.buildx-cache |
| 21 | + BUILDX_CACHE_KEY: ${{ github.ref }}-${{ github.sha }} |
| 22 | + |
6 | 23 | jobs: |
7 | | - push_to_registries: |
8 | | - name: Build and publish Docker image |
9 | | - runs-on: ubuntu-latest |
10 | | - steps: |
11 | | - - name: Check out the repo |
12 | | - uses: actions/checkout@v3 |
13 | | - - name: Set up QEMU |
14 | | - uses: docker/setup-qemu-action@v2 |
15 | | - - name: Set up Docker Buildx |
16 | | - uses: docker/setup-buildx-action@v2 |
17 | | - - name: Prepare |
18 | | - # In this preparation step, a few configurations are made |
19 | | - # according to tags that will be used to export the image |
20 | | - # for Docker Hub, as well as the name of the image itself |
21 | | - id: prep |
22 | | - run: | |
23 | | - DOCKER_IMAGE=ghcr.io/msk-access/genotype_variants |
24 | | - VERSION=noop |
25 | | - if [ "${{ github.event_name }}" = "schedule" ]; then |
26 | | - VERSION=nightly |
27 | | - elif [[ $GITHUB_REF == refs/tags/* ]]; then |
28 | | - VERSION=${GITHUB_REF#refs/tags/} |
29 | | - elif [[ $GITHUB_REF == refs/heads/* ]]; then |
30 | | - VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -r 's#/+#-#g') |
31 | | - fi |
32 | | - TAGS="${DOCKER_IMAGE}:${VERSION}" |
33 | | - if [[ $VERSION =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then |
34 | | - MINOR=${VERSION%.*} |
35 | | - MAJOR=${MINOR%.*} |
36 | | - TAGS="$TAGS,${DOCKER_IMAGE}:latest" |
37 | | - elif [[ $VERSION =~ ^v[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then |
38 | | - VERSION=$(echo ${VERSION#v}) |
39 | | - TAGS="${DOCKER_IMAGE}:${VERSION}" |
40 | | - elif [ "${{ github.event_name }}" = "push" ]; then |
41 | | - TAGS="$TAGS,${DOCKER_IMAGE}:sha-${GITHUB_SHA::8}" |
42 | | - fi |
43 | | - echo ::set-output name=version::${VERSION} |
44 | | - echo ::set-output name=tags::${TAGS} |
45 | | - - name: Login to GitHub Container Registry |
46 | | - #if: github.event_name != 'pull_request' |
47 | | - uses: docker/login-action@v2 |
48 | | - with: |
49 | | - registry: ghcr.io |
50 | | - username: ${{ github.repository_owner }} |
51 | | - password: ${{ secrets.RS_PAT }} |
52 | | - - name: Push to GitHub Packages |
53 | | - uses: docker/build-push-action@v3 |
54 | | - with: |
55 | | - context: . |
56 | | - file: ./Dockerfile |
57 | | - push: true |
58 | | - tags: ${{ steps.prep.outputs.tags }} |
59 | | - build-args: | |
60 | | - GENOTYPE_VARIANTS_VERSION=${{ steps.prep.outputs.version }} |
61 | | - labels: | |
62 | | - org.opencontainers.image.title=${{ github.event.repository.name }} |
63 | | - org.opencontainers.image.description=${{ github.event.repository.description }} |
64 | | - org.opencontainers.image.version=${{ steps.prep.outputs.version }} |
| 24 | + build-and-push: |
| 25 | + name: Build and Push Docker Image |
| 26 | + runs-on: ubuntu-latest |
| 27 | + permissions: |
| 28 | + contents: read |
| 29 | + packages: write |
| 30 | + security-events: write # For Trivy SARIF upload |
| 31 | + |
| 32 | + strategy: |
| 33 | + fail-fast: false |
| 34 | + matrix: |
| 35 | + platform: [linux/amd64, linux/arm64] |
| 36 | + |
| 37 | + steps: |
| 38 | + - name: Checkout repository |
| 39 | + uses: actions/checkout@v4 |
| 40 | + with: |
| 41 | + fetch-depth: 0 # Needed for version detection |
| 42 | + submodules: recursive |
| 43 | + |
| 44 | + - name: Set up QEMU |
| 45 | + uses: docker/setup-qemu-action@v3 |
| 46 | + with: |
| 47 | + platforms: arm64,amd64 |
| 48 | + |
| 49 | + - name: Set up Docker Buildx |
| 50 | + uses: docker/setup-buildx-action@v3 |
| 51 | + with: |
| 52 | + install: true |
| 53 | + driver-opts: | |
| 54 | + image=moby/buildkit:latest |
| 55 | + network=host |
| 56 | + buildkitd-config-inline: | |
| 57 | + [worker.oci] |
| 58 | + max-parallelism = 4 |
| 59 | + |
| 60 | + - name: Get current date |
| 61 | + id: date |
| 62 | + run: echo "date=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT |
| 63 | + |
| 64 | + - name: Extract metadata (tags, labels) for Docker |
| 65 | + id: meta |
| 66 | + uses: docker/metadata-action@v5 |
| 67 | + with: |
| 68 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 69 | + tags: | |
| 70 | + type=schedule,pattern=nightly |
| 71 | + type=ref,event=branch |
| 72 | + type=ref,event=pr |
| 73 | + type=semver,pattern={{version}} |
| 74 | + type=semver,pattern={{major}}.{{minor}} |
| 75 | + type=semver,pattern={{major}} |
| 76 | + type=sha,format=long,prefix=sha- |
| 77 | + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }} |
| 78 | + flavor: | |
| 79 | + latest=${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }} |
| 80 | + labels: | |
| 81 | + org.opencontainers.image.title=${{ github.event.repository.name }} |
| 82 | + org.opencontainers.image.description=${{ github.event.repository.description }} |
| 83 | + org.opencontainers.image.url=${{ github.server_url }}/${{ github.repository }} |
| 84 | + org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} |
| 85 | + org.opencontainers.image.created=${{ steps.date.outputs.date }} |
| 86 | + org.opencontainers.image.revision=${{ github.sha }} |
| 87 | + org.opencontainers.image.version=${{ github.ref_name }} |
| 88 | +
|
| 89 | + - name: Log in to GitHub Container Registry |
| 90 | + if: github.event_name != 'pull_request' |
| 91 | + uses: docker/login-action@v3 |
| 92 | + with: |
| 93 | + registry: ${{ env.REGISTRY }} |
| 94 | + username: ${{ github.actor }} |
| 95 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 96 | + |
| 97 | + - name: Set up Docker Buildx cache |
| 98 | + uses: actions/cache@v3 |
| 99 | + with: |
| 100 | + path: ${{ env.BUILDX_CACHE_DIR }} |
| 101 | + key: buildx-${{ runner.os }}-${{ matrix.platform }}-${{ env.BUILDX_CACHE_KEY }} |
| 102 | + restore-keys: | |
| 103 | + buildx-${{ runner.os }}-${{ matrix.platform }}- |
| 104 | + buildx-${{ runner.os }}- |
| 105 | +
|
| 106 | + - name: Build and push Docker image |
| 107 | + uses: docker/build-push-action@v5 |
| 108 | + with: |
| 109 | + context: . |
| 110 | + file: ${{ env.DOCKERFILE_PATH }} |
| 111 | + push: ${{ github.event_name != 'pull_request' }} |
| 112 | + tags: ${{ steps.meta.outputs.tags }} |
| 113 | + labels: ${{ steps.meta.outputs.labels }} |
| 114 | + platforms: ${{ matrix.platform }} |
| 115 | + cache-from: type=local,src=${{ env.BUILDX_CACHE_DIR }} |
| 116 | + cache-to: type=local,dest=${{ env.BUILDX_CACHE_DIR }}-new,mode=max |
| 117 | + build-args: | |
| 118 | + BUILDKIT_INLINE_CACHE=1 |
| 119 | + GENOTYPE_VARIANTS_VERSION=${{ github.ref_name }} |
| 120 | + BUILD_VERSION=${{ github.ref_name }} |
| 121 | + BUILD_DATE=${{ steps.date.outputs.date }} |
| 122 | + VCS_REF=${{ github.sha }} |
| 123 | + provenance: ${{ github.event_name != 'pull_request' }} |
| 124 | + sbom: true |
| 125 | + secrets: | |
| 126 | + GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} |
| 127 | +
|
| 128 | + - name: Run Trivy vulnerability scanner |
| 129 | + if: github.event_name != 'pull_request' |
| 130 | + uses: aquasecurity/trivy-action@master |
| 131 | + with: |
| 132 | + image-ref: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.meta.outputs.version }} |
| 133 | + format: 'sarif' |
| 134 | + output: 'trivy-results.sarif' |
| 135 | + severity: 'CRITICAL,HIGH' |
| 136 | + ignore-unfixed: true |
| 137 | + vuln-type: 'os,library' |
| 138 | + exit-code: '1' |
| 139 | + timeout: '5m' |
| 140 | + |
| 141 | + - name: Upload Trivy scan results to GitHub Security tab |
| 142 | + if: always() && (github.event_name != 'pull_request') |
| 143 | + uses: github/codeql-action/upload-sarif@v2 |
| 144 | + with: |
| 145 | + sarif_file: 'trivy-results.sarif' |
| 146 | + category: 'container-scan' |
| 147 | + |
| 148 | + - name: Update Buildx cache |
| 149 | + if: github.event_name != 'pull_request' |
| 150 | + run: | |
| 151 | + rm -rf ${{ env.BUILDX_CACHE_DIR }} |
| 152 | + mv ${{ env.BUILDX_CACHE_DIR }}-new ${{ env.BUILDX_CACHE_DIR }} |
| 153 | + echo "Updated build cache" |
| 154 | +
|
| 155 | + - name: Show image details |
| 156 | + if: always() |
| 157 | + run: | |
| 158 | + echo "Built image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}" |
| 159 | + echo "Tags: ${{ steps.meta.outputs.tags }}" |
| 160 | + echo "Labels: ${{ steps.meta.outputs.labels }}" |
0 commit comments