change use base_ref for env label #35
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: Build & Push Docker Image 🚀 | |
# Workflow that builds and pushes a Docker image for the release | |
# branches. The image will be tagged with an automatically incremented | |
# version (e.g. `1.3.0`) and the name of the release branch (`test` or | |
# `production`). The Git commit will be tagged with the version as | |
# well (e.g. `v1.3.0`). For more details, see README.md. | |
on: | |
# This workflow should only run on the release branches, when a PR | |
# has been merged (each job must include the | |
# `github.event.pull_request.merged == true` condition) | |
pull_request: | |
branches: | |
- test-ci | |
- production-ci | |
types: | |
- closed | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
jobs: | |
prerequisites: | |
runs-on: ubuntu-latest | |
if: github.event.pull_request.merged == true | |
outputs: | |
version: ${{ steps.extract-version.outputs.version }} | |
version-forelast: ${{ steps.extract-version-forelast.outputs.version }} | |
branch: ${{ steps.extract-branch.outputs.branch }} | |
steps: | |
- name: Checkout 🛎️ | |
uses: actions/checkout@v3 | |
- name: Get version tag of HEAD if any | |
id: extract-version | |
run: | | |
git fetch --prune --tags | |
echo "version=`git tag -l --points-at=HEAD | sed -En 's/^v(.*)/\1/p'`" >> $GITHUB_OUTPUT | |
- name: Get version tag of HEAD~1 if any | |
id: extract-version-forelast | |
run: | | |
git fetch --prune --tags | |
echo "version-forelast=`git tag -l --points-at=HEAD~1 | sed -En 's/^v(.*)/\1/p'`" >> $GITHUB_OUTPUT | |
- name: Extract branch name | |
id: extract-branch | |
shell: bash | |
run: | | |
echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT | |
# If the current commit has no version tag, an image needs to be | |
# built, tagged and pushed to the registry | |
build-and-push-image: | |
runs-on: ubuntu-latest | |
needs: prerequisites | |
if: github.event.pull_request.merged == true && needs.prerequisites.outputs.version == '' && needs.prerequisites.outputs.version-forelast == '' | |
strategy: | |
matrix: | |
node-version: [18] | |
permissions: | |
contents: read | |
packages: write | |
steps: | |
- name: Checkout 🛎️ | |
uses: actions/checkout@v3 | |
- name: Use Node.js ${{ matrix.node-version }} | |
uses: actions/setup-node@v3 | |
with: | |
node-version: ${{ matrix.version }} | |
cache: "npm" | |
- name: Install dependencies 🔧 | |
run: npm ci | |
- name: Build application 🏭 | |
run: | | |
npm run build | |
mv dist evento-portal | |
mkdir dist | |
mv evento-portal dist | |
- name: Bump Git version tag and push it | |
id: tag | |
uses: anothrNick/github-tag-action@1.64.0 | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
INITIAL_VERSION: 1.0.0 | |
WITH_V: true | |
- name: Login to Docker registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Determine environment and version tags based on Git & Docker registry | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: | | |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
tags: | | |
type=raw,value=${{ needs.prerequisites.outputs.branch }} | |
type=semver,pattern={{version}},value=${{ steps.tag.outputs.new_tag }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
# If the current commit has a version tag, the existing image should | |
# be reused by retagging and pushing it | |
retag-and-push-image: | |
runs-on: ubuntu-latest | |
needs: prerequisites | |
if: github.event.pull_request.merged == true && needs.prerequisites.outputs.version-forelast != '' | |
steps: | |
- name: Login to Docker registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Retag existing image & push | |
env: | |
VERSION_IMAGE: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.prerequisites.outputs.version }} | |
BRANCH_IMAGE: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.prerequisites.outputs.branch }} | |
run: | | |
docker pull $VERSION_IMAGE | |
docker tag $VERSION_IMAGE ${{ github.head_ref }} #$BRANCH_IMAGE | |
docker push $BRANCH_IMAGE |