build and push images to github container repository (ghcr) #193
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: CI and CD | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
env: | |
REGISTRY: ghcr.io | |
jobs: | |
ci-backend: | |
runs-on: ubuntu-latest | |
permissions: | |
packages: write | |
contents: read | |
env: | |
IMAGE_NAME: backend | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Setup Go | |
uses: actions/setup-go@v5 | |
with: | |
go-version: '1.23.x' | |
- name: Run unit tests | |
run: | | |
cd ./backend | |
# && go test ./... -v -race -cover | |
- name: Build image | |
run: | | |
pwd | |
docker build . --file Dockerfile --tag $IMAGE_NAME | |
- name: Log in to registry | |
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
- name: Push image | |
run: | | |
IMAGE_ID=$REGISTRY/${{ github.repository_owner }}/$IMAGE_NAME | |
# This changes all uppercase characters to lowercase. | |
IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') | |
# This strips the git ref prefix from the version. | |
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') | |
# This strips the "v" prefix from the tag name. | |
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') | |
# This uses the Docker `latest` tag convention. | |
[ "$VERSION" == "main" ] && VERSION=latest | |
echo IMAGE_ID=$IMAGE_ID | |
echo VERSION=$VERSION | |
docker tag $IMAGE_NAME $IMAGE_ID:$VERSION | |
docker push $IMAGE_ID:$VERSION | |
# cd-backend: | |
# runs-on: ubuntu-latest | |
# needs: | |
# - ci-backend | |
# steps: | |
# - name: Checkout code | |
# uses: actions/checkout@v4 | |
# - uses: actions/setup-node@v3 | |
# with: | |
# node-version: "18" | |
# - name: deploy to production | |
# env: | |
# LIARA_TOKEN: ${{ secrets.LIARA_API_TOKEN }} | |
# LIARA_BACKEND_APP_NAME: ${{secrets.LIARA_BACKEND_APP_NAME}} | |
# LIARA_FRONTEND_APP_NAME: ${{secrets.LIARA_FRONTEND_APP_NAME}} | |
# run: | | |
# IMAGE_NAME=backend | |
# IMAGE_ID=$REGISTRY/${{ github.repository_owner }}/$IMAGE_NAME | |
# VERSION=latest | |
# npm i -g @liara/cli@7 | |
# liara deploy --image $IMAGE_ID:$VERSION --platform=docker --port="80" --app="$LIARA_BACKEND_APP_NAME" --api-token="$LIARA_TOKEN" --detach | |
# cd-frontend: | |
# runs-on: ubuntu-latest | |
# needs: | |
# - cd-backend | |
# steps: | |
# - name: Checkout code | |
# uses: actions/checkout@v4 | |
# - uses: actions/setup-node@v3 | |
# with: | |
# node-version: "18" | |
# - name: deploy to production | |
# env: | |
# LIARA_TOKEN: ${{ secrets.LIARA_API_TOKEN }} | |
# LIARA_BACKEND_APP_NAME: ${{secrets.LIARA_BACKEND_APP_NAME}} | |
# LIARA_FRONTEND_APP_NAME: ${{secrets.LIARA_FRONTEND_APP_NAME}} | |
# run: | | |
# npm i -g @liara/cli@7 | |
# cd ./frontend && liara deploy --platform=docker --port="3000" --app="$LIARA_FRONTEND_APP_NAME" --api-token="$LIARA_TOKEN" --detach |