Build and Push Docker Image #13
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 and Push Docker Image | |
on: | |
workflow_dispatch: | |
inputs: | |
version: | |
description: 'enter version(x.y.z)' | |
required: true | |
default: '1.0.0' | |
container_arch: | |
type: choice | |
description: 'choose container architecture' | |
default: linux/amd64 | |
options: | |
- "linux/amd64" | |
- "linux/amd64,linux/arm64" | |
env: | |
ARCH: ${{ github.event.inputs.container_arch }} | |
VERSION: ${{ github.event.inputs.version }} | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
token: ${{ secrets.MY_TOKEN }} | |
- name: Log File Contents 1 | |
run: | | |
file_path="./package.json" | |
cat "$file_path" | |
- name: Set up Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: 16 | |
- name: Restore cached node_modules | |
id: restore-node-cache | |
uses: actions/cache@v3 | |
with: | |
path: '**/node_modules' | |
key: ${{ runner.OS }}-node-modules-${{ hashFiles('**/package-lock.json') }} | |
restore-keys: | | |
${{ runner.OS }}-node-modules- | |
- name: Install dependencies | |
if: steps.restore-node-cache.outputs.cache-hit != 'true' | |
run: npm ci | |
- name: Change version | |
run: | | |
converted_version=$(echo ${{ env.VERSION }} | sed -E 's/^([0-9]+\.[0-9]+)\.([a-zA-Z]+)/\1.0-\2/') | |
npm version $converted_version --no-git-tag-version --allow-same-version --no-commit-hooks --include-workspace-root -w=web | |
echo "converted_version=$converted_version" >> "$GITHUB_OUTPUT" | |
- name: Configure git | |
run: | | |
git config --global user.email "${{ vars.GIT_EMAIL }}" | |
git config --global user.name "${{ vars.GIT_USERNAME }}" | |
- name: Check if there are any changes | |
id: check_changes | |
run: | | |
git diff --exit-code --quiet || echo "::set-output name=changed::true" | |
continue-on-error: true | |
- name: Commit changes and Get current commit SHA | |
if: steps.check_changes.outputs.changed == 'true' | |
id: get_sha | |
run: | | |
git commit -am "chore: version ${{ env.VERSION }}" | |
echo "::set-output name=sha::$(git rev-parse HEAD)" | |
- name: Push changes | |
if: steps.check_changes.outputs.changed == 'true' | |
uses: ad-m/github-push-action@master | |
with: | |
github_token: ${{ secrets.MY_TOKEN }} | |
branch: ${{ github.ref }} | |
- name: Set NEW_COMMIT_SHA env variable | |
if: steps.check_changes.outputs.changed == 'true' | |
run: echo "NEW_COMMIT_SHA=${{ steps.get_sha.outputs.sha }}" >> $GITHUB_ENV | |
- name: Install dependencies | |
run: npm install | |
- name: Build Vue.js app | |
run: npm run build | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v1 | |
- name: Log File Contents 2 | |
run: | | |
file_path="./package.json" | |
cat "$file_path" | |
- name: Login to Docker Hub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v4 | |
with: | |
context: . | |
push: true | |
platforms: ${{ env.ARCH }} | |
tags: sulmo/vue-app:${{ env.VERSION }} | |
tagging: | |
needs: build-and-push | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
with: | |
ref: ${{ env.NEW_COMMIT_SHA }} | |
- name: Configure git | |
run: | | |
git config --global user.email "${{ vars.GIT_EMAIL }}" | |
git config --global user.name "${{ vars.GIT_USERNAME }}" | |
- name: Git tagging | |
run: | | |
git tag ${{ env.VERSION }} | |
git push origin ${{ env.VERSION }} |