diff --git a/.github/workflows/build-publish.yaml b/.github/workflows/build-publish.yaml new file mode 100644 index 0000000..971c94f --- /dev/null +++ b/.github/workflows/build-publish.yaml @@ -0,0 +1,66 @@ +name: 'Publish Container Image' + +# Controls when the action will run. Triggers the workflow on push or pull +# request events but only for the master branch +on: + push: + branches: + - '*' + +permissions: + contents: 'read' + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build-and-publish: + # The type of runner that the job will run on + runs-on: 'ubuntu-latest' + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + - name: 'Checkout Repository 🛎️' + uses: 'actions/checkout@v2' + + - name: 'Build 🏗️' + id: 'build' + run: | + image="$( \ + basename "$(echo "${{ github.repository }}")" \ + | tr '[:upper:]' '[:lower:]' \ + | sed 's/docker-//' \ + )" + echo "image=$image" >> "$GITHUB_OUTPUT" + + docker build --file "Dockerfile" --tag "$image" . + + - name: 'Login to GitHub Container Registry 🔑' + uses: 'docker/login-action@v1' + if: '${{ github.ref_name }} == ${{ github.event.repository.default_branch }}' + with: + registry: 'ghcr.io' + username: '${{ github.repository_owner }}' + password: '${{ secrets.GITHUB_TOKEN }}' + + - name: 'Publish to Registry 🐳' + if: '${{ github.ref_name }} == ${{ github.event.repository.default_branch }}' + run: | + image="${{ steps.build.outputs.image }}" + repo="ghcr.io/${{ github.repository_owner }}/$image" + branch="$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')" + version="$branch" + + echo "repo = $repo" + echo "image = $image" + echo "version = $version" + + docker tag "$image" "$repo/$image:$version" + docker push "$repo/$image:$version" + + # Use Docker `latest` tag convention + if [[ "${{ github.event.repository.default_branch }}" == "$branch" ]]; then + docker tag "$image" "$repo/$image:latest" + docker push "$repo/$image:latest" + fi + + echo "registry_uri=$repo/$image" >> "$GITHUB_OUTPUT"