Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Build and push devel Docker image on master changes via CI workflow #512

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

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions .github/workflows/release.docker.devel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
name: release-docker-devel

on:
push:
branches:
- master
paths:
- .github/workflows/release.docker.devel.yml
- .cargo/config.toml
- Cargo.lock
- Cargo.toml
- src/**

env:
TARGET_DIR: ./target

jobs:
build-release:
runs-on: ubuntu-22.04
env:
CARGO_BIN: cargo
# Emit backtraces on panics.
RUST_BACKTRACE: 1
# SWS features for Cargo build
CARGO_FEATURES: "--features=all"
# When CARGO_BIN is set to CROSS, this is set to `--target matrix.target`.
TARGET_FLAGS: ""
strategy:
matrix:
build:
# DOCKER: jobs just work only for one target.
# If multiple targets are needed then refactor the workflow and the 'devel' Dockerfile to be Docker Multiarch.
- linux-musl
include:
- build: linux-musl
arch: linux/amd64
rust: stable
target: x86_64-unknown-linux-musl

outputs:
target: ${{ steps.target.outputs.target }}
arch: ${{ steps.arch.outputs.arch }}

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
- name: Set up Cross
shell: bash
run: |
echo "Installing cross..."
curl -sSL \
"https://github.com/cross-rs/cross/releases/download/v0.2.5/cross-${{ matrix.target }}.tar.gz" \
| sudo tar zxf - -C /usr/local/bin/ cross cross-util
cross -V
echo "CARGO_BIN=/usr/local/bin/cross" >> $GITHUB_ENV
echo "TARGET_FLAGS=--target=${{ matrix.target }}" >> $GITHUB_ENV
echo "TARGET_DIR=${{ env.TARGET_DIR }}/${{ matrix.target }}" >> $GITHUB_ENV

- name: Show command used for Cargo
run: |
echo "cargo command is: ${{ env.CARGO_BIN }}"
echo "target dir is: ${{ env.TARGET_DIR }}"
- name: Build release binary
run: |
${{ env.CARGO_BIN }} build --bin static-web-server -vv --release ${{ env.CARGO_FEATURES }} ${{ env.TARGET_FLAGS }}
- name: Define target output
id: target
run: |
echo 'target=${{ matrix.target }}' >> "$GITHUB_OUTPUT"
- name: Define arch output
id: arch
run: |
echo 'arch=${{ matrix.arch }}' >> "$GITHUB_OUTPUT"
- name: Cache binary
uses: actions/cache@v4
with:
path: ${{ env.TARGET_DIR }}/release
# short-lived cache: https://github.com/actions/cache/blob/main/caching-strategies.md#creating-a-short-lived-cache
key: cache-${{ github.sha }}-${{ matrix.target }}

# NOTE: only one arch/target per Docker "kind" is supported.
# If multiple targets are needed then refactor the workflow and the 'devel' Dockerfile to be Docker Multiarch.
docker-image:
needs: ['build-release']
runs-on: ubuntu-22.04
permissions:
contents: read
packages: write
strategy:
matrix:
kind:
- scratch
- debian
include:
- kind: scratch
tag: devel
- kind: debian
tag: devel-debian
target:
- ${{ needs.build-release.outputs.target }}
arch:
- ${{ needs.build-release.outputs.arch }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 1
- name: Restore cache
uses: actions/cache/restore@v4
with:
path: ${{ env.TARGET_DIR }}/${{ matrix.target }}/release
key: cache-${{ github.sha }}-${{ matrix.target }}
- name: Copy binary
run: cp ${{ env.TARGET_DIR }}/${{ matrix.target }}/release/static-web-server ./docker/devel/

- name: Login to ghcr.io
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push (${{ matrix.kind }})
uses: docker/build-push-action@v6
with:
push: true
context: .
platforms: ${{ matrix.arch }}
file: ./docker/devel/Dockerfile.${{ matrix.kind }}
tags: ghcr.io/static-web-server/static-web-server:${{ matrix.tag }}