-
Notifications
You must be signed in to change notification settings - Fork 0
132 lines (128 loc) · 6.15 KB
/
docker_publish.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
name: Build & Publish Docker Images
on:
push:
branches: [ 'main' ]
pull_request:
env:
REGISTRY: ghcr.io
NAMESPACED_REGISTRY: ghcr.io/apollographql/ci-utility-docker-images
jobs:
calculate-images-to-build:
name: Calculate Images To Build
runs-on: ubuntu-latest
outputs:
changed_dirs: ${{ steps.filter_config_directories.outputs.changed_dirs }}
steps:
- name: "Checkout repository"
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871
- name: "Calculate changed files directories"
id: calculate_changed_files
uses: tj-actions/changed-files@1f772e919bcbf025d221836bc4b8e3b72e2ef117
with:
dir_names: true
dir_names_exclude_current_dir: true
json: true
- name: "Filter out config directories"
id: filter_config_directories
run: |
CHANGED_DIRS=$(echo "${{ steps.calculate_changed_files.outputs.all_changed_files }}" | jq -c '[.[] | select(. | contains(".") | not)'])
echo "changed_dirs=$CHANGED_DIRS" >> "$GITHUB_OUTPUT"
build-and-push-images:
name: Build and Push Docker Image
if: ${{ needs.calculate-images-to-build.outputs.changed_dirs != '' && toJson(fromJson(needs.calculate-images-to-build.outputs.changed_dirs)) != '[]' }}
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
attestations: write
id-token: write
needs:
- calculate-images-to-build
strategy:
matrix:
changed_dir: ${{ fromJSON(needs.calculate-images-to-build.outputs.changed_dirs ) }}
steps:
- name: Checkout repository
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871
- name: Log in to the Container Registry
uses: docker/login-action@1f36f5b7a2d2f7bfd524795fc966e6d88c37baa9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract Details From config.yml
id: extract_from_config_yaml
run: |
echo "desired_version=$(cat ${{ github.workspace }}/${{ matrix.changed_dir }}/config.yml | yq '.version')" >> "$GITHUB_OUTPUT"
echo "platforms=$(cat ${{ github.workspace }}/${{ matrix.changed_dir }}/config.yml | yq '.platforms | join(",")')" >> "$GITHUB_OUTPUT"
echo "description=$(cat ${{ github.workspace }}/${{ matrix.changed_dir }}/config.yml | yq '.description')" >> "$GITHUB_OUTPUT"
- name: Check Image to Build Does Not Already Exist
run: |
if docker manifest inspect ${{ env.NAMESPACED_REGISTRY }}/${{ matrix.changed_dir }}:${{ steps.extract_from_config_yaml.outputs.desired_version }} > /dev/null; then
echo "The tag "${{ env.NAMESPACED_REGISTRY }}/${{ matrix.changed_dir }}:${{ steps.extract_from_config_yaml.outputs.desired_version }}" already exists in the repository. Do you need to bump the version in the config.yml?"
exit 1
fi
- name: Calculate Version
id: calculate_version
run: |
VERSION=${{ github.event_name == 'pull_request' && format('{0}-PR{1}.{2}', steps.extract_from_config_yaml.outputs.desired_version, github.event.number, github.event.pull_request.head.sha) || steps.extract_from_config_yaml.outputs.desired_version}}
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@460e45646ded27e46146cedd75d922e7fd9ef69f
- name: Get Docker Metadata
id: meta
uses: docker/metadata-action@70b2cdc6480c1a8b86edf1777157f8f437de2166
env:
DOCKER_METADATA_PR_HEAD_SHA: true
with:
images: ${{ env.NAMESPACED_REGISTRY }}/${{ matrix.changed_dir }}
tags: |
type=semver,pattern={{version}},value=v${{ steps.calculate_version.outputs.version }}
type=sha,prefix=
labels: |
org.opencontainers.image.title=${{ matrix.changed_dir }}
org.opencontainers.image.description=${{ steps.extract_from_config_yaml.outputs.description }}
org.opencontainers.image.vendor=Apollo GraphQL
org.opencontainers.image.licenses=MIT
annotations: |
org.opencontainers.image.title=${{ matrix.changed_dir }}
org.opencontainers.image.description=${{ steps.extract_from_config_yaml.outputs.description }}
org.opencontainers.image.vendor=Apollo GraphQL
org.opencontainers.image.licenses=MIT
- name: Build and Push Docker image
id: push
uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75
with:
context: ${{ github.workspace }}/${{ matrix.changed_dir }}
file: ${{ github.workspace }}/${{ matrix.changed_dir }}/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
annotations: ${{ steps.meta.outputs.annotations }}
labels: ${{ steps.meta.outputs.labels }}
platforms: ${{ steps.extract_from_config_yaml.outputs.platforms }}
- name: Create Git Tag
uses: mathieudutour/github-tag-action@9d35d648e836d5db77936aa35a3a5c7bc16092c8
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
default_bump: false
default_prerelease_bump: false
custom_tag: ${{ matrix.changed_dir }}/v${{ steps.calculate_version.outputs.version }}
dry_run: ${{ github.event_name == 'pull_request' }}
tag_prefix: ""
- name: Create GitHub Release
if: ${{ github.event_name != 'pull_request' }}
uses: comnoco/create-release-action@7dea6dc82ac9d97ced7a764aa82811451bba80e0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ matrix.changed_dir }}/v${{ steps.calculate_version.outputs.version }}
release_name: ${{ matrix.changed_dir }} - v${{ steps.calculate_version.outputs.version }}
check-builds-all-completes:
name: Docker Images Built & Pushed
if: ${{ always() }}
runs-on: ubuntu-latest
needs:
- build-and-push-images
steps:
- run: |
exit ${{ (contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')) && 1 || 0 }}