build and push image #64
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 image | |
on: | |
workflow_dispatch: | |
inputs: | |
branch: | |
description: 'Build Docs Version From Branch, only main' | |
required: true | |
default: 'main' | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- id: checkout | |
uses: actions/checkout@v3 | |
with: | |
ref: ${{ inputs.branch }} | |
- name: branch status | |
run: | | |
git status | |
- name: Prepare | |
id: prep | |
run: | | |
COMMIT_ID=$(git rev-parse HEAD) | |
SHORT_COMMIT_ID=$(git rev-parse --short HEAD) | |
ACR_REGISTRY=registry.cn-hangzhou.aliyuncs.com | |
ACR_NAMESPACE=mocloud | |
ACR_IMAGE_NAME=matrixonecloud-docs.cn | |
ACR_IMAGE_TAG=${{ inputs.branch }}_${COMMIT_ID} | |
# Set output parameters. | |
# https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/ | |
echo "commit_id=${COMMIT_ID}" >> $GITHUB_OUTPUT | |
echo "short_commit_id=${SHORT_COMMIT_ID}" >> $GITHUB_OUTPUT | |
echo "acr_registry_name=${ACR_REGISTRY}" >> $GITHUB_OUTPUT | |
echo "acr_namespace=${ACR_NAMESPACE}" >> $GITHUB_OUTPUT | |
echo "acr_image_name=${ACR_IMAGE_NAME}" >> $GITHUB_OUTPUT | |
echo "acr_image_tag=${ACR_IMAGE_TAG}" >> $GITHUB_OUTPUT | |
- name: show info on the ${{ inputs.branch }} branch, commit ${{ steps.prep.outputs.commit_id }} | |
id: branch_commit | |
env: | |
ACR_REGISTRY: ${{ steps.prep.outputs.acr_registry_name }} | |
ACR_NAMESPACE: ${{ steps.prep.outputs.acr_namespace }} | |
ACR_IMAGE_NAME: ${{ steps.prep.outputs.acr_image_name }} | |
ACR_IMAGE_TAG: ${{ steps.prep.outputs.acr_image_tag }} | |
run: | | |
echo "commit_id:${{ steps.prep.outputs.commit_id }}" | |
echo "short_commit_id:${{ steps.prep.outputs.short_commit_id }}" | |
echo "acr_registry_name:${{ env.ACR_REGISTRY }}" | |
echo "acr_namespace:${{ env.ACR_NAMESPACE }}" | |
echo "acr_image_name:${{ env.ACR_IMAGE_NAME }}" | |
echo "acr_image_tag:${{ env.ACR_IMAGE_TAG }}" | |
- name: Git Config | |
run: | | |
git fetch --all | |
git config --global user.email ${{ secrets.GITUSEREMAIL }} | |
git config --global user.name ${{ secrets.GITUSERNAME }} | |
- name: login to aliyun ACR | |
env: | |
ACR_REGISTRY: ${{ steps.prep.outputs.acr_registry_name }} | |
uses: aliyun/acr-login@v1 | |
with: | |
login-server: ${{ env.ACR_REGISTRY }} | |
username: '${{ secrets.NEW_ACR_USERNAME }}' | |
password: '${{ secrets.NEW_ACR_PASSWORD }}' | |
- name: Set up Docker Buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Build ${{ inputs.branch }} && Push | |
uses: docker/build-push-action@v4 | |
env: | |
ACR_REGISTRY: ${{ steps.prep.outputs.acr_registry_name }} | |
ACR_NAMESPACE: ${{ steps.prep.outputs.acr_namespace }} | |
ACR_IMAGE_NAME: ${{ steps.prep.outputs.acr_image_name }} | |
ACR_IMAGE_TAG: ${{ steps.prep.outputs.acr_image_tag }} | |
with: | |
context: . # remove git-context restriction https://github.com/docker/build-push-action#git-context | |
push: true | |
tags: | | |
${{ env.ACR_REGISTRY }}/${{ env.ACR_NAMESPACE }}/${{ env.ACR_IMAGE_NAME }}:${{ env.ACR_IMAGE_TAG }} | |
${{ env.ACR_REGISTRY }}/${{ env.ACR_NAMESPACE }}/${{ env.ACR_IMAGE_NAME }}:latest | |
- name: ALI ACR Image Name and Tag from branch ${{ inputs.branch }} ${{ steps.prep.outputs.short_commit_id }} | |
env: | |
REGISTRY: ${{ steps.prep.outputs.acr_registry_name }} | |
NAMESPACE: ${{ steps.prep.outputs.acr_namespace }} | |
IMAGE_NAME: ${{ steps.prep.outputs.acr_image_name }} | |
IMAGE_TAG: ${{ steps.prep.outputs.acr_image_tag }} | |
run: | | |
echo "${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}" | |
echo "${{ env.IMAGE_TAG }}" | |
echo "${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}" | |
- name: Log out of ALI ACR | |
env: | |
REGISTRY: ${{ steps.prep.outputs.acr_registry_name }} | |
if: always() | |
run: docker logout ${{ env.REGISTRY }} |