Docker build 2nd try #94
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: Build | |
on: | |
workflow_dispatch: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
env: | |
CARGO_TERM_COLOR: always | |
jobs: | |
build: | |
container: debian:bookworm-slim | |
runs-on: ubuntu-latest | |
env: | |
toolchain: stable | |
steps: | |
- name: Harden Runner | |
uses: step-security/harden-runner@cb605e52c26070c328afc4562f0b4ada7618a84e # v2.10.4 | |
with: | |
egress-policy: audit | |
- name: Checkout | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
with: | |
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis | |
- name: Cache cargo registry | |
uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0 | |
with: | |
path: | | |
~/.cargo/registry | |
~/.cargo/git | |
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} | |
restore-keys: | | |
${{ runner.os }}-cargo-registry- | |
- name: Setup build environment | |
run: | | |
pwd | |
cat /etc/debian_version | |
apt search glibc | |
apt update | |
apt upgrade -y | |
apt search glibc | |
apt install -y curl build-essential gcc cmake pkg-config libssl-dev unzip | |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain ${{ env.toolchain }} --target x86_64-unknown-linux-gnu | |
. "$HOME/.cargo/env" | |
cargo --version | |
rustc --version | |
- name: Cache cargo build | |
uses: actions/cache@1bd1e32a3bdc45362d1e726936510720a7c30a57 # v4.2.0 | |
with: | |
path: target | |
key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }} | |
restore-keys: | | |
${{ runner.os }}-cargo-build- | |
- name: Install Cargo Tarpaulin | |
run: | | |
. "$HOME/.cargo/env" | |
cargo install cargo-tarpaulin | |
- name: Build | |
run: | | |
. "$HOME/.cargo/env" | |
cargo build | |
- name: Run Tests with Coverage | |
run: | | |
. "$HOME/.cargo/env" | |
cargo tarpaulin --engine llvm --out Xml | |
- name: Run Clippy | |
run: | | |
. "$HOME/.cargo/env" | |
cargo clippy --message-format=json &> clippy.json | |
- name: Update Sonar | |
uses: sonarsource/sonarqube-scan-action@bfd4e558cda28cda6b5defafb9232d191be8c203 # v4.2.1 | |
if: github.ref == 'refs/heads/main' | |
env: | |
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} | |
- name: Build Release | |
run: | | |
. "$HOME/.cargo/env" | |
cargo build --release | |
ldd target/release/mqtt-gateway | |
- name: Upload Release | |
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0 | |
with: | |
name: mqtt-gateway | |
path: target/release/mqtt-gateway | |
# If you wish to fail your job when the Quality Gate is red, uncomment the | |
# following lines. This would typically be used to fail a deployment. | |
# - uses: sonarsource/sonarqube-quality-gate-action@master | |
# timeout-minutes: 5 | |
# env: | |
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
docker-image: | |
runs-on: ubuntu-latest | |
if: | |
needs: build | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
# Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. | |
permissions: | |
contents: read | |
packages: write | |
attestations: write | |
id-token: write | |
# | |
steps: | |
- name: Harden Runner | |
uses: step-security/harden-runner@cb605e52c26070c328afc4562f0b4ada7618a84e # v2.10.4 | |
with: | |
egress-policy: audit | |
- name: Checkout repository | |
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | |
- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8 | |
with: | |
name: mqtt-gateway | |
path: target/release/mqtt-gateway | |
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. | |
- name: Log in to the Container registry | |
uses: docker/login-action@327cd5a69de6c009b9ce71bce8395f28e651bf99 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. | |
- name: Extract metadata (tags, labels) for Docker | |
id: meta | |
uses: docker/metadata-action@8e1d5461f02b7886d3c1a774bfbd873650445aa2 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
tags: latest | |
# This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. | |
# It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. | |
# It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. | |
- name: Build and push Docker image | |
id: push | |
uses: docker/build-push-action@67a2d409c0a876cbe6b11854e3e25193efe4e62d | |
with: | |
context: . | |
push: ${{ github.ref == 'refs/heads/main' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
# This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)." | |
- name: Generate artifact attestation | |
uses: actions/attest-build-provenance@7668571508540a607bdfd90a87a560489fe372eb # v2.1.0 | |
if: github.ref == 'refs/heads/main' | |
with: | |
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}} | |
subject-digest: ${{ steps.push.outputs.digest }} | |
push-to-registry: true |