Simplify the current CI/CD workflow #25
Workflow file for this run
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
name: cicd | |
on: | |
pull_request: | |
branches: | |
- main | |
push: | |
branches: | |
- main | |
tags: | |
- 'v[0-9]+.[0-9]+.[0-9]+' | |
jobs: | |
docker-build-and-push-images: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
strategy: | |
matrix: | |
include: | |
- host_namespace: ghcr.io/us-joet/everest-demo | |
image_name: manager | |
context: ./manager | |
- host_namespace: ghcr.io/us-joet/everest-demo | |
image_name: mqtt-server | |
context: ./mosquitto | |
- host_namespace: ghcr.io/us-joet/everest-demo | |
image_name: nodered | |
context: ./nodered | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Ensure Docker image version is not referencing an existing release | |
id: docker-image-version-check | |
run: | | |
if ! [[ -s .docker_image_version ]]; then | |
echo 'Error: No .docker_image_version file found.' | |
exit 1 | |
fi | |
if [[ $(sed '/^\s*$/d' .docker_image_version | wc -l) -ge 2 ]]; then | |
echo 'Error: .docker_image_version should contain only one line.' | |
exit 1 | |
fi | |
SEMVER="$(cat .docker_images_version)" | |
# Fail if any previous Docker image version value matches the one in | |
# this PR (excluding the current image version). | |
for commit in $(git --no-pager log --first-parent --format=%H -- .docker_image_version | tail -n +2); do | |
if git --no-pager grep -hF "${SEMVER}" $commit -- .docker_image_version | grep -qx ${SEMVER}; then | |
echo 'Error: The version in .docker_image_version matches an' | |
echo ' earlier version on main. Please update the value in' | |
echo ' .docker_image_version to a new version.' | |
exit 1 | |
fi | |
done | |
if git show-ref --tags --verify --quiet "refs/tags/v${SEMVER}"; then | |
echo "Error: The tag 'v${SEMVER}' is already a GitHub release.' | |
echo ' Please update the version in .docker_image_version' | |
exit 1 | |
else | |
echo "SEMVER=${SEMVER}" >> "${GITHUB_OUTPUT}" | |
fi | |
- name: Set Docker image metadata | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ matrix.host_namespace }}/${{ matrix.image_name }} | |
tags: | | |
type=ref,event=branch | |
type=ref,event=pr | |
type=semver,pattern=${{ steps.docker-image-version-check.outputs.SEMVER }} | |
- name: Log into GitHub container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Build and push | |
uses: docker/build-push-action@v5 | |
with: | |
context: ${{ matrix.context }} | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: type=gha,scope=${{ matrix.image_name }} | |
cache-to: type=gha,mode=max,scope=${{ matrix.image_name }} |