.gitlab-ci.yml 2.45 KB
Newer Older
Andrew Okin's avatar
Andrew Okin committed
1 2 3 4 5 6 7 8 9 10 11 12
image: ruby:2.4

variables:
  JEKYLL_ENV: production
  LC_ALL: C.UTF-8

cache:
  paths:
    - vendor

build:
  stage: build
13 14
  before_script:
    - bundle install --path=vendor
Andrew Okin's avatar
Andrew Okin committed
15
  script:
16
    - bundle exec jekyll build
Andrew Okin's avatar
Andrew Okin committed
17 18
  artifacts:
    paths:
19
      - _site
Andrew Okin's avatar
Andrew Okin committed
20

21 22
deploy_master:
  extends: .deploy
Andrew Okin's avatar
Andrew Okin committed
23 24 25
  only:
    refs:
      - master
Andrew Okin's avatar
Andrew Okin committed
26 27 28
  environment:
    name: production
    url: https://forkk.net
29
  script:
30 31 32 33 34
    # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    # We're using tr to fix line endings which makes ed25519 keys work
    # without extra base64 encoding.
    # https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
35 36 37 38 39 40 41 42 43 44 45 46
    # Deploy the artifacts from the build stage
    - rsync -a --delete _site/ "$SSH_DEPLOY_DEST"

deploy_test:
  extends: .deploy
  only:
    refs:
      - test
  environment:
    name: test
    url: https://test.forkk.net
  script:
47
    - echo "$SSH_TEST_PRIVATE_KEY" | tr -d '\r' | ssh-add -
48 49
    - rsync -a --delete _site/ "$SSH_TEST_DEPLOY_DEST"

50 51
deploy_branch:
  extends: .deploy
52 53 54 55 56
  only:
    - branches
  except:
    - master
    - test
57 58 59
  environment:
    name: branch/$CI_COMMIT_REF_NAME
    url: https://test.forkk.net/branch/$CI_COMMIT_REF_SLUG
60
    on_stop: stop_branch
61 62 63 64
  script:
    - echo "$SSH_TEST_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - rsync -a --delete _site/ "$SSH_BRANCH_DEPLOY_DEST/$CI_COMMIT_REF_SLUG"

65 66 67 68 69 70 71 72 73 74 75 76 77
stop_branch:
  extends: deploy_branch
  dependencies: []
  when: manual
  variables:
    GIT_STRATEGY: none
  environment:
    name: branch/$CI_COMMIT_REF_NAME
    action: stop
  script:
    - echo "$SSH_TEST_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - ssh "$SSH_BRANCH_DEPLOY_HOST" "rm -r $SSH_BRANCH_DEPLOY_PATH/$CI_COMMIT_REF_SLUG"

78 79 80 81 82 83 84 85 86 87 88
.deploy:
  stage: deploy
  dependencies:
    - build
  script: []
  before_script:
    # Ensure ssh-agent is installed
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    # Ensure rsync is installed
    - 'which rsync || ( apt-get update -y && apt-get install rsync -y )'

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    # Run ssh-agent (inside the build environment)
    - eval $(ssh-agent -s)

    # Create the SSH directory and give it the right permissions
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh

    # Add the public host key for the target server
    - echo "$SSH_HOST_KEY" > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts

    # Add the SSH config
    - echo "$SSH_CONFIG" > ~/.ssh/config
    - chmod 644 ~/.ssh/config