Skip to content

Commit

Permalink
ci: make docker reusable workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
ktmeaton committed Nov 24, 2023
1 parent 63fb0e4 commit 10cb9da
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 83 deletions.
82 changes: 1 addition & 81 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ on:
required: true
type: string

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:

# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -53,80 +49,4 @@ jobs:

# ---------------------------------------------------------------------------
docker:

needs: compile
if: ${{ always() }}

strategy:
matrix:
arch: [x86_64-unknown-linux-musl]
os: [ubuntu-latest]
binary: [rebar]

runs-on: ${{ matrix.os }}

permissions:
contents: read
packages: write

steps:
- name: checkout
uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: download binary from build
uses: actions/download-artifact@v3
with:
name: rebar-${{ matrix.arch }}
path: target/${{ matrix.arch }}/release/

- name: update binary permission
run: |
chmod +x target/${{ matrix.arch }}/release/${{ matrix.binary }}
- name: registry login
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: build local
uses: docker/build-push-action@v5
with:
context: .
tags: ${{ env.IMAGE_TAG }}
outputs: type=docker,dest=rebar.tar

- name: test local
run: |
docker load --input rebar.tar
docker run ${{ env.IMAGE_TAG }} rebar --help
# push only on tag?
# - name: build
# uses: docker/build-push-action@v5
# with:
# context: .
# load: true
# tags: ${{ steps.meta.outputs.tags }}
# labels: ${{ steps.meta.outputs.labels }}

- name: upload image artifact
uses: actions/upload-artifact@v3
with:
name: rebar-docker
path: rebar.tar
retention-days: 7
if-no-files-found: error
uses: ./.github/workflows/docker.yaml
1 change: 1 addition & 0 deletions .github/workflows/compile.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Re-usable workflow to compile the binary application on various systems
name: Compile

on:
Expand Down
103 changes: 103 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# Re-usable workflow to create the docker image
name: Docker

on:
workflow_call:

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:

# ---------------------------------------------------------------------------
# The docker image only needs the linux version compiled

compile:
uses: ./.github/workflows/compile.yaml
strategy:
fail-fast: false
matrix:
include:
# Linux
- arch: x86_64-unknown-linux-musl
os: ubuntu-latest
binary: rebar
with:
arch: ${{ matrix.arch }}
os: ${{ matrix.os }}
binary: ${{ matrix.binary }}

# ---------------------------------------------------------------------------
docker:

needs: compile

strategy:
matrix:
arch: [x86_64-unknown-linux-musl]
os: [ubuntu-latest]
binary: [rebar]

runs-on: ${{ matrix.os }}

permissions:
contents: read
packages: write

steps:
- name: checkout
uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: download binary from build
uses: actions/download-artifact@v3
with:
name: rebar-${{ matrix.arch }}
path: target/${{ matrix.arch }}/release/

- name: update binary permission
run: |
chmod +x target/${{ matrix.arch }}/release/${{ matrix.binary }}
- name: registry login
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: build
uses: docker/build-push-action@v5
with:
context: .
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
outputs: type=docker,dest=rebar.tar

- name: load
run: |
docker load --input rebar.tar
- name: help
run: |
docker run ${{ env.IMAGE_NAME }}:$DOCKER_METADATA_OUTPUT_VERSION rebar --help
- name: upload image artifact
uses: actions/upload-artifact@v3
with:
name: rebar-docker
path: rebar.tar
retention-days: 7
if-no-files-found: error
25 changes: 23 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ on:
env:
GITHUB_USERNAME: ${{ github.actor }}
GITHUB_TOKEN: ${{ github.token }}
IMAGE_NAME: ${{ github.repository }}

jobs:

build:
uses: ./.github/workflows/build.yaml

# ---------------------------------------------------------------------------
direct:
test:

needs: build
if: ${{ always() }}
Expand All @@ -30,19 +31,26 @@ jobs:
fail-fast: false
matrix:
include:
# linux
- arch: x86_64-unknown-linux-musl
os: ubuntu-latest
binary: rebar
prefix: "./"
# windows
- arch: x86_64-pc-windows-gnu
os: windows-latest
binary: rebar.exe
prefix: "./"
# macOS
- arch: x86_64-apple-darwin
os: macos-latest
binary: rebar
prefix: "./"
# docker
- arch: docker
os: ubuntu-latest
binary: rebar
prefix: "docker run "

runs-on: ${{ matrix.os }}

Expand All @@ -51,18 +59,31 @@ jobs:
uses: actions/checkout@v3

- name: download binary from build
if: matrix.arch != docker
if: ${{ matrix.arch != docker }}
uses: actions/download-artifact@v3
with:
name: rebar-${{ matrix.arch }}
path: target/${{ matrix.arch }}/release

- name: download docker image from build
if: ${{ matrix.arch == docker }}
uses: actions/download-artifact@v3
with:
name: rebar-${{ matrix.arch }}
path: target/${{ matrix.arch }}/release

- name: install
if: ${{ matrix.arch != docker }}
run: |
chmod +x target/${{ matrix.arch }}/release/${{ matrix.binary }}
cp target/${{ matrix.arch }}/release/${{ matrix.binary }} .
./${{ matrix.binary }} --help
- name: load docker image
if: ${{ matrix.arch == docker }}
run: |
docker load target/${{ matrix.arch }}/release/${{ matrix.binary }}.tar
- name: dataset list
run: |
./${{ matrix.binary }} dataset list
Expand Down

0 comments on commit 10cb9da

Please sign in to comment.