Push to Container Registry #21
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: Push to Container Registry | |
# only trigger on pull request closed events | |
on: | |
pull_request: | |
types: [ closed ] | |
workflow_dispatch: | |
branches: [ master ] | |
jobs: | |
push_image: | |
# this job will only run if the PR has been merged | |
if: (github.event.pull_request.merged == true) || (github.event_name == 'workflow_dispatch') | |
runs-on: ubuntu-20.04 | |
steps: | |
- name: Check the Pull Request | |
if: github.event.pull_request != null | |
run: echo "Event '${{ github.event_name }}' - PR ${{ github.event.pull_request.number }} has been merged" | |
- name: Check for Manual Launch | |
if: github.event_name == 'workflow_dispatch' | |
run: echo "Event '${{ github.event_name }}' - Manual Push to Registry" | |
- name: Install Git Tools | |
run: | | |
perl --version | |
sudo apt-get update | |
sudo apt-get -y install git cpanminus | |
cpanm Process::SubProcess | |
echo "Process::SubProcess Command:" | |
which run_subprocess.pl || true | |
- name: Checkout | |
uses: actions/checkout@v3 | |
with: | |
token: ${{ secrets.IMAGE_PUBLISH_TOKEN }} | |
# - name: Set up QEMU | |
# uses: docker/setup-qemu-action@v1 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2.5.0 | |
# Test the Docker Image Build | |
- name: Build the Image | |
run: | | |
echo "Build the Docker Image" | |
docker build -t rust_builder . | |
# Show Compiler Version | |
- name: Show Compiler Version | |
id: rust_version | |
run: | | |
rust_version_info=`docker run -t rust_builder bash -c "rustc --version"` | |
rust_version=`echo "$rust_version_info" | awk '{print $2}'` | |
echo "* Rust Compiler Version:" | tee > VERSION | |
echo "$rust_version | $rust_version_info" | tee >> VERSION | |
echo "version_info=$rust_version_info" >> $GITHUB_OUTPUT | |
echo "version=$rust_version" >> $GITHUB_OUTPUT | |
- name: Check Repository for Rust Version Tag | |
id: git_tag | |
run: | | |
git fetch --tags | |
tag_label=`git tag -l "v${{ steps.rust_version.outputs.version }}"` | |
if [ -z $tag_label ]; then tag_label="v${{ steps.rust_version.outputs.version }}"; tag_exists=0; else tag_exists=1; fi; | |
echo "git - tag: '$tag_label'; exists: '$tag_exists'" | |
echo "tag=$tag_label" >> $GITHUB_OUTPUT | |
echo "exists=$tag_exists" >> $GITHUB_OUTPUT | |
# Version file might exist on retry | |
- name: Tag the Repository with Rust Version Tag | |
id: git_tag_set | |
if: steps.git_tag.outputs.exists == 0 | |
run: | | |
echo "git - tag: '${{ steps.git_tag.outputs.tag }}'; exists: '${{ steps.git_tag.outputs.exists }}'" | |
echo "git - commit: '${{ steps.package_version.outputs.version_commit }}'" | |
git checkout master | |
author_res=`git log -1 | grep -i author | sed -re 's/^[^:]+: ([^<]+) <([^>]+)>.*/\1|\2/i'` | |
echo -e "Commit Author:\n$author_res" | |
if [ -z "$author_res" ]; then echo "could not find GIT COMMIT AUTHOR"; exit 1; fi; | |
author_user=`echo "$author_res" | cut -d"|" -f1` | |
author_email=`echo "$author_res" | cut -d"|" -f2` | |
git config user.name "$author_user" | |
git config user.email "$author_email" | |
git add VERSION | |
git commit -m "Added new Rust Compiler Version: ${{ steps.rust_version.outputs.version }}" | |
echo "Show commit on 'master':" | |
git show | |
git push || true | |
git tag -a "${{ steps.git_tag.outputs.tag }}" -m "Update Image '${{ steps.git_tag.outputs.tag }}' with Rust Compiler Version: ${{ steps.package_version.outputs.version_info }} " | |
git push origin ${{ steps.git_tag.outputs.tag }} | |
# Show Compiler Version | |
- name: Set Compiler Version Tag | |
id: image_tag | |
if: steps.git_tag.outputs.exists == 0 | |
run: | | |
echo "rustc ver: '${{ steps.rust_version.outputs.version }}'" | |
docker tag rust_builder rust_builder:${{ steps.git_tag.outputs.tag }} | |
- name: Extract metadata (tags, labels) for Docker | |
id: image_meta | |
if: steps.git_tag.outputs.exists == 0 | |
uses: docker/metadata-action@v4.3.0 | |
with: | |
images: ghcr.io/${{ github.repository }} | |
tags: | | |
# use custom value instead of git tag | |
type=semver,pattern={{version}},value=v${{ steps.rust_version.outputs.version }} | |
# Show Image Meta Data | |
- name: Show Image Tags and Labels | |
if: steps.git_tag.outputs.exists == 0 | |
run: | | |
echo "* Image Tags:" | |
echo "${{ steps.image_meta.outputs.tags }}" | |
echo "* Image Labels:" | |
echo "${{ steps.image_meta.outputs.labels }}" | |
echo "* Image Inspection:" | |
docker inspect rust_builder | |
- name: Login to GitHub Container Registry | |
if: steps.git_tag.outputs.exists == 0 | |
uses: docker/login-action@v2.1.0 | |
with: | |
registry: ghcr.io | |
username: ${{ github.repository_owner }} | |
password: ${{ secrets.IMAGE_PUBLISH_AUTH_TOKEN }} | |
- name: Build and push Docker image | |
if: steps.git_tag.outputs.exists == 0 | |
uses: docker/build-push-action@v4.0.0 | |
with: | |
context: . | |
push: true | |
tags: ${{ steps.image_meta.outputs.tags }} | |
labels: ${{ steps.image_meta.outputs.labels }} | |
secrets: | | |
GIT_AUTH_TOKEN=${{ secrets.IMAGE_PUBLISH_AUTH_TOKEN }} | |