-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new workflow to build Docker Images
- Loading branch information
1 parent
f4a6de8
commit 62e6ddb
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
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@v4 | ||
- name: "Calculate changed files directories" | ||
id: calculate_changed_files | ||
uses: tj-actions/changed-files@v44 | ||
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-image: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
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@v4 | ||
- name: Log in to the Container Registry | ||
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 | ||
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" | ||
- 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@v3 | ||
- name: Get Docker Metadata | ||
id: meta | ||
uses: docker/metadata-action@v5 | ||
with: | ||
images: ${{ env.NAMESPACED_REGISTRY }}/${{ matrix.changed_dir }} | ||
tags: | | ||
type=semver,pattern={{version}},value=v${{ steps.calculate_version.outputs.version }} | ||
type=sha | ||
- name: Build and Push Docker image | ||
id: push | ||
uses: docker/build-push-action@v6 | ||
with: | ||
context: ${{ github.workspace }}/${{ matrix.changed_dir }} | ||
file: ${{ github.workspace }}/${{ matrix.changed_dir }}/Dockerfile | ||
push: true | ||
tags: ${{ steps.meta.outputs.tags }} | ||
labels: ${{ steps.meta.outputs.labels }} | ||
platforms: ${{ steps.extract_from_config_yaml.outputs.platforms }} | ||
- name: Generate artifact attestation | ||
uses: actions/attest-build-provenance@v1 | ||
with: | ||
subject-name: ${{ env.NAMESPACED_REGISTRY }}/${{ matrix.changed_dir }} | ||
subject-digest: ${{ steps.push.outputs.digest }} | ||
push-to-registry: true | ||
- name: Create Git Tag | ||
if: ${{ github.event_name != 'pull_request' }} | ||
uses: mathieudutour/github-tag-action@v6.2 | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
default_bump: false | ||
default_prerelease_bump: false | ||
custom_tag: ${{ matrix.changed_dir }}/v${{ steps.calculate_version.outputs.version }} | ||
- name: Create GitHub Release | ||
if: ${{ github.event_name != 'pull_request' }} | ||
uses: comnoco/create-release-action@v2.0.5 | ||
with: | ||
tag_name: ${{ matrix.changed_dir }}/v${{ steps.calculate_version.outputs.version }} | ||
release_name: ${{ matrix.changed_dir }} - v${{ steps.calculate_version.outputs.version }} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version: 0.1.0 | ||
platforms: | ||
- linux/arm64 | ||
- linux/amd64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
version: 0.1.0 | ||
platforms: | ||
- linux/arm64 | ||
- linux/amd64 |