Skip to content

Commit 3a7cef1

Browse files
authored
Merge pull request #10423 from misilot/automated-image-build-github-actions
Automatic Building of Docker Images and Pushing to DockerHub
2 parents 44d3a42 + a7760b3 commit 3a7cef1

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

.github/workflows/docker.yml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Snipe-IT Docker image build for hub.docker.com
2+
name: Docker images
3+
4+
# Run this Build for all pushes to 'master' or develop branch, or tagged releases.
5+
# Also run for PRs to ensure PR doesn't break Docker build process
6+
on:
7+
push:
8+
branches:
9+
- master
10+
- develop
11+
tags:
12+
- 'v**'
13+
# Allows you to run this workflow manually from the Actions tab
14+
workflow_dispatch:
15+
16+
pull_request:
17+
18+
jobs:
19+
docker:
20+
# Ensure this job never runs on forked repos. It's only executed for 'snipe/snipe-it'
21+
if: github.repository == 'snipe/snipe-it'
22+
runs-on: ubuntu-latest
23+
env:
24+
# Define tags to use for Docker images based on Git tags/branches (for docker/metadata-action)
25+
# For a new commit on default branch (master), use the literal tag 'latest' on Docker image.
26+
# For a new commit on other branches, use the branch name as the tag for Docker image.
27+
# For a new tag, copy that tag name as the tag for Docker image.
28+
IMAGE_TAGS: |
29+
type=raw,value=latest,enable=${{ endsWith(github.ref, github.event.repository.default_branch) }}
30+
type=ref,event=branch,enable=${{ !endsWith(github.ref, github.event.repository.default_branch) }}
31+
type=ref,event=tag
32+
# Define default tag "flavor" for docker/metadata-action per
33+
# https://github.com/docker/metadata-action#flavor-input
34+
# We turn off 'latest' tag by default.
35+
TAGS_FLAVOR: |
36+
latest=false
37+
38+
steps:
39+
# https://github.com/actions/checkout
40+
- name: Checkout codebase
41+
uses: actions/checkout@v2
42+
43+
# https://github.com/docker/setup-buildx-action
44+
- name: Setup Docker Buildx
45+
uses: docker/setup-buildx-action@v1
46+
47+
# https://github.com/docker/login-action
48+
- name: Login to DockerHub
49+
# Only login if not a PR, as PRs only trigger a Docker build and not a push
50+
if: github.event_name != 'pull_request'
51+
uses: docker/login-action@v1
52+
with:
53+
username: ${{ secrets.DOCKER_USERNAME }}
54+
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
55+
56+
###############################################
57+
# Build/Push the 'snipe/snipe-it' image
58+
###############################################
59+
# https://github.com/docker/metadata-action
60+
# Get Metadata for docker_build step below
61+
- name: Sync metadata (tags, labels) from GitHub to Docker for 'snipe-it' image
62+
id: meta_build
63+
uses: docker/metadata-action@v3
64+
with:
65+
images: snipe/snipe-it
66+
tags: ${{ env.IMAGE_TAGS }}
67+
flavor: ${{ env.TAGS_FLAVOR }}
68+
69+
# https://github.com/docker/build-push-action
70+
- name: Build and push 'snipe-it' image
71+
id: docker_build
72+
uses: docker/build-push-action@v2
73+
with:
74+
context: .
75+
file: ./Dockerfile
76+
platforms: linux/amd64
77+
# For pull requests, we run the Docker build (to ensure no PR changes break the build),
78+
# but we ONLY do an image push to DockerHub if it's NOT a PR
79+
push: ${{ github.event_name != 'pull_request' }}
80+
# Use tags / labels provided by 'docker/metadata-action' above
81+
tags: ${{ steps.meta_build.outputs.tags }}
82+
labels: ${{ steps.meta_build.outputs.labels }}

0 commit comments

Comments
 (0)