-
Notifications
You must be signed in to change notification settings - Fork 0
157 lines (149 loc) · 5.71 KB
/
docker-build.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
---
#
# https://docs.docker.com/build/ci/github-actions/
# https://github.com/docker/build-push-action
# https://docs.github.com/en/actions/creating-actions/creating-a-docker-container-action
#
name: Docker Build Workflow
on: # yamllint disable-line rule:truthy
schedule:
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of the month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
# │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT)
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ │
# * * * * *
- cron: '15 8 1 * *'
workflow_dispatch:
pull_request:
env:
IMAGE_NAME: ci-generic-debian
defaults:
run:
shell: bash
jobs:
stage1:
name: Change Check
runs-on: 'ubuntu-latest'
outputs:
docs_changed: ${{ steps.check_file_changed.outputs.docs_changed }}
matrix_exercise: ${{ steps.check_file_changed.outputs.matrix_exercise }}
env:
re_change: '^(Dockerfile.*|.github/docker/.*|.github/workflows/docker-build.yml)$'
steps:
- name: Check GitHub Vars
id: github-vars
run: set | grep -e ^CI -e ^GITHUB_ -e ^RUNNER_
- name: Checkout Repo
id: checkout-repo
uses: actions/checkout@v3
with:
fetch-depth: 0
ref: ${{ github.ref }}
submodules: recursive
- name: Get Change List
id: check_file_changed
run: |-
{
printf "Workflow: %s\n\n" "${GITHUB_WORKFLOW}"
printf "Runner: name[%s] arch[%s]\n" "${RUNNER_NAME}" "${RUNNER_ARCH}"
printf "Repo: %s\n" "${GITHUB_REPOSITORY}"
printf "User: %s\n" "${GITHUB_TRIGGERING_ACTOR}"
printf "\n"
} | tee -a "${GITHUB_STEP_SUMMARY}"
# Diff HEAD with the previous commit then output to stdout.
declare -a GIT_DIFF
mapfile -t GIT_DIFF < <(git diff --name-only HEAD^ HEAD | tee /tmp/changed_files.txt)
{
printf "=== Which files changed? ===\n"
printf "\n"
printf "\`\`\`text\n"
printf "%s\n" "${GIT_DIFF[@]}"
printf "\`\`\`\n"
printf "\n"
} | tee -a "${GITHUB_STEP_SUMMARY}"
# Get changed exercise list.
if [[ ${GITHUB_EVENT_NAME} != pull_request ]]; then
printf "Manual run, pretending we have a diff.\n"
HAS_DIFF=true
fi
printf "\n"
# Check if the files are present in the changed file list (added, modified, deleted) then output to stdout.
HAS_DIFF="${HAS_DIFF:-false}"
printf "=== Which Docker files changed? ===\n"
if printf "%s\n" "${GIT_DIFF[@]}" | grep -E "${{ env.re_change }}"; then
HAS_DIFF=true
fi
printf "\n"
# Did Docker files change?
{
printf "=== Did Docker files change? ===\n"
printf "Changes Detected: %s\n" "${HAS_DIFF}"
printf "\n"
printf "\`\`\`text\n"
printf "%s\n" "${GIT_DIFF[@]}" | grep -E "${{ env.re_change }}" || true
printf "\`\`\`\n"
printf "\n"
} | tee -a "${GITHUB_STEP_SUMMARY}"
# Set the output named "docs_changed"
printf "%s=%s\n" "docs_changed" "${HAS_DIFF}" >> "${GITHUB_OUTPUT}"
stage2:
name: Docker Build
strategy:
matrix:
os: ["ubuntu-latest"]
runs-on: "${{ matrix.os }}"
needs: [stage1]
if: needs.stage1.outputs.docs_changed == 'True'
outputs:
tag_date: ${{ steps.setup_image_info.outputs.tag_date }}
steps:
- name: Setup Env [${{ matrix.os }}]
id: setup_image_info
run: |-
tag_date="$(date +%Y%m%d)"
printf "%s=%s\n" "tag_date" "${tag_date}" >> "${GITHUB_OUTPUT}"
- name: Docker Build Checkout [${{ matrix.os }}]
uses: actions/checkout@v3
- name: Login to Docker Hub [${{ matrix.os }}]
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx [${{ matrix.os }}]
uses: docker/setup-buildx-action@v2
- name: Build and push [${{ matrix.os }}]
uses: docker/build-push-action@v4
with:
context: .
file: ./Dockerfile
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }}:latest,${{ secrets.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }}:${{ steps.setup_image_info.outputs.tag_date }}
stage3:
name: Docker Test
env:
ORG: ${{ secrets.DOCKERHUB_USERNAME }}
IMAGE_NAME: ci-generic-debian
strategy:
matrix:
os:
- vpayno/ci-generic-debian:latest
- vpayno/ci-generic-debian:${{ needs.stage2.outputs.tag_date }}
runs-on: ubuntu-latest
container: ${{ matrix.os }}
needs: [stage2]
steps:
- name: Test New Container [${{ matrix.os }}]
run: |-
printf "%s\n" "Hello World!"
- name: Docker Build Summary [${{ matrix.os }}]
run: |-
ORG_IMAGE="${{ secrets.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }}"
TOKEN="$(curl "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${ORG_IMAGE}:pull" | jq -r .access_token)"
{
printf "Docker Hub Image Info\n"
curl -sS --header "Authorization: Bearer ${TOKEN}" "https://index.docker.io/v2/${ORG_IMAGE}/tags/list" | jq .
} | tee -a "${GITHUB_STEP_SUMMARY}"