Skip to content

Git push mirror


This job does a git push to a remote git repo.

This job provides the following badge:

  • None

Dependencies

  • None

your .gitlab-ci.yml changes

To use this job add the following to your .gitlab-ci.yml file

stages:
    - sync

include:
    - remote: https://gitlab.com/nofusscomputing/projects/gitlab-ci/-/raw/development/git_push_mirror/.gitlab-ci.yml

Github (Push --mirror):
    variables:
        GIT_SYNC_URL: "https://${username variable}:${pasword variable}@github.com/NoFussComputing/gitlab-ci.git"
    extends:
        - .git_push_mirror

CI/CD Variables required

var name Description
GIT_SYNC_URL this is the remote git repositories https clone address.
Note: if the remote repository requires authentication, you will need to build the url. like above.

Job Workflow

  • This job is designed to run on successful completion of the validation tasks and only on the development and master branches. You can safely override the rules when creating the job with your own. i.e.
Github (Push --mirror):
    variables:
        GIT_SYNC_URL: "https://${username variable}:${pasword variable}@github.com/NoFussComputing/gitlab-ci.git"
    extends:
        - .git_push_mirror
    rules:
      - if: '$CI_COMMIT_BRANCH == "master"'
        when: never
      - if: '$CI_COMMIT_BRANCH == "development"'
        when: always

This will cause the job to only run on the development branch.

Artifacts

  • None

Gitlab job Definition

When you include this definition the following makes up the job definition

.gitlab-ci.yml
.git_push_mirror:
    image: alpine:latest
    stage: sync
    before_script:
        - apk update
        - apk add git
    script:
        - if [ "0$GIT_SYNC_URL" == "0"]; then echo "[ERROR] you must define variable GIT_SYNC_URL for mirroring this repository."; fi
        - git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/$CI_PROJECT_PATH --mirror $CI_PROJECT_NAME
        - cd $CI_PROJECT_NAME 
        - git remote add destination $GIT_SYNC_URL 
        - echo "https://gitlab.com/ $CI_PROJECT_PATH / $CI_PROJECT_NAME .git"
        - git push destination --mirror
    artifacts:
        expire_in: 1 day
    rules:
        - if: '$JOB_STOP_GIT_PUSH_MIRROR'
          when: never

        - if: $GIT_SYNC_URL == null
          when: never

        - if: # condition_master_or_dev_push
            ( 
              $CI_COMMIT_BRANCH == "master" || 
              $CI_COMMIT_BRANCH == "development" 
            ) && 
            $CI_PIPELINE_SOURCE == "push"
          when: always

        - when: never

Note

Docs Still under development

About:

This page forms part of our Project Gitlab-CI.

Page Metadata
Version: ToDo: place files short git commit here
Date Created: 2021-08-03
Date Edited: 2023-05-23

Contribution:

Would You like to contribute to our Gitlab-CI project? You can assist in the following ways:

 

ToDo: Add the page list of contributors