Add workflows to push to Docker Hub #48
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: Main | ||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
branches: | ||
- master | ||
release: | ||
types: | ||
- published | ||
jobs: | ||
lint: | ||
name: Lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 22 | ||
cache: 'yarn' | ||
- name: Install dependencies | ||
run: yarn install | ||
- name: Run lint | ||
run: yarn run lint | ||
test: | ||
name: Test | ||
runs-on: ubuntu-latest | ||
needs: lint | ||
services: | ||
mariadb: | ||
image: mariadb:latest | ||
ports: | ||
- 3306 | ||
env: | ||
MARIADB_USER: quotes | ||
MARIADB_PASSWORD: p4ssw0rd | ||
MARIADB_DATABASE: quotes | ||
MARIADB_ROOT_PASSWORD: p4ssw0rd | ||
options: --health-cmd="mariadb-admin ping" --health-interval=5s --health-timeout=2s --health-retries=3 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: 22 | ||
cache: 'yarn' | ||
- name: Install dependencies | ||
run: yarn install | ||
- name: Verify MariaDB connection | ||
env: | ||
PORT: ${{ job.services.mariadb.ports[3306] }} | ||
run: | | ||
while ! mysqladmin ping -h"127.0.0.1" -P"$PORT" --silent; do | ||
sleep 1 | ||
done | ||
- name: Rename config | ||
env: | ||
PORT: ${{ job.services.mariadb.ports[3306] }} | ||
run: cat src/config.ts | sed "s/3306/$PORT/g" > src/config.tmp && mv src/config.tmp src/config.ts | ||
- name: Build projects, run migrations and seeds | ||
run: | | ||
yarn run build | ||
yarn run migrate | ||
yarn run seed | ||
- name: Run tests | ||
env: | ||
TOKEN: ${{ secrets.CODECOV_TOKEN }} | ||
run: | | ||
yarn test | ||
CODECOV_TOKEN="$TOKEN" bash <(curl -s https://codecov.io/bash) | ||
build: | ||
name: Build container with tag version | ||
if: github.event_name == 'release' | ||
runs-on: ubuntu-latest | ||
needs: test | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 1 | ||
- name: Set variables | ||
id: vars | ||
run: echo ::set-output name=tag::${GITHUB_REF#refs/*/} | ||
- name: Configure AWS credentials | ||
uses: aws-actions/configure-aws-credentials@v1 | ||
with: | ||
aws-access-key-id: ${{ secrets.AWS_GRAPHQL_JUFFALOW_COM_ACCESS_KEY_ID }} | ||
aws-secret-access-key: ${{ secrets.AWS_GRAPHQL_JUFFALOW_COM_SECRET_ACCESS_KEY }} | ||
aws-region: ${{ secrets.AWS_CONTAINER_REGISTRY_REGION }} | ||
- name: Login to Amazon ECR | ||
id: login-ecr | ||
uses: aws-actions/amazon-ecr-login@v1 | ||
- name: Build, tag and push docker image to Amazon ECR | ||
env: | ||
REGISTRY: ${{ steps.login-ecr.outputs.registry }} | ||
REPOSITORY: express-graphql-example | ||
TAG: ${{ steps.vars.outputs.tag }} | ||
run: | | ||
docker build -t $REGISTRY/$REPOSITORY:$TAG . | ||
docker push $REGISTRY/$REPOSITORY:$TAG | ||
push_latest_dockerhub: | ||
name: Push image with latest tag to DockerHub | ||
if: github.event_name == 'push' | ||
runs-on: ubuntu-latest | ||
needs: [lint_test_build] | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 1 | ||
- name: Login to DockerHub | ||
env: | ||
DOCKER_HUB_USER: ${{ secrets.DOCKER_HUB_USER }} | ||
DOCKER_HUB_ACCESS_TOKEN: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | ||
run: docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_ACCESS_TOKEN | ||
- name: Build and tag Docker image | ||
env: | ||
USER: ${{ secrets.DOCKER_HUB_USER }} | ||
REPOSITORY: express-graphql-example | ||
TAG: latest | ||
run: docker build -t $USER/$REPOSITORY:$TAG . | ||
- name: Push Docker image | ||
env: | ||
USER: ${{ secrets.DOCKER_HUB_USER }} | ||
REPOSITORY: express-graphql-example | ||
TAG: latest | ||
run: docker push $USER/$REPOSITORY:$TAG | ||
push_release_dockerhub: | ||
name: Push image with release version tag to DockerHub | ||
if: github.event_name == 'release' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 1 | ||
- name: Set tag variable | ||
id: version | ||
run: echo ::set-output name=tag::${GITHUB_REF#"refs/tags/"} | ||
- name: Login to DockerHub | ||
env: | ||
DOCKER_HUB_USER: ${{ secrets.DOCKER_HUB_USER }} | ||
DOCKER_HUB_ACCESS_TOKEN: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }} | ||
run: docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_ACCESS_TOKEN | ||
- name: Build and tag Docker image | ||
env: | ||
USER: ${{ secrets.DOCKER_HUB_USER }} | ||
REPOSITORY: express-graphql-example | ||
TAG: ${{ steps.version.outputs.tag }} | ||
run: docker build -t $USER/$REPOSITORY:$TAG . | ||
- name: Push Docker image | ||
env: | ||
USER: ${{ secrets.DOCKER_HUB_USER }} | ||
REPOSITORY: express-graphql-example | ||
TAG: ${{ steps.version.outputs.tag }} | ||
run: docker push $USER/$REPOSITORY:$TAG |