Merge pull request #9 from zhuoyuan-liu/distributed-query #7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: A Build, test and push osctrl images | |
on: | |
push: | |
branches: | |
- main | |
env: | |
GOLANG_VERSION: 1.23.1 | |
OSQUERY_VERSION: 5.14.1 | |
OSCTRL_ADMIN_IMAGE: ${{ secrets.CONTAINER_REGISTRY }}/${{ secrets.DOCKER_HUB_ORG }}/osctrl-admin | |
jobs: | |
build-prep: | |
runs-on: ubuntu-latest | |
name: Prepare for building images | |
environment: dev | |
outputs: | |
dev-tag: ${{ steps.prep.outputs.BUILD_ID }} | |
steps: | |
- name: Generate build ID | |
id: prep | |
run: | | |
sha=${GITHUB_SHA::8} | |
ts=$(date +%s) | |
echo "BUILD_ID=dev-${sha}-${ts}" >> $GITHUB_OUTPUT | |
build: | |
needs: build-prep | |
runs-on: ubuntu-22.04 | |
name: Build docker images for tls, api, and cli | |
environment: dev | |
strategy: | |
matrix: | |
components: [tls, api, cli] | |
steps: | |
- name: Checkout | |
id: checkout | |
uses: actions/checkout@v4 | |
- uses: actions/setup-go@v5 | |
with: | |
go-version-file: 'go.mod' | |
- name: Build docker image | |
uses: ./.github/actions/build/ko-build | |
with: | |
osctrl_component: ${{ matrix.components }} | |
build_id: ${{needs.build-prep.outputs.dev-tag}} | |
cr_username: ${{ secrets.DOCKER_HUB_USERNAME }} | |
cr_access_token: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | |
cr_name: ${{ secrets.CONTAINER_REGISTRY }} | |
cr_repo: ${{ secrets.DOCKER_HUB_ORG }} | |
build_and_test: | |
name: Build binaries for osctrl admin | |
runs-on: ubuntu-22.04 | |
strategy: | |
matrix: | |
components: ['admin'] | |
goos: ['linux'] | |
goarch: ['amd64', 'arm64'] | |
steps: | |
########################### Checkout code ########################### | |
- name: Checkout code | |
uses: actions/[email protected] | |
with: | |
fetch-depth: 2 | |
########################### Generate SHA1 commit hash ########################### | |
# https://newbedev.com/getting-current-branch-and-commit-hash-in-github-action | |
- name: Declare GIT hash and branch | |
id: vars | |
shell: bash | |
run: | | |
echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT | |
echo "branch=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_OUTPUT | |
echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
########################### Build osctrl ########################### | |
- name: Build osctrl binaries | |
# Build all osctrl components for linux for all archs | |
# Build all osctrl components for darwin for all archs | |
# Build osctrl cli for windows for all archs | |
if: matrix.goos == 'linux' || matrix.goos == 'darwin' || (matrix.goos == 'windows' && matrix.components == 'cli') | |
uses: ./.github/actions/build/binaries | |
with: | |
go_os: "${{ matrix.goos }}" | |
go_arch: "${{ matrix.goarch }}" | |
osctrl_component: "${{ matrix.components }}" | |
commit_sha: "${{ steps.vars.outputs.sha_short }}" | |
commit_branch: "${{ steps.vars.outputs.branch }}" | |
golang_version: "${{ env.GOLANG_VERSION }}" | |
########################### Test binaries ########################### | |
# - name: Run tests | |
# id: bin_tests | |
# uses: .github/actions/test/binaries | |
# with: | |
# go_os: "${{ matrix.goos }}" | |
# go_arch: "${{ matrix.goarch }}" | |
# osctrl_component: "${{ matrix.components }}" | |
# commit_sha: "${{ steps.vars.outputs.sha_short }}" | |
# commit_branch: "${{ steps.vars.outputs.branch }}" | |
# golang_version: "${{ env.GOLANG_VERSION }}" | |
create_docker_images: | |
needs: | |
- build_and_test | |
- build-prep | |
runs-on: ubuntu-22.04 | |
strategy: | |
matrix: | |
components: ['admin'] | |
goos: ['linux'] | |
goarch: ['amd64', 'arm64'] | |
steps: | |
########################### Checkout code ########################### | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 2 | |
########################### Generate SHA1 commit hash ########################### | |
# https://newbedev.com/getting-current-branch-and-commit-hash-in-github-action | |
- name: Declare GIT hash and branch | |
id: vars | |
shell: bash | |
run: | | |
echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT | |
echo "branch=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_OUTPUT | |
echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | |
########################### Build Docker containers ########################### | |
- name: Build and deploy osctrl Docker containers | |
uses: ./.github/actions/build/my-docker | |
with: | |
#### golang env vars #### | |
go_os: ${{ matrix.goos }} | |
go_arch: ${{ matrix.goarch }} | |
#### Build vars #### | |
osctrl_component: ${{ matrix.components }} | |
commit_sha: ${{ steps.vars.outputs.sha_short }} | |
#### Dockerhub creds #### | |
docker_hub_org: ${{ secrets.DOCKER_HUB_ORG }} | |
docker_hub_username: ${{ secrets.DOCKER_HUB_USERNAME }} | |
docker_hub_access_token: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | |
container_registry: ${{ secrets.CONTAINER_REGISTRY }} | |
push_docker_images: | |
needs: | |
- create_docker_images | |
- build-prep | |
runs-on: ubuntu-22.04 | |
strategy: | |
matrix: | |
components: ['admin'] | |
steps: | |
########################### Get digests from build ########################### | |
- name: Download digests | |
uses: actions/[email protected] | |
with: | |
pattern: digests-osctrl-${{ matrix.components }}-* | |
merge-multiple: true | |
path: /tmp/digests | |
########################### Setup Docker ########################### | |
- name: Set up Docker Buildx | |
uses: docker/[email protected] | |
########################### Add meta data for containers ########################### | |
- name: Docker meta | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.OSCTRL_ADMIN_IMAGE }} | |
tags: | | |
type=raw,value=${{needs.build-prep.outputs.dev-tag}} | |
########################### Log into Dockerhub ########################### | |
- name: Login to Container Registry | |
uses: docker/[email protected] | |
with: | |
registry: ${{ secrets.CONTAINER_REGISTRY }} | |
username: ${{ secrets.DOCKER_HUB_USERNAME }} | |
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | |
########################### Create and push images ########################### | |
- 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.OSCTRL_ADMIN_IMAGE }}@sha256:%s ' *) | |
########################### Inspect new image ########################### | |
- name: Inspect image | |
run: | | |
docker buildx imagetools inspect ${{ env.OSCTRL_ADMIN_IMAGE }}:${{ steps.meta.outputs.version }} |