Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build and push rootless docker container #8572

Merged
merged 5 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/build-test-docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ name: build-test-docker
on:
workflow_dispatch:
inputs:
docker-flavor:
required: true
type: string
description: A multi-line string in the format accepted by docker metadata tag action for the flavor of the image
docker-tags:
required: true
type: string
Expand All @@ -23,13 +27,21 @@ on:
type: string
description: Dockerfile to build
default: Dockerfile
target:
required: true
type: string
description: Dockerfile target to build
enable-tests:
required: true
type: boolean
description: Whether or not to run validation on the built image

workflow_call:
inputs:
docker-flavor:
required: true
type: string
description: A multi-line string in the format accepted by docker metadata tag action for the flavor of the image
docker-tags:
required: true
type: string
Expand All @@ -46,6 +58,10 @@ on:
required: true
type: string
description: Dockerfile to build
target:
cgdolan marked this conversation as resolved.
Show resolved Hide resolved
required: true
type: string
description: Dockerfile target to build
enable-tests:
required: true
type: boolean
Expand All @@ -71,6 +87,7 @@ jobs:
uses: docker/metadata-action@v4
with:
images: ${{ inputs.repository-name }}
flavor: ${{ inputs.docker-flavor }}
tags: ${{ inputs.docker-tags }}
- uses: depot/setup-action@v1
- name: Build image
Expand All @@ -83,6 +100,7 @@ jobs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
file: ${{ inputs.file }}
target: ${{ inputs.target }}
buildx-fallback: true
- name: Load image
if: ${{ inputs.enable-tests }}
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,33 @@ jobs:
secrets: inherit
needs: [inputs]
with:
docker-flavor: |
latest=auto
docker-tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
repository-name: ${{ github.repository }}
artifact-name: image-release
file: Dockerfile
target: semgrep-cli
enable-tests: true

build-test-docker-nonroot:
uses: ./.github/workflows/build-test-docker.yaml
secrets: inherit
with:
docker-flavor: |
latest=auto
suffix=-nonroot
docker-tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
repository-name: ${{ github.repository }}
artifact-name: image-release-nonroot
file: Dockerfile
target: nonroot
enable-tests: false

build-test-osx-x86:
uses: ./.github/workflows/build-test-osx-x86.yaml
secrets: inherit
Expand Down Expand Up @@ -128,6 +147,7 @@ jobs:
needs:
[
build-test-docker,
build-test-docker-nonroot,
build-test-manylinux-x86,
build-test-manylinux-aarch64,
build-test-osx-x86,
Expand All @@ -146,6 +166,15 @@ jobs:
repository-name: ${{ github.repository }}
dry-run: ${{ needs.inputs.outputs.dry-run == 'true' }}

push-docker-nonroot:
needs: [wait-for-build-test, inputs]
uses: ./.github/workflows/push-docker.yaml
secrets: inherit
with:
artifact-name: image-release-nonroot
repository-name: ${{ github.repository }}
dry-run: ${{ needs.inputs.outputs.dry-run == 'true' }}

upload-wheels:
name: Upload Wheels to PyPI
runs-on: ubuntu-latest
Expand Down
33 changes: 33 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ jobs:
uses: ./.github/workflows/build-test-docker.yaml
secrets: inherit
with:
docker-flavor: |
latest=auto
docker-tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
Expand All @@ -315,6 +317,7 @@ jobs:
artifact-name: image-test
repository-name: ${{ github.repository }}
file: Dockerfile
target: semgrep-cli
enable-tests: true

push-docker:
Expand All @@ -327,6 +330,36 @@ jobs:
repository-name: ${{ github.repository }}
dry-run: false

build-test-docker-nonroot:
uses: ./.github/workflows/build-test-docker.yaml
secrets: inherit
with:
docker-flavor: |
latest=auto
suffix=-nonroot
docker-tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=ref,event=pr
type=ref,event=branch
type=sha,event=branch
type=edge
artifact-name: image-test-nonroot
repository-name: ${{ github.repository }}
file: Dockerfile
target: nonroot
enable-tests: false

push-docker-nonroot:
needs: [build-test-docker-nonroot]
uses: ./.github/workflows/push-docker.yaml
if: github.ref == 'refs/heads/develop' || (github.actor != 'dependabot[bot]' && !(github.event.pull_request.head.repo.full_name != github.repository))
secrets: inherit
with:
artifact-name: image-test-nonroot
repository-name: ${{ github.repository }}
dry-run: false

test-semgrep-pro:
needs: [build-test-docker, push-docker]
uses: ./.github/workflows/test-semgrep-pro.yaml
Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ RUN adduser -D -u 1000 -h /home/semgrep semgrep \

# Disabling defaulting to the user semgrep for now
# We can set it by default once we fix the circle ci workflows
# See nonroot build stage below.
#USER semgrep

# Workaround for rootless containers as git operations may fail due to dubious
Expand All @@ -232,3 +233,10 @@ RUN printf "[safe]\n directory = /src" > ~semgrep/.gitconfig && \
# to interactively explore the docker image.
CMD ["semgrep", "--help"]
LABEL maintainer="support@semgrep.com"

cgdolan marked this conversation as resolved.
Show resolved Hide resolved
# Additional build stage that sets a non-root user.
# Can't make this the default in semgrep-cli stage because of permissions errors
# on the mounted volume when using instructions for running semgrep with docker:
# `docker run -v "${PWD}:/src" -i returntocorp/semgrep semgrep`
FROM semgrep-cli AS nonroot
USER semgrep