name: Build test and release on: push: branches: - main paths-ignore: - 'charts/**' - 'docs/**' - '.github/**' - 'README.md' pull_request: branches: - main paths-ignore: - 'charts/**' - 'docs/**' - '.github/**' - 'README.md' workflow_dispatch: jobs: prepare: name: Preparing build context runs-on: ubuntu-latest outputs: SANITISED_REPOSITORY_NAME: ${{ steps.get_env.outputs.SANITISED_REPOSITORY_NAME }} DOCKER_IMAGE: ${{ steps.get_env.outputs.DOCKER_IMAGE }} GITHUB_COMMIT_NUMBER: ${{ steps.get_env.outputs.GITHUB_COMMIT_NUMBER }} GITHUB_SHA: ${{ steps.get_env.outputs.GITHUB_SHA }} DOCKER_IMAGE_SEMVER: ${{ steps.semver.outputs.semantic_version }} CHECK_IF_MASTER_BRANCH: ${{ steps.get_env.outputs.CHECK_FOR_MASTER_BRANCH }} steps: - name: Checkout repo uses: actions/checkout@v3 with: fetch-depth: '0' - name: Configure git for private modules uses: extractions/netrc@v1 with: machine: github.com username: ${{ github.actor }} password: ${{ secrets.GHCR_TOKEN }} - name: Set environment variables id: get_env run: | TMP_SANITISED_REPOSITORY_NAME=$(echo ${{ github.event.repository.name }} | sed -e 's|\.|-|g') CHECK_FOR_MASTER_BRANCH="false" echo "SANITISED_REPOSITORY_NAME=$TMP_SANITISED_REPOSITORY_NAME" >> $GITHUB_OUTPUT echo "DOCKER_IMAGE=ghcr.io/${{ github.repository_owner }}/$TMP_SANITISED_REPOSITORY_NAME" >> $GITHUB_OUTPUT echo "GITHUB_COMMIT_NUMBER=$(git rev-list --count HEAD)" >> $GITHUB_OUTPUT echo "GITHUB_SHA=$(echo ${GITHUB_SHA::8})" >> $GITHUB_OUTPUT if [[ ${{ github.ref }} == 'refs/heads/master' || ${{ github.ref }} == 'refs/heads/main' ]]; then CHECK_FOR_MASTER_BRANCH="true" fi echo "CHECK_FOR_MASTER_BRANCH=$CHECK_FOR_MASTER_BRANCH" >> $GITHUB_OUTPUT - name: Establish semver id: semver uses: lukaszraczylo/semver-generator@v1 with: config_file: semver.yaml repository_local: true github_username: ${{ github.actor }} github_token: ${{ secrets.GHCR_TOKEN }} - name: Semver check run: | echo "Semantic version detected: ${{ steps.semver.outputs.semantic_version }}" test: name: "Unit testing" needs: [prepare] runs-on: ubuntu-20.04 # container: github/super-linter:v4 steps: - name: Checkout repository uses: actions/checkout@v3 - name: Install Go uses: actions/setup-go@v3 with: go-version: 1.21 - name: Configure git for private modules uses: extractions/netrc@v1 with: machine: github.com username: ${{ github.actor }} password: ${{ secrets.GHCR_TOKEN }} - uses: actions/cache@v3 with: path: | ~/.cache/go-build ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - name: Run unit tests run: | make test build-docker: name: "Building docker image" needs: [ prepare, test ] runs-on: ubuntu-20.04 if: needs.prepare.outputs.CHECK_IF_MASTER_BRANCH == 'true' steps: - name: Checkout repo uses: actions/checkout@v3 - name: Set up QEMU uses: docker/setup-qemu-action@v2 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - name: Login to GHCR if: github.event_name != 'pull_request' uses: docker/login-action@v2 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GHCR_TOKEN }} - uses: actions/cache@v3 with: path: | ~/.cache/go-build ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - name: Build and push if: github.event_name != 'pull_request' && ${{ needs.prepare.outputs.CHECK_FOR_MASTER_BRANCH == 'true' }} run: | make docker-buildx IMG=${{ needs.prepare.outputs.DOCKER_IMAGE }}:${{ needs.prepare.outputs.DOCKER_IMAGE_SEMVER }} IMG_SECONDARY_TAG=${{ needs.prepare.outputs.DOCKER_IMAGE }}:latest release: name: Create Release runs-on: ubuntu-latest needs: [ prepare, build-docker ] if: needs.prepare.outputs.CHECK_IF_MASTER_BRANCH == 'true' steps: - name: Checkout code uses: actions/checkout@v3 with: fetch-depth: 0 - name: Get list of the commits since last release run: | echo "$(git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:"%h %s")" > .release_notes - name: Create Release id: create_release if: needs.prepare.outputs.CHECK_IF_MASTER_BRANCH == 'true' uses: softprops/action-gh-release@v1 with: name: v${{ needs.prepare.outputs.DOCKER_IMAGE_SEMVER }} body_path: .release_notes token: ${{ secrets.GHCR_TOKEN }} tag_name: v${{ needs.prepare.outputs.DOCKER_IMAGE_SEMVER }} prerelease: ${{ github.ref != 'refs/heads/master' && github.ref != 'refs/heads/main' }}