Skip to content
This repository has been archived by the owner on Oct 7, 2022. It is now read-only.

Commit

Permalink
ci: reuse build and push steps into an action
Browse files Browse the repository at this point in the history
  • Loading branch information
upsetbit committed May 12, 2022
1 parent 943ac87 commit a74a0f2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 33 deletions.
53 changes: 53 additions & 0 deletions .github/actions/build-and-push/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: build-and-push


inputs:
path:
required: true
description: The directory path of the Dockerfile (context location)

image-name:
required: true
description: The Docker image name (user/repo:tag)

tag-suffix:
required: true
description: The Docker image tag suffix (bare, full, edge etc)


runs:
using: composite
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Lint Dockerfile
run: docker run --rm -i hadolint/hadolint < ${{ inputs.path }}/Dockerfile

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: caian
password: ${{ secrets.DOCKERHUB_TOKEN }}

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

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: build-image-${{ inputs.tag-suffix }}-buildx-${{ github.sha }}
restore-keys: |
build-image-${{ inputs.tag-suffix }}-buildx-
- name: Build and push "${{ inputs.tag-suffix }}" image
uses: docker/build-push-action@v2
with:
context: ${{ inputs.path }}
push: true
tags: ${{ inputs.image-name }}
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
42 changes: 9 additions & 33 deletions .github/workflows/build-bare-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,20 @@ on:
jobs:
build-bare-image:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Lint Dockerfile
run: docker run --rm -i hadolint/hadolint < images/bare/Dockerfile

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: caian
password: ${{ secrets.DOCKERHUB_TOKEN }}
env:
tag-suffix: bare

steps:
- name: Retrieve metadata for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: caian/sample-devcontainer
tags: type=semver,pattern={{version}}-bare

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

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: build-bare-image-buildx-${{ github.sha }}
restore-keys: |
build-bare-image-buildx-
tags: type=semver,pattern={{version}}

- name: Build and push "bare" (base) image
uses: docker/build-push-action@v2
- name: Build and push
uses: ./.github/actions/build-and-push
with:
context: images/bare
push: true
tags: ${{ steps.meta.outputs.tags }}
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
path: image/${{ env.tag-suffix }}
version: ${{ steps.meta.outputs.tags }}-${{ env.tag-suffix }}
tag-suffix: ${{ env.tag-suffix }}

0 comments on commit a74a0f2

Please sign in to comment.